Articles in this section
Category / Section

How to render x-axis labels without duplicates when indexing is enabled in an Angular Stacking Bar Chart?

1 min read

Description

This article shows how to render x-axis labels without duplicates when indexing is enabled in Angular Stacking Bar Charts.

Solution

If isIndexed is set to true, this will results in the repetition of axis labels. This behavior occurs because each x-value is rendered on the axis according to the index value of the dataSource.

To avoid duplicate X-axis labels in an Angular Stacking Bar Chart when indexing is enabled, the axisLabelRender event can be implemented. This event is triggered before the axis gets rendered, allowing to filter out the duplicate X-axis labels to ensure that each label is displayed only once.

Code-snippet:

The following example shows how to filter out duplicate labels in axisLabelRender event.

app.component.html

<ejs-chart (axisLabelRender)="axisLabelRender($event)">
</ejs-chart> 

app.component.ts

public axisLabelRender(args): void {
       if (args.axis.name === 'primaryXAxis') {
           const labels: string[] = args.text.split(',').map((label) => label.trim());
           const uniqueLabels: string[] = Array.from(new Set(labels)); 
           args.text = uniqueLabels.join(', ');
       }
}; 

The following screenshot illustrates the output of the above code snippet.

Stacking Bar.png

View Sample in Stackblitz

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