Skip to content

Commit 8373049

Browse files
committed
Fix hit testing for chart labels
1 parent d09bb65 commit 8373049

3 files changed

Lines changed: 20 additions & 18 deletions

File tree

src/components/Charts/BarChart/BarChartContent.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ function BarChartContent({data, isLoading, yAxisUnit, yAxisUnitPosition = 'left'
124124
const domainWidth = bounds.right - bounds.left;
125125
const calculatedBarWidth = ((1 - BAR_INNER_PADDING) * domainWidth) / data.length;
126126
barWidth.set(calculatedBarWidth);
127-
chartBottom.set(bounds.bottom);
128127
yZero.set(0);
129128
setBarAreaWidth(domainWidth);
130129
setBoundsLeft(bounds.left);
@@ -192,6 +191,10 @@ function BarChartContent({data, isLoading, yAxisUnit, yAxisUnitPosition = 'left'
192191
if (!fontMgr || xAxisLabelHeight === undefined) {
193192
return null;
194193
}
194+
195+
const chartBoundsBottom = args.yScale(Math.min(...args.yTicks));
196+
chartBottom.set(chartBoundsBottom);
197+
195198
return (
196199
<>
197200
<ChartXAxisLabels
@@ -207,7 +210,7 @@ function BarChartContent({data, isLoading, yAxisUnit, yAxisUnitPosition = 'left'
207210
fontMgr={fontMgr}
208211
labelColor={theme.textSupporting}
209212
xScale={args.xScale}
210-
chartBoundsBottom={args.yScale(Math.min(...args.yTicks))}
213+
chartBoundsBottom={chartBoundsBottom}
211214
/>
212215
<ChartYAxisLabels
213216
yTicks={args.yTicks}

src/components/Charts/LineChart/LineChartContent.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ function LineChartContent({data, isLoading, yAxisUnit, yAxisUnitPosition = 'left
143143
setPlotAreaWidth(bounds.right - bounds.left);
144144
setBoundsLeft(bounds.left);
145145
setBoundsRight(bounds.right);
146-
chartBottom.set(bounds.bottom);
147146
};
148147

149148
const checkIsOverDot = (args: HitTestArgs) => {
@@ -175,6 +174,8 @@ function LineChartContent({data, isLoading, yAxisUnit, yAxisUnitPosition = 'left
175174
}));
176175

177176
const renderOutside = (args: CartesianChartRenderArg<{x: number; y: number}, 'y'>) => {
177+
const chartBoundsBottom = args.yScale(Math.min(...args.yTicks));
178+
chartBottom.set(chartBoundsBottom);
178179
return (
179180
<>
180181
<LeftFrameLine
@@ -202,7 +203,7 @@ function LineChartContent({data, isLoading, yAxisUnit, yAxisUnitPosition = 'left
202203
fontMgr={fontMgr}
203204
labelColor={theme.textSupporting}
204205
xScale={args.xScale}
205-
chartBoundsBottom={args.yScale(Math.min(...args.yTicks))}
206+
chartBoundsBottom={chartBoundsBottom}
206207
/>
207208
)}
208209
{!!fontMgr && (

src/components/Charts/hooks/useLabelHitTesting.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ type LabelHitGeometry = {
2121
/** Per-label: labelWidth / 2 — half-extent for 0° and 90° hit bounds */
2222
halfWidths: number[];
2323

24-
/** Per-label: rightUpperCorner.x = targetX + cornerAnchorDX[i] */
25-
cornerAnchorDX: number[];
24+
/** rightUpperCorner.x = targetX + cornerAnchorDX */
25+
cornerAnchorDX: number;
2626

27-
/** Per-label: rightUpperCorner.y = labelY + cornerAnchorDY[i] */
28-
cornerAnchorDY: number[];
27+
/** rightUpperCorner.y = labelY + cornerAnchorDY */
28+
cornerAnchorDY: number;
2929

30-
/** Per-label: yMin90 = labelY + yMin90Offsets[i] */
31-
yMin90Offsets: number[];
30+
/** yMin90 = labelY + yMin90Offset */
31+
yMin90Offset: number;
3232

3333
/** Per-label: yMax90 = labelY + yMax90Offsets[i] */
3434
yMax90Offsets: number[];
@@ -76,9 +76,9 @@ function useLabelHitTesting({fontMgr, fontSize, truncatedLabelWidths, labelRotat
7676
iconSin: variables.iconSizeExtraSmall * sinA,
7777
labelSins: truncatedLabelWidths.map((w) => w * sinA),
7878
halfWidths: truncatedLabelWidths.map((w) => w / 2),
79-
cornerAnchorDX: truncatedLabelWidths.map(() => -iconThirdSin),
80-
cornerAnchorDY: truncatedLabelWidths.map(() => iconThirdSin),
81-
yMin90Offsets: truncatedLabelWidths.map(() => padding),
79+
cornerAnchorDX: -iconThirdSin,
80+
cornerAnchorDY: iconThirdSin,
81+
yMin90Offset: padding,
8282
yMax90Offsets: truncatedLabelWidths.map((w) => w + padding),
8383
};
8484
}
@@ -94,17 +94,15 @@ function useLabelHitTesting({fontMgr, fontSize, truncatedLabelWidths, labelRotat
9494
return false;
9595
}
9696

97-
const {labelYOffset, iconSin, labelSins, halfWidths, cornerAnchorDX, cornerAnchorDY, yMin90Offsets, yMax90Offsets} = labelHitGeometry;
97+
const {labelYOffset, iconSin, labelSins, halfWidths, cornerAnchorDX, cornerAnchorDY, yMin90Offset, yMax90Offsets} = labelHitGeometry;
9898
const padding = variables.iconSizeExtraSmall / 2;
9999
const halfWidth = halfWidths.at(activeIndex) ?? 0;
100100
const labelY = args.chartBottom + labelYOffset;
101101

102102
let corners45: Array<{x: number; y: number}> | undefined;
103103
if (angleRad > 0 && angleRad < DIAGONAL_ANGLE_RADIAN_THRESHOLD) {
104104
const labelSin = labelSins.at(activeIndex) ?? 0;
105-
const anchorDX = cornerAnchorDX.at(activeIndex) ?? 0;
106-
const anchorDY = cornerAnchorDY.at(activeIndex) ?? 0;
107-
const rightUpperCorner = {x: args.targetX + anchorDX, y: labelY + anchorDY};
105+
const rightUpperCorner = {x: args.targetX + cornerAnchorDX, y: labelY + cornerAnchorDY};
108106
const rightLowerCorner = {x: rightUpperCorner.x + iconSin, y: rightUpperCorner.y + iconSin};
109107
const leftUpperCorner = {x: rightUpperCorner.x - labelSin, y: rightUpperCorner.y + labelSin};
110108
const leftLowerCorner = {x: rightLowerCorner.x - labelSin, y: rightLowerCorner.y + labelSin};
@@ -120,7 +118,7 @@ function useLabelHitTesting({fontMgr, fontSize, truncatedLabelWidths, labelRotat
120118
halfWidth,
121119
padding,
122120
corners45,
123-
yMin90: labelY + (yMin90Offsets.at(activeIndex) ?? 0),
121+
yMin90: labelY + yMin90Offset,
124122
yMax90: labelY + (yMax90Offsets.at(activeIndex) ?? 0),
125123
});
126124
};

0 commit comments

Comments
 (0)