Search code examples
javascriptbrushecharts

How to programmatically clear a brush selection in echarts


I'm using echarts library. I'm creating histograms with bar charts, and using brush selection to highlight bars. Also I want to programmatically select / unselect bars. Following echarts documentation and examples, I understand that this kind of actions are done by using chart.dispatchAction but no brush action is documented or showed. The only related things documented is an option (brush.removeOnClick) which, only if brush.brushMode === 'single' allows to clear the brush selection on click. How is it possible to programmatically create / clear brush selections? thanks


Solution

  • There is an undocumented brush action;

    Usage example:

    Clear brush with the command clear:

    chart.dispatchAction({
      type: 'brush',
      command: 'clear',
      areas: [],
    });
    

    Create a brush selection (at least in a bar chart with axisType === 'category') with:

    chart.dispatchAction({
      type: 'brush',
      areas: [
        {
          brushType: 'lineX',
          xAxisIndex: 0,
          coordRange: [from, to],
        },
      ],
    });
    

    Where coordRange is an array of 2 axis ticks indexes.

    There are a lot of undocumented things in echarts.