Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
concatenate on tooltip from data array in chart js

I am trying to get these four data values from the array and I want to display them on the tooltip along with the % symbol at the end.

var myChart = new Chart(ctx, {
        type: 'line',
        data: {
            datasets: [
                {
                    label: 'Net Profit Margin',
                    data:[, 18, 19, 21, 20,]

                },
                {
                    label: 'Gross Profit Margin',
                    data:  [, 57, 54, 57, 59,]
                },
            ]
        },

and in the tooltip, I am trying to get the data so that I can display it by using callbacks.

tooltip: {
                 
                    callbacks: {
                        label: function (tooltipItem) {
                            output = myChart.data.datasets[0].data[]
                        }
                    }


                },

Now I want to know should I write in data[___] in order to get the first object when I hover on it.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Beside the tooltip label callback, you also need to define interaction mode 'dataset'.

options: {
  interaction: {
    mode: 'dataset'
  },
  plugins: {
    tooltip: {
      callbacks: {
        label: context => context.label + ': ' + context.formattedValue + '%'
      }
    }
  }
}

Please take a look at below runnable code and see how it works.

var myChart = new Chart('myChart', {
  type: 'line',
  data: {
    labels: ['A', 'B', 'C', 'D'],
    datasets: [{
        label: 'Net Profit Margin',
        data: [18, 19, 21, 20]
      },
      {
        label: 'Gross Profit Margin',
        data: [57, 54, 57, 59]
      },
    ]
  },
  options: {
    interaction: {
      mode: 'dataset'
    },
    plugins: {
      tooltip: {
        callbacks: {
          label: context => context.label + ': ' + context.formattedValue + '%'
        }
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.min.js"></script>
<canvas id="myChart" height="80"></canvas>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda