Skip to content

Commit c1b429e

Browse files
committed
fix: Fixes cartesian tooltip placement in Firefox
1 parent c356b2f commit c1b429e

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/core/chart-api/chart-extra-tooltip.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import AsyncStore from "../../internal/utils/async-store";
1212
import { SVGRendererPool, SVGRendererSingle } from "../../internal/utils/renderer-utils";
1313
import { DebouncedCall, isEqualArrays } from "../../internal/utils/utils";
1414
import { Rect } from "../interfaces";
15-
import { getGroupRect, getPointRect, getSeriesId, isXThreshold } from "../utils";
15+
import { getGroupRect, getPointRect, getSeriesId, isXThreshold, safeRect } from "../utils";
1616
import { ChartExtraContext } from "./chart-extra-context";
1717

1818
import testClasses from "../test-classes/styles.css.js";
@@ -61,7 +61,9 @@ export class ChartExtraTooltip extends AsyncStore<ReactiveTooltipState> {
6161
// trackRef.current = targetElement.element as it might get invalidated unexpectedly.
6262
// The getTrack function ensures the latest element reference is given on each request.
6363
public getTargetTrack = () => (this.targetTrack.element?.element ?? null) as null | SVGElement;
64-
public getGroupTrack = () => (this.groupTrack.element?.element ?? null) as null | SVGElement;
64+
public getGroupTrack = () => {
65+
return (this.groupTrack.element?.element ?? null) as null | SVGElement;
66+
};
6567

6668
public onChartDestroy() {
6769
this.cursor.destroy();
@@ -222,8 +224,7 @@ function getPieChartTargetPlacement(point: Highcharts.Point): Rect {
222224
// Instead, there is a `tooltipPos` tuple, which is not covered by TS.
223225
// See: https://github.com/highcharts/highcharts/issues/23118.
224226
if ("tooltipPos" in point && Array.isArray(point.tooltipPos)) {
225-
// We use very small but non-zero track size as otherwise it is placed incorrectly in Firefox.
226-
return { x: point.tooltipPos[0], y: point.tooltipPos[1], width: 0.1, height: 0.1 };
227+
return safeRect({ x: point.tooltipPos[0], y: point.tooltipPos[1], width: 0, height: 0 });
227228
}
228229
// We use the alternative, middle, tooltip placement as a fallback, in case the undocumented "tooltipPos"
229230
// is no longer available in the point.
@@ -244,10 +245,10 @@ function getPieMiddlePlacement(point: Highcharts.Point): Rect {
244245

245246
function getSolidGaugeTargetPlacement(point: Highcharts.Point): Rect {
246247
const chart = point.series.chart;
247-
return {
248+
return safeRect({
248249
x: chart.plotLeft + chart.plotWidth / 2,
249250
y: chart.plotTop + chart.plotHeight / 2,
250-
width: 0.1,
251-
height: 0.1,
252-
};
251+
width: 0,
252+
height: 0,
253+
});
253254
}

src/core/utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,16 @@ export function getGroupRect(points: readonly Highcharts.Point[]): Rect {
221221
maxX = Math.max(maxX, r.x + r.width);
222222
maxY = Math.max(maxY, r.y + r.height);
223223
}
224-
return chart.inverted
225-
? { x: chart.plotLeft, y: minY, width: chart.plotWidth, height: maxY - minY }
226-
: { x: minX, y: chart.plotTop, width: maxX - minX, height: chart.plotHeight };
224+
return safeRect(
225+
chart.inverted
226+
? { x: chart.plotLeft, y: minY, width: chart.plotWidth, height: maxY - minY }
227+
: { x: minX, y: chart.plotTop, width: maxX - minX, height: chart.plotHeight },
228+
);
229+
}
230+
231+
// We ensure rect width and height are non-zero to ensure its position is calculated correctly in Firefox.
232+
export function safeRect(rect: Rect): Rect {
233+
return { ...rect, width: Math.max(0.1, rect.width), height: Math.max(0.1, rect.height) };
227234
}
228235

229236
export function getChartAccessibleDescription(chart: Highcharts.Chart) {

0 commit comments

Comments
 (0)