Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

267
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda