How can I change color of only one column in Barchart using Chart.js

情到浓时终转凉″ 提交于 2020-01-04 03:56:28

问题


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


来源:https://stackoverflow.com/questions/26800128/how-can-i-change-color-of-only-one-column-in-barchart-using-chart-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!