Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing style of individual point Chart.js

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?

like image 352
agreis1 Avatar asked Aug 21 '18 16:08

agreis1


1 Answers

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!

like image 70
simplelenz Avatar answered Sep 23 '22 08:09

simplelenz