Chartjs-ChartJS timeline graph with events

0👍

First, you need to define the xAxis as a time cartesian axis. Then you can define the data of your dataset as individual points through objects containing x and y properties.

Please note that 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.

const img = new Image(16, 16);
img.src = 'https://i.stack.imgur.com/Q94Tt.png'; 

var ctx = document.getElementById('chart').getContext('2d');
var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    datasets: [{
      data: [
        { x: "2020-03-22", y: 0 },
        { x: "2020-04-01", y: 0 },
        { x: "2020-04-02", y: 0 },
        { x: "2020-04-03", y: 0 },
        { x: "2020-04-08", y: 0 },
        { x: "2020-04-12", y: 0 },
        { x: "2020-04-15", y: 0 }
      ],
      pointStyle: img,
      borderWidth: 1
    }]
  },
  options: {
    legend: {
      display: false
    },
    scales: {
      yAxes: [{
        ticks: {
          display: false,
        },        
        gridLines: {
          display: false
        }
      }],
      xAxes: [{
        type: 'time',
        time: {
          unit: 'day',
          tooltipFormat: 'MMM DD'
        },
        gridLines: {
          display:false
        }
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="chart" height="28"></canvas>

Leave a comment