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

230
Visualizações
chart js double tooltip on hover

How do you create custom tooltips that doesn't repeat if the points are on the same (x,y)? Take a look at "C" here. I'd like to only show the tooltip label once. It's the same information in any tooltip that is going to be shown if the points are on the same X-value.

var labels = ["A", "B", "C", "D"];
var d1 = [1, 2, 3, 10];
var d2 = [3, 4, 3, 7];
var dd1 = 'y';
var dd2 = 'y';

var ctx = $("#lchart");
var lchart = new Chart(ctx, {
   type: "line",
   data: {
      labels: labels,
      datasets: [{
            cubicInterpolationMode: "monotone",
            label: "s1",
            data: d1,
            pointHoverRadius: 6,
            pointRadius: 6,
            pointBorderWidth: 3
         },
         {
            cubicInterpolationMode: "monotone",
            label: "s2",
            data: d2,
            pointHoverRadius: 6,
            pointRadius: 6,
            pointBorderWidth: 3,
         }

      ]
   },
   options: {
      legend: {
         display: false
      },
      tooltips: {
         yPadding: 15,
         xPadding: 15,
         backgroundColor: "yellow",
         titleFontSize: 14,
         titleFontColor: "#999",
         bodyFontColor: '#000',
         bodyFontSize: 12,
         displayColors: false,
         callbacks: {
            label: function(tooltipItem, data) {
               var labelrows = [
                  "Title:",
                  dd1,
                  "",
                  dd2
               ];
               return labelrows;
            }
         }
      }
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<body>
    <canvas id="lchart"></canvas>
</body>

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

0

Dont hardcode your response for the label and it runs for every label, you will need to use the title callback and just use the data passed in the arg for the value of the item:

var labels = ["A", "B", "C", "D"];
var d1 = [1, 2, 3, 10];
var d2 = [3, 4, 3, 7];
var dd1 = 'y';
var dd2 = 'y';

var ctx = $("#lchart");
var lchart = new Chart(ctx, {
   type: "line",
   data: {
      labels: labels,
      datasets: [{
            cubicInterpolationMode: "monotone",
            label: "s1",
            data: d1,
            pointHoverRadius: 6,
            pointRadius: 6,
            pointBorderWidth: 3
         },
         {
            cubicInterpolationMode: "monotone",
            label: "s2",
            data: d2,
            pointHoverRadius: 6,
            pointRadius: 6,
            pointBorderWidth: 3,
         }

      ]
   },
   options: {
      legend: {
         display: false
      },
      tooltips: {
         yPadding: 15,
         xPadding: 15,
         mode: "index",
         backgroundColor: "yellow",
         titleFontSize: 14,
         titleFontColor: "#999",
         bodyFontColor: '#000',
         bodyFontSize: 12,
         displayColors: false,
         callbacks: {
            beforeBody: () => ('Title:'),
            label: function(ttItem, data) {
               return ttItem.yLabel;
            }
         }
      }
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<body>
    <canvas id="lchart"></canvas>
</body>

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