eugen.a
Posts: 1
Joined: Fri Jun 03, 2022 9:05 am

Failing to extend Tooltip.prototype.refresh to display nearest points of other series

Hi,
I'm trying to extend the `Tooltip.prototype.refresh` method with `H.wrap` to make it display the closest points of other series on the chart (for cases when x values of series do not always match).

I've found a stackoverflow thread to kickstart my implementation

But unfortunately the accepted there solution didn't work for me. For two reasons:
1. points[0].series.chart.series array contains 4 series (2 factual from the chart and 2 with names "Navigator 2"). Which i tried to solve filtering out the series that contain the substring "Navigator".
2. type of points from the arguments of the refresh method mismatch with the one from points from "non-navigator" series from the chart (my guess is that it might be related to the HighchartsStock extension). So when I find the nearest point from the chart and push it to the array of points that i get in the arguments to the refresh method Highcharts crashes with the following error:
TypeError: Cannot read properties of undefined (reading 'isCartesian')

Here's my code:

Code: Select all

H.wrap(
    H.Tooltip.prototype,
    'refresh',
    function (this: typeof Highcharts, proceed, points: Highcharts.Point[], ...rest) {
      const currentPoint = points[0];
      const { chart } = currentPoint.series;

      chart.series
        .filter((series: Highcharts.Series) => !series.name.includes('Navigator'))
        .forEach((series: Highcharts.Series) => {
          // Don't process a series of a currentPoint
          if (series.name === currentPoint.series.name) {
            return;
          }

          let closestPoint: Highcharts.Point | null = null;
          let distance: number;
          let minimalDistance = Number.MAX_VALUE;

          series.points.forEach((point: Highcharts.Point) => {
            distance = Math.abs(point.x - currentPoint.x);
            if (distance < minimalDistance) {
              minimalDistance = distance;
              closestPoint = point;
            }
          });

          // Add the closest point to the array of current tooltip points
          if (closestPoint && !points.includes(closestPoint)) {
            points.push(closestPoint);
          }
        });

      proceed.apply(this, [points, ...rest]);
    },
  );

Any hints, clues or advices on how to move this from the dead-point are highly appreciated!
Thanks,
Eugen
michal.f
Posts: 1114
Joined: Thu Aug 12, 2021 12:04 pm

Re: Failing to extend Tooltip.prototype.refresh to display nearest points of other series

Hello,

Welcome to our forum and thanks for contacting us with your question!

I was trying to reproduce your problem but without success. Could you reproduce the issue in an online editor based on this demo: https://codesandbox.io/s/highcharts-typ ... c/index.ts

Best regards!
Michał Filipiec
Highcharts Developer
https://blacklabel.pl/highcharts/

Return to “Highcharts Stock”