Chartjs-Chart.js – Timeline

0👍

Your code looks fine except that you don’t need to define data.labels since the data in your dataset is defined as individual points through objects containing x and y properties.

Chart.js internally uses Moment.js for the functionality of the time axis. Therefore you should use the bundled version of Chart.js that includes Moment.js in a single file.

var ctx = document.getElementById('graph').getContext('2d');
var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    datasets: [{
      label: "Something",
      data: [
        { x: "2013-02-08", y: 1 }, 
        { x: "2013-02-10", y: 10 }
      ]
    }]
  },
  options: {
    scales: {
      xAxes: [{
        type: 'time',
        time: {
          unit: 'day'
        }
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="graph"></canvas>

Leave a comment