How to Avoid Duplicate X-Axis Labels in Angular Stacking Bar Chart?
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 result 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 the filtering out of 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:
Refer to the working sample for additional details and implementation : View Sample in Stackblitz
Conclusion
We hope you enjoyed learning about how to avoid duplicate X-axis labels in the Angular Stacked Bar Chart.
You can refer to our Angular Charts feature tour page to learn about its other groundbreaking features and documentation, as well as how to quickly get started with configuration specifications. You can also explore our Angular Charts example to understand how to create and manipulate data.
For current customers, you can check out our components on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to explore 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!