How to Use LegendClick to Toggle Legend Items in Angular Charts?
Description
This article demonstrates how to enable and disable legend items across multiple Angular charts using the legendClick event. It allows you to toggle the visibility of corresponding series across charts for synchronized interactivity.
Solution
In Angular charts, you can handle the legendClick event to toggle the visibility of a series by updating its visible property. When a legend item is clicked, the legendClick event is triggered, and you can access the clicked series via args.series.
To synchronize this behavior across multiple charts, you can apply the same visibility change to the corresponding series in the other chart(s).
Code Snippet
app.component.html
<ejs-chart #chart1 (legendClick)="legendClick($event)"></ejs-chart>
<ejs-chart #chart2 (legendClick)="legendClick1($event)"></ejs-chart>
app.component.ts
public legendClick(args: ILegendClickEventArgs): void {
this.index = args.series.index;
this.chart2.series[this.index].visible = args.series.visible ? false : true;
this.chart2.refresh();
}
public legendClick1(args: ILegendClickEventArgs): void {
this.index = args.series.index;
this.chart1.series[this.index].visible = args.series.visible ? false : true;
this.chart1.refresh();
}
The following image illustrates the output of the above code.
Conclusion
We hope you enjoyed learning about How to use LegendClick to Toggle Legend Items in Angular Charts.
You can refer to our Angular Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Angular Chart example to understand how to create and manipulate data.
For current customers, you can check out our 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 other controls.
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!