How can I change color of only one column in Barch

2019-07-04 07:31发布

问题:

js

http://www.chartjs.org/docs/#bar-chart

I am using the basic sample:

HTML

<canvas id="canvas" height="450" width="600"></canvas>

JS

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]
        }
    ]
};

window.onload = function(){
    var ctx = document.getElementById("canvas").getContext("2d");
    window.myBar = new Chart(ctx).Bar(data, {
         responsive : true
    });
}

I wanted to change color of one column. How can I do it? I tried several times a lot of tutorials, but I didnt find solution. Can you help me?

回答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();