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

174
Vistas
How to get the label value from bar chart in javascript?

So I've got a bar chart which has two different bars for supply and demand and I want it to tell me which one it is when I click on the bar. Because right now it just gives me the same index and x axis when I click on the bars. So how can I fix this problem? Here is my code :

var canvas = document.getElementById('myChart');
var data = {
    labels: ["iOS", "Android", "React", "Angular", "SA", "BA", "Backbase"],
    datasets: [
        {
            label: "supply",
            fill: false,
            lineTension: 0.1,
            backgroundColor: "rgba(75,192,192,0.4)",
            borderColor: "rgba(75,192,192,1)",
            borderCapStyle: 'butt',
            borderDash: [],
            borderDashOffset: 0.0,
            borderJoinStyle: 'miter',
            pointBorderColor: "rgba(75,192,192,1)",
            pointBackgroundColor: "#fff",
            pointBorderWidth: 1,
            pointHoverRadius: 5,
            pointHoverBackgroundColor: "rgba(75,192,192,1)",
            pointHoverBorderColor: "rgba(220,220,220,1)",
            pointHoverBorderWidth: 2,
            pointRadius: 5,
            grouped: true,
            pointHitRadius: 10,
            data: [1, 2, 3, 4, 5, 6, 7],
        },
                {
            label: "demand",
            fill: false,
            lineTension: 0.1,
            backgroundColor: "rgba(75,29,192,0.4)",
            borderColor: "rgba(75,192,192,1)",
            borderCapStyle: 'butt',
            borderDash: [],
            borderDashOffset: 0.0,
            borderJoinStyle: 'miter',
            pointBorderColor: "rgba(75,192,192,1)",
            pointBackgroundColor: "#fff",
            pointBorderWidth: 1,
            pointHoverRadius: 5,
            pointHoverBackgroundColor: "rgba(75,192,192,1)",
            pointHoverBorderColor: "rgba(220,220,220,1)",
            pointHoverBorderWidth: 2,
            pointRadius: 5,
            grouped: true,
            pointHitRadius: 10,
            data: [5, 9, 8, 1, 6, 5, 4],
        }
        
    ]
};

var option = {
    showLines: true,
  onClick: function(evt) {
  
   console.log(myLineChart.getElementAtEvent(evt));
   alert(data.datasets[0].data[myLineChart.getElementAtEvent(evt)[0]._index]);
   alert(data.labels[myLineChart.getElementAtEvent(evt)[0]._index]);
  
    
    }
};
var myLineChart = Chart.Bar(canvas,{
    data:data,
  options:option
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Hey guys I figured it out here is the code :

alert(data.labels[myLineChart.getElementAtEvent(evt)[0]._index])
alert(data.datasets[myLineChart.getElementAtEvent(evt)[0]._datasetIndex].label)
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