Chartjs-Cannot change the color of column chart according to its value

0👍

barChartData is an array of ChartDataSets that contains a single element. Therefore, to make it work, you could change your code as follows:

[colors]="barChartData[0].data.map(v => v > 0 ? barChartColors[0] : barChartColors[1])"

or more compact…

[colors]="barChartData[0].data.map(v => barChartColors[v > 0 ? 0 : 1])"

This code uses Array.map(), on the data array inside ChartDataSets. It returns a new array with the appropriate color for each value.

Leave a comment