How to Hide/Show Series by Clicking Single Legend in JavaScript Chart?
This article explains how to use the legendClick event in JavaScript Chart to toggle the visibility of all series when a single legend item is clicked. This approach overrides the default behavior to ensure consistent toggling across all series.
Implementation Steps
- Handle the legendClick event to control series visibility.
- Disable default legend toggle behavior using legendSettings.toggleVisibility = false.
- Toggle visibility of all series, including the one clicked.
Code Example
let chart: Chart = new Chart({
.....
legendClick: (args: ILegendClickEventArgs) => {
for (let i = 0; i < chart.visibleSeries.length; i++) {
if (args.series.index === args.chart.visibleSeries[i].index) {
args.chart.visibleSeries[i].visible === true ? true : false;
} else {
args.chart.visibleSeries[i].visible =
!args.chart.visibleSeries[i].visible;
}
}
},
legendSettings: { toggleVisibility: true }
});
chart.appendTo('#container');
Output
Before Legend Click:
After Legend Click:
Live Sample
Conclusion
I hope you enjoyed learning about how to hide/show series by clicking single legend in JavaScript Chart.
You can refer to our JavaScript Chart feature tour page to learn about its other groundbreaking feature representations. You can also explore our JavaScript Chart Documentation to understand how to present and manipulate data.
For current customers, you can check out our JavaScript components on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to explore our JavaScript Chart and other JavaScript components.
If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our support forums, Direct-Trac or feedback portal, or the feedback portal. We are always happy to assist you!