Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

265
Views
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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!