I want to change the style of a single point in my Chart.js graph. I tried the solutions here
ChartJS - Different color per data point
But none of them work for me. For example, to change the background color of the 4th point in my first dataset, I would try the following 2 solutions (and also removing/adding .config):
myChart.config.data.datasets[0].points[4].pointBackgroundColor = 'red';
myChart.config.data.datasets[0].pointBackgroundColor[4] = "red";
myChart.update();
to no effect. On the contrary, changing the style of the entire dataset works perfectly fine:
myChart.config.data.datasets[0].pointBackgroundColor = 'lightgreen';
myChart.update();
jsfiddle: https://jsfiddle.net/e8n4xd4z/15766/; Why is this?
https://jsfiddle.net/e8n4xd4z/15822/
You can use an array of colors to the pointBackgroundColor
in the options
as follows
pointBackgroundColor: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"]
Then if you want to change any individual point color later you can do that as follows
myChart.config.data.datasets[0]['pointBackgroundColor'][4] = 'red';
I have attached the link to the updated jsfiddle. You can check that. Cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With