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

205
Vistas
Chart.JS tooltip callbacks label and title (v3.5)

(Please note: There are lots of answers for v2, this is for v3)

I'm trying to setup tooltips label and title for a doughnut chart.

Code:

        //Create the donut chart
    donut = new Chart('questions_positivity_donut', {
        type: 'doughnut',
        data: { 
            labels: ["Positive", "Other"],
            datasets: [{
                label: 'Sentiment',
                data: [user_context.state.avg_joy, (1-user_context.state.avg_joy)],
                backgroundColor: ['#a9a9a9','#f2f2f2']
            }]
        },
        options: {
            cutout: "70%",
            plugins: {
                legend: {
                    display: false
                },
                maintainAspectRatio: false,
                responsive: true,
                tooltip: {
                    callbacks: {
                        label: function(context) {

                            let label = new Intl.NumberFormat('en-US', {style: 'percent', minimumFractionDigits: 0, maximumFractionDigits: 0}).format(context.formattedValue);
                            return label;
                        },
                        title: function(context) {

                            let title = context.parsed.x;
                            return title;
                        } 
                    },
                    displayColors: false
                }                   
            }
        }
    }); 

The label now works, and displays the value of the data, but the title is returning blank, instead of returning the label of the data ("Positive" or "Other").

How can I return the correct title in the tooltip.callback?

Example: "Positive 35%" and "Other 65%"

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

0

If you log the context you could see its an array containing objects, with the default interaction mode you are using it only contains a single item so you can select that one and then access the label attribute on it like so:

var options = {
  type: 'doughnut',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      backgroundColor: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"]
    }]
  },
  options: {
    plugins: {
      tooltip: {
        callbacks: {
          label: function(context) {

            let label = new Intl.NumberFormat('en-US', {
              style: 'percent',
              minimumFractionDigits: 0,
              maximumFractionDigits: 0
            }).format(context.formattedValue);
            return label;
          },
          title: function(context) {
            let title = context[0].label;
            return title;
          }
        },
      }
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/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