javascript - Charts.js - How to set custom tooltip text for each dataset - Stack Overflow

Using charts.js, how do I add custom tooltip text for each dataset?For dataset 1, I want to add "s

Using charts.js, how do I add custom tooltip text for each dataset?

For dataset 1, I want to add "some text 1" to the tooltip.

For dataset 2, I want to add "some text 2" to the tooltip.

Using tooltip callbacks I can add extra text to the tooltip, but it applies the same text to the tooltips of both datasets. My code below is using a callback to do that. But how can I change the tooltip text for each dataset?

var chart = new Chart(ctx, {
  // The type of chart we want to create
  type: "line",
  // The data for our dataset
  data: {
    labels: monthLabels,
    datasets: [{
      label: "Dataset 1",
      data: [12, 123, 234, 32, 23],
    }, {
      label: "Dataset 2",
      data: [4, 54, 765, 45, 5],
    }]
  },
  // Configuration options go here
  options: {
    tooltips: {
      enabled: true,
      mode: 'single',
      callbacks: {
        label: function(tooltipItems, data) {
          return tooltipItems.yLabel + 'some text here';
        }
      }
    }
  }
});

Using charts.js, how do I add custom tooltip text for each dataset?

For dataset 1, I want to add "some text 1" to the tooltip.

For dataset 2, I want to add "some text 2" to the tooltip.

Using tooltip callbacks I can add extra text to the tooltip, but it applies the same text to the tooltips of both datasets. My code below is using a callback to do that. But how can I change the tooltip text for each dataset?

var chart = new Chart(ctx, {
  // The type of chart we want to create
  type: "line",
  // The data for our dataset
  data: {
    labels: monthLabels,
    datasets: [{
      label: "Dataset 1",
      data: [12, 123, 234, 32, 23],
    }, {
      label: "Dataset 2",
      data: [4, 54, 765, 45, 5],
    }]
  },
  // Configuration options go here
  options: {
    tooltips: {
      enabled: true,
      mode: 'single',
      callbacks: {
        label: function(tooltipItems, data) {
          return tooltipItems.yLabel + 'some text here';
        }
      }
    }
  }
});
Share Improve this question asked May 1, 2020 at 7:39 TinyTigerTinyTiger 2,19112 gold badges66 silver badges131 bronze badges 1
  • Have you tried reading this? chartjs/docs/latest/configuration/… – Federico Provenziani Commented May 1, 2020 at 7:52
Add a ment  | 

1 Answer 1

Reset to default 4

You can do this easily by referring to the current dataset index tooltipItems.datasetIndex and then based on that index set the tooltip text like:

label: function(tooltipItems, data) {
    var text = tooltipItems.datasetIndex === 0 ? 'some text 1' : 'some text 2'
    return tooltipItems.yLabel + ' ' + text;
}

Working Demo:

var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
  // The type of chart we want to create
  type: "line",
  // The data for our dataset
  data: {
    labels: Array.from({length: 5}, (x,i)=> `Label ${i+1}`),
    datasets: [{
      label: "Dataset 1",
      data: [12, 123, 234, 32, 23],
    }, {
      label: "Dataset 2",
      data: [4, 54, 765, 45, 5],
    }]
  },
  // Configuration options go here
  options: {
    tooltips: {
      enabled: true,
      mode: 'single',
      callbacks: {
        label: function(tooltipItems, data) {
          var text = tooltipItems.datasetIndex === 0 ? 'some text 1' : 'some text 2'
          return tooltipItems.yLabel + ' ' + text;
        }
      }
    }
  }
});
.chart-container {
   width: 500px;
}
#myChart {
  display: block; 
  width: 500px; 
}
<script src="https://cdn.jsdelivr/npm/[email protected]"></script>

<div class="chart-container">
    <canvas id="myChart"></canvas>
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745522812a4631337.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信