Change chart.js bar color - Javascript Chart.js

Javascript examples for Chart.js:Bar Chart

Description

Change chart.js bar color

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script> 
      <script type="text/javascript">
    $(window).load(function(){/*from   w w  w  . ja  v  a2s  .c o m*/
var dData1 = 90; 
var dData2 = 70; 
var barChartData = {
    labels: ["M?S", "ANO"],
    datasets: [{
        fillColor: "#f37c8a",
        strokeColor: "none",
        data: [dData1, dData2]
    }]
}
var ctx = document.getElementById("myChart").getContext("2d");
var barChart = new Chart(ctx).Bar(barChartData);
if (dData1 < 75) {
    barChart.datasets[0].bars[0].fillColor = barChart.datasets[0].bars[0].highlightFill = barChart.datasets[0].bars[0]._saved.fillColor = "#f37c8a";
}
else if (dData1 > 76 && dData1 < 85) {
    barChart.datasets[0].bars[0].fillColor = barChart.datasets[0].bars[0].highlightFill = barChart.datasets[0].bars[0]._saved.fillColor = "#f3e97c";
}
else if (dData1 > 86) {
    barChart.datasets[0].bars[0].fillColor = barChart.datasets[0].bars[0].highlightFill = barChart.datasets[0].bars[0]._saved.fillColor = "#9ae27d";
} else {
    barChart.datasets[0].bars[0].fillColor = barChart.datasets[0].bars[0].highlightFill = barChart.datasets[0].bars[0]._saved.fillColor = "#fff";
};
barChart.update();
    });

      </script> 
   </head> 
   <body> 
      <canvas id="myChart"></canvas>  
   </body>
</html>

Related Tutorials