Júnior Lobo
Júnior Lobo

Reputation: 23

Chart.js duration time graph

I'm trying to build a chart of the top 10 activities that take the most time to perform and I'm basing myself on the duration of each one.

I created an example in Google Spreadsheet to exemplify what i need.

enter image description here

I couldn't create a graph with 'hour' data, so I converted it to seconds. But I would like to display the hours on the X axis as in the image.

Here's my code: enter link description here

<canvas id="myChart" height="200"></canvas>

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>

<script>
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  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: 5000
        }
      }]
    }
  }
});
</script>

Any tips?

Upvotes: 0

Views: 405

Answers (1)

uminder
uminder

Reputation: 26150

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>

Upvotes: 1

Related Questions