How to format y axis values as millions in a React chart?
This article explains how to format y axis values as millions in the React Chart.
Customize the y axis values as millions
Axis labels are generated based on the provided data points. You can customize the entire axis label text by using the axisLabelRender event.
In the following code example, the primary y axis values are formatted in millions.
Index.js
import { createRoot } from 'react-dom/client';
import * as React from 'react';
import { ChartComponent, SeriesCollectionDirective, SeriesDirective, Inject, Legend, Category,
StackingColumnSeries, Tooltip, Highlight } from '@syncfusion/ej2-react-charts';
export let data1 = [
{ year: '2018', revenue: 12500000 },
{ year: '2019', revenue: 13800000 },
{ year: '2020', revenue: 11500000 },
{ year: '2021', revenue: 14200000 },
{ year: '2022', revenue: 12000000 }
];
export let data2 = [
{ year: '2018', revenue: 8500000 },
{ year: '2019', revenue: 9200000 },
{ year: '2020', revenue: 7500000 },
{ year: '2021', revenue: 8100000 },
{ year: '2022', revenue: 7800000 }
];
export let data3 = [
{ year: '2018', revenue: 6300000 },
{ year: '2019', revenue: 7100000 },
{ year: '2020', revenue: 5900000 },
{ year: '2021', revenue: 6600000 },
{ year: '2022', revenue: 7200000 }
];
const StackedColumn = () => {
const axisLabelRender = (args) => {
if (args.axis.name == 'primaryYAxis') {
args.text = args.text.replace('0000000', '0M').replace('000000', 'M');
}
};
return (
<ChartComponent
id="charts"
primaryXAxis={{
valueType: 'Category',
title: 'Year'
}}
primaryYAxis={{
labelFormat: '{value}',
title: 'Sales Revenue (In Millions)'
}}
title="International Sales Revenue Data"
axisLabelRender={axisLabelRender.bind(this)}
>
<Inject
services={[ StackingColumnSeries, Category, Legend, Tooltip, Highlight ]}
/>
<SeriesCollectionDirective>
<SeriesDirective
dataSource={data1}
xName="year"
yName="revenue"
name="USD"
type="StackingColumn"
legendShape="Circle"
/>
<SeriesDirective
dataSource={data2}
xName="year"
yName="revenue"
name="GBP"
type="StackingColumn"
legendShape="Circle"
/>
<SeriesDirective
dataSource={data3}
xName="year"
yName="revenue"
name="EUR"
type="StackingColumn"
legendShape="Circle"
/>
</SeriesCollectionDirective>
</ChartComponent>
);
};
export default StackedColumn;
const root = createRoot(document.getElementById('sample'));
root.render(<StackedColumn />);
The following screenshot portrays the results of the code snippet mentioned above.
Screenshot:
Conclusion
I hope you enjoyed learning how to format y axis values as millions in the 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, support portal, or feedback portal. We are always happy to assist you!