[Chartjs]-How to display legend in toolbox in Charts.js?

0👍

You can achieve this by using a custom callback to alter the text of the tooltip like so:

  options: {
    tooltips: {
        callbacks: {
        label: (tooltipItem, data) => (`${data.datasets[tooltipItem.datasetIndex].label}(${tooltipItem.xLabel},${tooltipItem.yLabel})`)
      }
    }
  }

Working example:

var options = {
  type: 'scatter',
  data: {
    datasets: [{
      label: '# of Votes',
      data: [{
        x: 5,
        y: 4
      }, {
        x: 2,
        y: 7
      }, {
        x: 3,
        y: 3
      }, {
        x: 3.5,
        y: 5.5
      }],
      backgroundColor: 'red'
    }]
  },
  options: {
    tooltips: {
      callbacks: {
        label: (tooltipItem, data) => (`${data.datasets[tooltipItem.datasetIndex].label}(${tooltipItem.xLabel},${tooltipItem.yLabel})`)
      }
    }
  }
}

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/2.9.4/Chart.js" integrity="sha512-hZf9Qhp3rlDJBvAKvmiG+goaaKRZA6LKUO35oK6EsM0/kjPK32Yw7URqrq3Q+Nvbbt8Usss+IekL7CRn83dYmw==" crossorigin="anonymous"></script>
</body>

Leave a comment