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

182
Vistas
Accessing data from the callback function of a bar chart using chart js

I am currently trying to show a text stocked in the data in the tooltip, I can#t figure how to make it work, I have tried many things.

You can find below an example of what I#m trying to do, I want that the "text" value is displayed when hovering one of the bar.

I tried this

let data = [{
      label: 'Available',
      backgroundColor: '#3366ff',
      data: [500],
      text : "test1"
    }, {
      label: 'Budget',
      backgroundColor: '#009999',
      data: [5000, 6500],
      text : "test2"
    }, {
      label: 'Actual',
      backgroundColor: '#92d400',
      data: [5200, 7245],
      text : "test3"
    }];

    new Chart(document.getElementById("bar-chart-horizontal"), {
        type: 'horizontalBar',
        data: {
            labels: ["Rooms", "Guests"],
            datasets: data,
        },
        options: {
            scales: {
                xAxes: [{
                    ticks: {
                        beginAtZero: true
                    }
                }]
            },
            tooltips: {
                callbacks: {
                    label: function(tooltipItem, data) {
                        var item = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
                        return item.text;
                    }
                }
            }
        }
    });

But this is not working, it is not even displaying anything logical, only "Room" and "Guests".

What should I do to retrieve the correct informations ? I tried doing it like this : Fiddle

But it doesn't work when applied to my code... I'm probably missing something pretty stupid but I can't figure what.

Thanks to all the people that will read this and try to help me.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This should work:

...
options: {
  tooltips: {
    callbacks: {
      label: (context, data)  => data.datasets[context.datasetIndex].data[context.index].text
    }
  }
...

jsfiddle

about 4 years ago · Juan Pablo Isaza Denunciar

0

You just need to update the version of chart.js you are using to the latest V2 version since V3 has breaking changes.

Also you need to specify the x for the number for a horizontalBar instead of a y in both the dataset and the tooltip callback:

var ctx = document.getElementById('chartJSContainer').getContext('2d');

new Chart(ctx, {
  type: 'horizontalBar',
  data: {
    labels: ['2017/06/12', '2017/06/23', '2017/07/12', '2017/07/23', '2017/08/12', '2017/08/23', '2017/09/12'],
    datasets: [{
      label: 'Values',
      data: [{
          x: 12,
          value: 12,
          text: "test1"
        },
        {
          x: 3,
          value: 13,
          text: "test2"
        },
        {
          x: 1,
          value: 15,
          text: "test3"
        },
        {
          x: -3,
          value: 5,
          text: "test4"
        },
        {
          x: 67,
          value: 18,
          text: "test5"
        },
        {
          x: 12,
          value: 11,
          text: "test6"
        },
        {
          x: 13,
          value: 19,
          text: "test7"
        }
      ],
      fill: false,
      borderWidth: 2
    }]
  },
  options: {
    tooltips: {
      callbacks: {
        label: function(tooltipItem, data) {
          var item = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
          return item.x + ' ' + item.value + item.text;
        }
      }
    },
    responsive: true,
    maintainAspectRatio: false,
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        }
      }]
    }
  }
});
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
</body>

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