Chartjs-Change color of certain bars of same dataset periodically Chartjs

0👍

You can just set backgroundColor as an array of colors with the same length as your bar graph data.

For example:

const config = {
  type: 'bar',
  data: {
    labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
    datasets: [
      {
        label: 'Dataset 1',
        backgroundColor: ['red', 'red', 'blue', 'blue', 'blue', 'red', 'red'],
        borderWidth: 1,
        data: [-3, 30, -10, 33, -9, 14, -11],
      }
    ],
  },
  options: {
    legend: {
      display: false
    }
  }
};

const ctx = document.getElementById('canvas').getContext('2d');
new Chart(ctx, config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.js"></script>
<body>
    <canvas id="canvas" width="600" height="400"></canvas>
</body>

Leave a comment