Change point style in Chartjs via external event - Javascript Chart.js

Javascript examples for Chart.js:Event

Description

Change point style in Chartjs via external event

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://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){//from w w w .  j a  va2s  . c  om
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [72, 49, 43, 49, 35, 82],
      pointBackgroundColor: ["red", "blue", "yellow", "green", "purple", "orange"]
    }]
  },
/*   options: {
    onClick: function(evt, activeElements) {
      var elementIndex = activeElements[0]._index;
      this.data.datasets[0].pointBackgroundColor[elementIndex] = 'white';
      this.update();
    }
  } */
});
function highLightGraph(pointToChange) {
   var chartIndexArray = myChart.data
  var chartIndex = chartIndexArray.labels.indexOf(pointToChange)
  // This won't work either since you are setting the array pointBackgroundColor to 'white', so everything is white
  //myChart.data.datasets[0].pointBackgroundColor = 'white'; // changes all colours, not what I want
  // This won't work since data[chartIndex] is a number.
  //myChart.data.datasets[0].data[chartIndex].pointBackgroundColor = 'white';
  // What you have to do is change the pointBackgroundColor's array, not it's value
  myChart.data.datasets[0].pointBackgroundColor[chartIndex] = 'white';
  myChart.update();
}
highLightGraph("Blue")
    }

      </script> 
   </head> 
   <body> 
      <canvas id="myChart" width="400" height="200"></canvas>  
   </body>
</html>

Related Tutorials