Phil
Phil

Reputation: 4069

ChartJS 2.6 - Change color of bar in Bar chart programmatically

Can anyone please show me how to change the background/hoverColor of a given bar chart color? I'm logging out the chart object to the console, and I do not see anything obvious that I can set to accomplish this. I want to be able to change a column color in a bar chart on demand programmatically.

All I can see in the chart object is chart.data.datasets[0]._meta.data[0]._view.backgroundColor attribute. But there must be an easier way than to travel all the way down the object like this, isn't there?

enter image description here

Upvotes: 1

Views: 3042

Answers (1)

Tot Zam
Tot Zam

Reputation: 8746

You should be able to access an array of the background colors directly in the datasets object:

chart.data.datasets[0].backgroundColor[0]

Example usage: https://jsfiddle.net/6d0jsyxu/1/

Upvotes: 3

Related Questions