So I created a bar chart using echarts(v4.6.0) library inside Reactjs project. It has legend, data series chunked into groups with same colour and dataZoom slider. Every legend label corresponds to particular group of bars in the graph(they have same colours). However at the moment if user clicks on legend label, bars that correspond to this label disappears. What I want to achieve is when user clicks on legend label, the chart needs to zoom in to the group of bars that correspond to it instead of hiding them. Is there any way to do this?
Asked
Active
Viewed 927 times
0

valentin Ivanov
- 83
- 1
- 10
1 Answers
0
In the common practice to make bar selected you can use the emphasis
, it's style set activated by mouseover (something like rules declaration under :hover
in CSS), for example (see tutorial):
itemStyle: {
// default styles
normal: {
shadowBlur: 200,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
// activated by mouseover
emphasis: {
shadowBlur: 200,
shadowColor: 'rgba(0, 0, 0, 0.8)'
}
}
If you need just to show bar in focus, it's easy:
- Define
emphasis
styles. - Capture event
legendselectchanged
on legend component and suppress default behavior (see workaround) for prevent series disappear. - Dispatch
highlight action
for series or dataIndex and bar will be looks like selected.
I could be wrong, but as far as I know to zoom single (and stacked) bar it's not easy task. There are several unconventional ways like a draw resized bar clone above original, manipulations with datazoom component and recalculate/update bar data on mouseover and then return back old data on mouseout. Let me know if you choose one of these options I'm will try to help you.

Sergey Fedorov
- 3,696
- 2
- 17
- 21
-
+ tutorial https://echarts.apache.org/en/tutorial.html#Events%20and%20Actions%20in%20ECharts events and actions. – Sergey Fedorov May 23 '20 at 16:45
-
Thanks Sergey, unfortunately I tried to add `legendselectchanged` listener using on chart object as described in library but I receive an error saying `Property 'on' does not exist on type 'Chart'.` Maybe they are still not introduced in v4.6.0? I couldn't find version for which the documentation is about. – valentin Ivanov May 26 '20 at 12:00
-
Event working fine with 4.6, see example (http://jsfiddle.net/sbdy4v6n/5/). Probably you have a littlemistake little. Obviously you called the subscription on global – Sergey Fedorov May 26 '20 at 17:57
-
Apologies for late reply, there is snippet of my code : `const container = useRef
(document.createElement('div')); let myChart: Chart; myChart = echarts.init(container); options = { ... } myChart.setOption(options, { notMerge: true, silent: true }); myChart.on('legendselectchanged', function(params) { console.log("params-",params); }); ` – valentin Ivanov May 28 '20 at 20:36