[Chartjs]-ChartJS – How to change color of some data points in graph

1👍

you can provide an array of colors for option: pointBackgroundColor
and optionally: pointBorderColor

each color in the array represents a point on the line.

see following working snippet…

window.chartColors = {
  red: 'rgb(255, 99, 132)',
  orange: 'rgb(255, 159, 64)',
  yellow: 'rgb(255, 205, 86)',
  green: 'rgb(51, 204, 51)',
  blue: 'rgb(54, 162, 235)',
  purple: 'rgb(153, 102, 255)',
  grey: 'rgb(201, 203, 207)'
};


var options2 = {
  type: 'line',
  data: {
    labels: ["02/07/2020 8:01", "02/07/2020 13:00", "02/07/2020 17:00", "02/07/2020 22:00", "02/08/2020 8:01", "02/08/2020 13:01", "02/08/2020 17:00", "02/08/2020 22:00", "02/09/2020 8:01", "02/09/2020 13:00", "02/09/2020 17:00", "02/09/2020 22:00"],
    datasets: [{
      label: 'Water Level',
      data: [13.534, 13.652, 13.298, 13.062, 11.763, 13.613, 13.534, 12.629, 11.369, 13.495, 13.574, 13.456],
      borderWidth: 1,
      lineTension: 0,
      fill: false,
      borderColor: window.chartColors.blue,

      // provide colors for each point
      pointBackgroundColor: [window.chartColors.blue, window.chartColors.blue, window.chartColors.blue, window.chartColors.red, window.chartColors.orange, window.chartColors.yellow, window.chartColors.green, window.chartColors.purple, window.chartColors.grey, window.chartColors.blue, window.chartColors.blue, window.chartColors.blue],
      pointBorderColor: [window.chartColors.blue, window.chartColors.blue, window.chartColors.blue, window.chartColors.red, window.chartColors.orange, window.chartColors.yellow, window.chartColors.green, window.chartColors.purple, window.chartColors.grey, window.chartColors.blue, window.chartColors.blue, window.chartColors.blue]
    }],
    scales: {
      xAxes: [{
        ticks: {
          beginAtZero: false
        }
      }]
    }
  },
  options: {
    legend: {
      position: 'bottom',
      display: false
    },
    responsive: true,
    bezierCurve: false,
    scales: {
      xAxes: [{
        ticks: {
          autoSkip: true,
          maxRotation: 90,
          minRotation: 90
        }
      }],
      yAxes: [{
        ticks: {
          min: 0,
          max: 36,
          stepSize: 10
        },
        scaleLabel: {
          display: true,
          labelString: 'inches'
        }
      }]
    }
  },
  plugins: []
};
var ctx2 = document.getElementById('chart2').getContext('2d');
var chart2 = new Chart(ctx2, options2);

function RefreshGraph2() {
  console.log(chart2);
  var dt2 = {
    serial: "310021000e51353532343635",
    from: $("#txtFrom2").val(),
    to: $("#txtTo2").val()
  };

  $.post("/Mobile/GetGraphData", dt2, function(data) {
    var labels2 = _.pluck(data.result, 'createdAtLabel');
    var values2 = _.pluck(data.result, 'waterLevel');
    chart2.data.labels = labels2;
    chart2.data.datasets[0].data = values2;
    console.log(labels2);
    console.log(values2);
    //chart2.data.datasets[0].data = data.result;
    chart2.update();
    return false;
  });
}

$(document).ready(function() {
  $("#btnRefresh2").on("click", function() {
    RefreshGraph2();
    return false;
  });
});
.row {
  background: #f8f9fa;
  margin-top: 20px;
}

.col {
  border: solid 1px #6c757d;
  padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.3"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css">
<div id="collapse2TertiaryOne0" class="collapse show" style="padding:0px;">
  <div class="" style="padding: 0px;">
    <div class="tabs">
      <ul class="nav nav-tabs nav-justified" role="group">
        <li class="nav-item" role="button">
          test
        </li>
      </ul>
      <div class="tab-content">
        <div id="dataLog0" class="tab-pane active">
          <canvas class="wrapper col-12" id="chart2" style="padding-left:2px;padding-right:2px" name="chart2"></canvas>
        </div>
      </div>
    </div>
  </div>
</div>

Leave a comment