Chartjs-Chart.js duration time graph

1👍

You can define a ticks.callback function on your x-axis as follows:

xAxes: [{
  ticks: {
    min: 0,
    max: 45000,
    stepSize: 7200,
    callback: v => Number.isInteger(v / 3600) ? (v / 3600) + ':00:00' : ''
  }
}]

Please take a look at your amended and runnable code below and see how it works.

var myChart = new Chart('myChart', {
  type: 'horizontalBar',
  data: {
    labels: ["Work", "Study", "Gym"],
    datasets: [{
      label: '# activities',
      data: [30000, 9000, 5400],
      time: ['08:20', '02:30', '01:30:00'],
      backgroundColor: [
        'rgba(255, 99, 132, 0.2)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)'
      ],
      borderColor: [
        'rgba(255,99,132,1)',
        'rgba(54, 162, 235, 1)',
        'rgba(255, 206, 86, 1)'
      ],
      borderWidth: 1
    }]
  },
  options: {
    tooltips: {
      callbacks: {
        label: function(tooltipItem, data) {
          return 'Duration ' + data['datasets'][0]['time'][tooltipItem['index']];
        }
      }
    },
    scales: {
      xAxes: [{
        ticks: {
          min: 0,
          max: 45000,
          stepSize: 7200,
          callback: v => Number.isInteger(v / 3600) ? (v / 3600) + ':00:00' : ''
        }
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
<canvas id="myChart" height="90"></canvas>

Leave a comment