[Chartjs]-Chart.js bar color based on labels values

1👍

Instead of using bars[i].value property, you can use bars[i].label which gives you the label of the xAxe.

So in your loop, change to this :

for(i=0;i<bars.length;i++){
    var color="green";

    if(bars[i].label == "bad"){
        color="red";
    }
    else if(bars[i].label == "average"){
        color="orange"
    }
    else{
        color="green"
    }
    bars[i].fillColor = color;
}

You can find the full code in this jsFiddle and here is its result :

enter image description here

Leave a comment