Articles in this section
Category / Section

How to Toggle Series Based on Clicked Legend in Angular Charts?

2 mins read

Description

This article explains how to dynamically toggle chart series visibility in Angular Charts by interacting with the legend. When a legend item is clicked, only the corresponding series is displayed, hiding the rest. Clicking the same legend again restores the chart to show all series.

Solution

Using the legendClick event, the chart can react to legend interactions by comparing the clicked item’s legendText and toggling the visibility of the series accordingly.

Code Snippet

The following example shows how to toggle series based on the clicked legend.

app.component.ts

  public previousSelected = null;
  public legendClick(args): void {
       if (this.previousSelected === args.legendText)
       {
           for (let i = 0; i < args.chart.series.length; i++)
           {
               if (args.chart.series[i].name === this.previousSelected)
               {
                   args.chart.series[i].visible = false;
               }
               else
               {
                   args.chart.series[i].visible = true;
               }
           }
           this.previousSelected = null;
       }

       else
       {
           for (let j = 0; j < args.chart.series.length; j++)
           {
               args.chart.series[j].visible = false;
           }
           this.previousSelected = args.legendText;
       }
   }

Output

The following screenshot illustrates the output of the above code snippet

image.png

Live Example

Refer to the working sample for additional details and implementation: View Sample in Stackblitz

Conclusion

we hope you enjoyed learning about how to toggle series based on clicked legend in Angular Charts.

You can refer to our Angular Chart feature tour page to learn about its other groundbreaking feature representations. You can explore our Angular chart documentation to understand how to present and manipulate data.

For current customers, you can check out our Angular components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our Angular Diagram and other Angular components.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk Support, or 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