Articles in this section
Category / Section

How to Hide/Show Series by Clicking Single Legend in JavaScript Chart?

2 mins read

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

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:

image.png

After Legend Click:

image.png

Live Sample

StackBlitz 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!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied