Chart.js to add new property to dataset - Javascript Chart.js

Javascript examples for Chart.js:Chart Data

Description

Chart.js to add new property to dataset

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>chart.js 2.1</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.0/Chart.bundle.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){//from   ww  w . ja  v a  2 s  .  c  om
var roadsChartData = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [{
    code: ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
    label: "My First dataset",
    data: [65, 0, 80, 81, 56, 85, 40],
    fill: false
  }]
}
var ctx = document.getElementById("roads-chart").getContext("2d");
var roadsBar = new Chart(ctx, {
  type: 'bar',
  data: roadsChartData,
  options: {
    title: {
      display: true,
      text: "Stan Start"
    },
    tooltips: {
      mode: 'label'
    },
    responsive: true,
    scales: {
      xAxes: [{
        stacked: true,
      }],
      yAxes: [{
        stacked: true
      }]
    },
    onClick: function(e, d) {
      if (d.length > 0) {
        console.log(this.config.data.datasets[d[0]._datasetIndex].code[d[0]._index]);
      }
    }
  }
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="roads-chart"></canvas>  
   </body>
</html>

Related Tutorials