0

I'm trying to integrate this tooltip-based-on-mouse-position customization into a react-js-2 bar chart. I see where to put the new options:

options: {
  tooltips: {
    mode: 'index',
    position: 'cursor',
    intersect: false
  }
}

in my existing options block declaration. But I don't understand how/where in the app do I integrate the related function?

Chart.Tooltip.positioners.cursor = function(chartElements, coordinates) {
  return coordinates;
};

I've tried simply declaring it as Chart.Tooltip.positioners.cursor = ... or ChartJS.Tooltip.positioners.cursor = ... or Tooltip.positioners.cursor = ... in a few different places but it either causes an error or has no effect. I'm on ChartJS v3.8.0 and react-js-2 v4.1.0. Thank you.

1 Answers1

1

Your options are wrong, you need to define the tooltip options in the options.plugins.tooltip namespace and not the options.tooltips namespace.

Example:

Chart.Tooltip.positioners.mouse = function(items, evtPos) {
  return evtPos
};

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderColor: 'pink'
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderColor: 'orange'
      }
    ]
  },
  options: {
    plugins: {
      tooltip: {
        intersect: false,
        position: 'mouse',
      }
    }
  }
}

const 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/3.8.0/chart.js"></script>
</body>

React import:

import { Chart, registerables, Tooltip } from "chart.js";
import { Chart as ReactChartJs } from "react-chartjs-2";

Chart.register(...registerables);

Tooltip.positioners.mouse = function(items, evtPos) {
  return evtPos
};

const options = {
  plugins: {
    tooltip: {
      position: 'mouse',
      intersect: false
    }
  }
}

<ReactChartJs type="line" data={data} options={options} />
LeeLenalee
  • 27,463
  • 6
  • 45
  • 69
  • 1
    Thank you very much, works perfectly. I was going to ask how to know that ChartJs considers tooltip as a plugin, then I found it [here](https://www.chartjs.org/docs/latest/configuration/tooltip.html). Being new to both libraries, the combined architecture is a little confusing. For example, what considerations would go into deciding between using `` component? Also, for a custom tooltip, would I be supplying a React component? Or straight javascript/HTML? Any comments and/or pointers much appreciated. – thoroughlyConfused Jun 08 '22 at 06:57
  • I think the `` you are stuck with a line. For the custom tooltip I dont know if you can supply a react component – LeeLenalee Jun 08 '22 at 07:34
  • I now notice that while the solution above _positions_ the tooltip nicely, the data provided by the tooltip is error-prone. It changes as I slide along a given bar in the chart, sometimes displaying the label and data of other bars in the chart. (Horizontal Bar Chart.) – thoroughlyConfused Jun 15 '22 at 06:44
  • Could probably be solved by setting `mode: 'y'` in the options of the tooltip – LeeLenalee Jun 15 '22 at 07:15