How to customize each label of the axis in different format in React Chart?
This article explains how to customize each label of the axis in different format in React Chart.
Customize each label of the axis in a different format
You can change the individual axis labels by using the axisLabelRender event.
In the following example, the axis label render event is used to represent the first label of each month in month format, while the remaining labels within the same month are represented in day format.
Index.js
import { createRoot } from 'react-dom/client';
import * as React from 'react';
import { ChartComponent, SeriesCollectionDirective, SeriesDirective, Inject, LineSeries, DateTime } from '@syncfusion/ej2-react-charts';
let month = null;
const Line = () => {
const axisLabelRender = (args) => {
if (args.axis.name === 'primaryXAxis') {
const labelValue = new Date(args.value);
if (month !== labelValue.getMonth()) {
month = labelValue.getMonth();
args.text = labelValue.toLocaleString('en-US', { month: 'short' });
} else {
args.text = labelValue.getDate().toString();
}
}
};
return (
<ChartComponent
id="charts"
primaryXAxis={{
valueType: 'DateTime',
edgeLabelPlacement: 'Shift',
intervalType: 'Days',
interval: 11,
minimum: new Date(2012, 6, 1),
maximum: new Date(2012, 8, 27)
}}
primaryYAxis={{
rangePadding: 'None',
minimum: 0,
maximum: 20,
interval: 4
}}
axisLabelRender={axisLabelRender.bind(this)}
>
<Inject services={[LineSeries, DateTime]} />
<SeriesCollectionDirective>
<SeriesDirective
dataSource={data1}
xName="x"
yName="y"
type="Line"
></SeriesDirective>
</SeriesCollectionDirective>
</ChartComponent>
);
};
export default Line;
const root = createRoot(document.getElementById('sample'));
root.render(<Line />);
The screenshot below provides a visual representation of the result obtained from the mentioned code snippet.
Screenshot:
Conclusion
I hope you enjoyed learning how to customize each label of the axis in different format in React Chart Component.
You can refer to our React 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 React 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, Direct-Trac, or feedback portal. We are always happy to assist you!