How to customize the point color based on the y value in React Chart?
This article explains how to set the different colors to chart point based on its y value in the React Chart.
Customize the series color based on y value
You can easily customize the series color based on the y value using the pointRender event and adjust the data label fill color with the textRender event.
In the following code example, the series and data label fill colors are customized using events.
Index.js
import { createRoot } from 'react-dom/client';
import * as React from 'react';
import { ChartComponent, SeriesCollectionDirective, SeriesDirective, Inject, ColumnSeries, Category, DataLabel
} from '@syncfusion/ej2-react-charts';
const Column = () => {
const pointRender = (args) => {
if (args.point.y === 50) {
args.fill = 'blue';
} else if (args.point.y <= 29) {
args.fill = 'green';
} else if (args.point.y >= 40 && args.point.y <= 64) {
args.fill = 'red';
} else if (args.point.y >= 30 && args.point.y <= 32) {
args.fill = 'blue';
}
};
const textRender = (args) => {
if (args.text === '50') {
args.color = 'blue';
} else if (args.text <= '29') {
args.color = 'green';
} else if (args.text >= '40' && args.text <= '64') {
args.color = 'red';
} else if (args.text >= '30' && args.text <= '32') {
args.color = 'blue';
}
};
return (
<ChartComponent
id="charts"
primaryXAxis={{
valueType: 'Category',
interval: 1
}}
primaryYAxis={{
minimum: 0,
maximum: 70,
interval: 5
}}
pointRender={pointRender.bind(this)}
textRender={textRender.bind(this)}
>
<Inject services={[ColumnSeries, Category, DataLabel]} />
<SeriesCollectionDirective>
<SeriesDirective
dataSource={data}
xName="x"
yName="y"
type="Column"
columnWidth={0.9}
marker={{
dataLabel: {
visible: true,
position: 'Outer',
rx: 0,
ry: 0,
border: { width: 2 }
}
}}
/>
</SeriesCollectionDirective>
</ChartComponent>
);
};
export default Column;
const root = createRoot(document.getElementById('sample'));
root.render(<Column />);
The following screenshot illustrate the output of the above code snippet.
Screenshot:
Conclusion
I hope you enjoyed learning how to set the different colors to chart point based on its y value 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!