Chartjs-It is possible to always show all tooltip in Chartjs 3.1

0👍

If you want all datapoints you cant use the tooltip for that, you will have to use the datalabels plugin for that: https://v2_0_0-beta_1–chartjs-plugin-datalabels.netlify.app/samples/charts/line.html

If you only want to show 1 datapoint with the tooltip on download you can programaticatlly trigger the tooltip like so:

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1
      }
    ]
  },
  options: {}
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
var chart = new Chart(ctx, options);

document.getElementById("triggger").addEventListener("click", () => {
  const tooltip = chart.tooltip;
  if (tooltip.getActiveElements().length > 0) {
    tooltip.setActiveElements([], {
      x: 0,
      y: 0
    });
  } else {
    const chartArea = chart.chartArea;
    tooltip.setActiveElements([{
      datasetIndex: 0,
      index: 2,
    }, {
      datasetIndex: 1,
      index: 2,
    }]);
  }

  chart.update();
});
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <br>
  <button id="triggger">
      trigger hover
    </button>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.2.0/chart.js" integrity="sha512-opXrgVcTHsEVdBUZqTPlW9S8+99hNbaHmXtAdXXc61OUU6gOII5ku/PzZFqexHXc3hnK8IrJKHo+T7O4GRIJcw==" crossorigin="anonymous"></script>
</body>

Leave a comment