[Chartjs]-How can I change color of only one column in Barchart using Chart.js

1👍

You can alter the colors like this. Here is a jsfiddle

var data = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            label: "My First dataset",
            fillColor: "rgba(220,220,220,0.5)",
            strokeColor: "rgba(220,220,220,0.8)",
            highlightFill: "rgba(220,220,220,0.75)",
            highlightStroke: "rgba(220,220,220,1)",
            data: [65, 59, 80, 81, 56, 55, 40]
        }
    ]
};
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx).Bar(data);

// change color of first bar
myChart.datasets[0].bars[0].fillColor = "rgba(255,0,0,1)";
myChart.update();

1👍

As for nowadays, you can specify backgroundColor as an array of color per each dataset record:

{ datasets: [{
                label: t('Dataset with last column red'),
                backgroundColor: graphData.length
                  ? [...new Array(calculatedGraphData.length - 1).fill('#ff0000'), '#ff0000']
                  : '#ff0000',
                data: graphData,
              }] }

Leave a comment