Adding Common Legends to Single-Series Charts
Environment
Product | Progress® Kendo UI® for Angular Charts |
Description
How can I add a common legend for two or more single-series charts like Pie, Donut, or Funnel, instead of having separate legends for each chart?
Solution
-
Add a new chart with same categories but set their values to zero so that only the legends are seen.
<kendo-chart> <kendo-chart-legend position="top"></kendo-chart-legend> <kendo-chart-series> <kendo-chart-series-item type="pie" [data]="[ { category: 'A', value: 0 }, { category: 'B', value: 0 }, { category: 'C', value: 0 } ]" field="value" categoryField="category"> </kendo-chart-series-item> </kendo-chart-series> </kendo-chart>
-
Add the
legendItemClickEvent
to the legend-only chart.<kendo-chart (legendItemClick)="click($event)"> </kendo-chart>
-
Add Template Reference Variables to the
SeriesItemComponent
of the all charts.<kendo-chart> <kendo-chart-series> <kendo-chart-series-item #chartSeries1> </kendo-chart-series-item> </kendo-chart-series> </kendo-chart>
-
Add
ViewChild
for the Template Reference Variable.@ViewChild("chartSeries1") public toggleChartSeries1: any;
-
Use the
togglePointVisibility
method in theViewChild
variables during thelegendItemClick
event.public click(e: LegendItemClickEvent): void { this.toggleChartSeries1.togglePointVisibility(e.pointIndex); }
The following example demonstrates the full implementation of the suggested approach.