react-native-livechart 4.26.0 → 4.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/dist/components/CustomReferenceLineOverlay.d.ts +8 -1
  2. package/dist/components/CustomReferenceLineOverlay.d.ts.map +1 -1
  3. package/dist/components/CustomTooltipOverlay.d.ts +3 -2
  4. package/dist/components/CustomTooltipOverlay.d.ts.map +1 -1
  5. package/dist/components/LiveChart.d.ts +1 -1
  6. package/dist/components/LiveChart.d.ts.map +1 -1
  7. package/dist/components/LiveChartSeries.d.ts.map +1 -1
  8. package/dist/components/ReferenceLineOverlay.d.ts +4 -0
  9. package/dist/components/ReferenceLineOverlay.d.ts.map +1 -1
  10. package/dist/components/ReferenceValueBadge.d.ts +15 -0
  11. package/dist/components/ReferenceValueBadge.d.ts.map +1 -0
  12. package/dist/components/XAxisOverlay.d.ts +17 -0
  13. package/dist/components/XAxisOverlay.d.ts.map +1 -1
  14. package/dist/core/liveChartEngineTick.d.ts +4 -0
  15. package/dist/core/liveChartEngineTick.d.ts.map +1 -1
  16. package/dist/core/liveChartSeriesEngineTick.d.ts +2 -0
  17. package/dist/core/liveChartSeriesEngineTick.d.ts.map +1 -1
  18. package/dist/core/resolveConfig.d.ts +28 -1
  19. package/dist/core/resolveConfig.d.ts.map +1 -1
  20. package/dist/core/useLiveChartEngine.d.ts +7 -0
  21. package/dist/core/useLiveChartEngine.d.ts.map +1 -1
  22. package/dist/core/useLiveChartSeriesEngine.d.ts +2 -0
  23. package/dist/core/useLiveChartSeriesEngine.d.ts.map +1 -1
  24. package/dist/draw/grid.d.ts +2 -2
  25. package/dist/draw/line.d.ts +15 -1
  26. package/dist/draw/line.d.ts.map +1 -1
  27. package/dist/hooks/crosshairShared.d.ts +16 -1
  28. package/dist/hooks/crosshairShared.d.ts.map +1 -1
  29. package/dist/hooks/referenceBadgeFont.d.ts +5 -0
  30. package/dist/hooks/referenceBadgeFont.d.ts.map +1 -0
  31. package/dist/hooks/resolveChartLayout.d.ts +8 -0
  32. package/dist/hooks/resolveChartLayout.d.ts.map +1 -1
  33. package/dist/hooks/useBadge.d.ts +16 -1
  34. package/dist/hooks/useBadge.d.ts.map +1 -1
  35. package/dist/hooks/useChartSkiaFont.d.ts +1 -0
  36. package/dist/hooks/useChartSkiaFont.d.ts.map +1 -1
  37. package/dist/hooks/useCrosshair.d.ts +7 -2
  38. package/dist/hooks/useCrosshair.d.ts.map +1 -1
  39. package/dist/hooks/useCrosshairSeries.d.ts +4 -1
  40. package/dist/hooks/useCrosshairSeries.d.ts.map +1 -1
  41. package/dist/hooks/useLatestCallback.d.ts +6 -0
  42. package/dist/hooks/useLatestCallback.d.ts.map +1 -0
  43. package/dist/hooks/useMarkers.d.ts.map +1 -1
  44. package/dist/hooks/usePanScroll.d.ts +3 -1
  45. package/dist/hooks/usePanScroll.d.ts.map +1 -1
  46. package/dist/hooks/usePinchZoom.d.ts.map +1 -1
  47. package/dist/hooks/useReferenceDrag.d.ts +20 -5
  48. package/dist/hooks/useReferenceDrag.d.ts.map +1 -1
  49. package/dist/hooks/useReferenceLine.d.ts +24 -12
  50. package/dist/hooks/useReferenceLine.d.ts.map +1 -1
  51. package/dist/hooks/useReferenceLinePress.d.ts +4 -2
  52. package/dist/hooks/useReferenceLinePress.d.ts.map +1 -1
  53. package/dist/hooks/useReferenceTagStack.d.ts +31 -0
  54. package/dist/hooks/useReferenceTagStack.d.ts.map +1 -0
  55. package/dist/hooks/useScrubMarkers.d.ts +12 -0
  56. package/dist/hooks/useScrubMarkers.d.ts.map +1 -0
  57. package/dist/hooks/useXAxis.d.ts +1 -1
  58. package/dist/hooks/useXAxis.d.ts.map +1 -1
  59. package/dist/hooks/useYAxisScaleGesture.d.ts +34 -0
  60. package/dist/hooks/useYAxisScaleGesture.d.ts.map +1 -0
  61. package/dist/index.d.ts +1 -1
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/math/axisBadgeLayout.d.ts +16 -0
  64. package/dist/math/axisBadgeLayout.d.ts.map +1 -0
  65. package/dist/math/markers.d.ts +3 -0
  66. package/dist/math/markers.d.ts.map +1 -1
  67. package/dist/math/range.d.ts +1 -1
  68. package/dist/math/range.d.ts.map +1 -1
  69. package/dist/math/referenceTagStack.d.ts +36 -0
  70. package/dist/math/referenceTagStack.d.ts.map +1 -0
  71. package/dist/math/scrubMarkers.d.ts +12 -0
  72. package/dist/math/scrubMarkers.d.ts.map +1 -0
  73. package/dist/types.d.ts +202 -32
  74. package/dist/types.d.ts.map +1 -1
  75. package/package.json +1 -1
  76. package/src/components/CustomReferenceLineOverlay.tsx +50 -8
  77. package/src/components/CustomTooltipOverlay.tsx +5 -1
  78. package/src/components/LiveChart.tsx +560 -138
  79. package/src/components/LiveChartSeries.tsx +113 -13
  80. package/src/components/ReferenceLineOverlay.tsx +62 -26
  81. package/src/components/ReferenceValueBadge.tsx +102 -0
  82. package/src/components/XAxisOverlay.tsx +104 -1
  83. package/src/core/liveChartEngineTick.ts +49 -4
  84. package/src/core/liveChartSeriesEngineTick.ts +37 -0
  85. package/src/core/resolveConfig.ts +95 -2
  86. package/src/core/useLiveChartEngine.ts +43 -9
  87. package/src/core/useLiveChartSeriesEngine.ts +5 -0
  88. package/src/draw/grid.ts +2 -2
  89. package/src/draw/line.ts +23 -1
  90. package/src/hooks/crosshairShared.ts +56 -1
  91. package/src/hooks/referenceBadgeFont.ts +35 -0
  92. package/src/hooks/resolveChartLayout.ts +17 -0
  93. package/src/hooks/useBadge.ts +44 -5
  94. package/src/hooks/useChartSkiaFont.ts +1 -1
  95. package/src/hooks/useCrosshair.ts +444 -310
  96. package/src/hooks/useCrosshairSeries.ts +29 -10
  97. package/src/hooks/useLatestCallback.ts +15 -0
  98. package/src/hooks/useMarkers.ts +51 -39
  99. package/src/hooks/usePanScroll.ts +155 -127
  100. package/src/hooks/usePinchZoom.ts +84 -63
  101. package/src/hooks/useReferenceDrag.ts +390 -155
  102. package/src/hooks/useReferenceLine.ts +453 -277
  103. package/src/hooks/useReferenceLineGrouping.ts +1 -1
  104. package/src/hooks/useReferenceLinePress.ts +121 -53
  105. package/src/hooks/useReferenceTagStack.ts +81 -0
  106. package/src/hooks/useScrubMarkers.ts +79 -0
  107. package/src/hooks/useXAxis.ts +11 -2
  108. package/src/hooks/useYAxisScaleGesture.ts +263 -0
  109. package/src/index.ts +10 -0
  110. package/src/math/axisBadgeLayout.ts +37 -0
  111. package/src/math/markers.ts +28 -3
  112. package/src/math/range.ts +35 -0
  113. package/src/math/referenceTagStack.ts +112 -0
  114. package/src/math/scrubMarkers.ts +55 -0
  115. package/src/types.ts +207 -32
@@ -3,10 +3,7 @@ import { useRef } from "react";
3
3
 
4
4
  import type { SkFont } from "@shopify/react-native-skia";
5
5
  import type { ChartEngineLayout } from "../core/useLiveChartEngine";
6
- import {
7
- rightAnchoredYAxisColumnLayout,
8
- type YAxisEntry,
9
- } from "../draw/grid";
6
+ import { rightAnchoredYAxisColumnLayout, type YAxisEntry } from "../draw/grid";
10
7
  import type { ChartPadding } from "../draw/line";
11
8
  import {
12
9
  measureFontTextWidth,
@@ -17,10 +14,12 @@ import {
17
14
  referenceLineForm,
18
15
  resolveReferenceBadge,
19
16
  } from "../math/referenceLines";
17
+ import { axisBadgeBounds, type AxisBadgeConfig } from "../math/axisBadgeLayout";
20
18
  import type { ReferenceLine } from "../types";
21
19
 
22
20
  /** Screen-space geometry for one reference line / band, recomputed each frame. */
23
21
  export interface ReferenceLineLayout {
22
+ valueBadge: ValueBadgeLayout | null;
24
23
  visible: boolean;
25
24
  /** Line y, or band top-edge y. */
26
25
  y: number;
@@ -66,7 +65,15 @@ export interface ReferenceLineLayout {
66
65
  connEnd: number;
67
66
  }
68
67
 
68
+ export interface ValueBadgeLayout extends ReferenceBadgeRect {
69
+ text: string;
70
+ textX: number;
71
+ textY: number;
72
+ chevronCx: number;
73
+ }
74
+
69
75
  const INVISIBLE: ReferenceLineLayout = {
76
+ valueBadge: null,
70
77
  visible: false,
71
78
  y: -1,
72
79
  yBottom: -1,
@@ -134,9 +141,7 @@ function badgeGeometry(
134
141
  // price-pill centering in computeActionBadgeLayout.
135
142
  const iconBounds = icon ? font.measureText(icon) : null;
136
143
  const iconW = iconBounds ? iconBounds.width : 0;
137
- const textW = text
138
- ? measureFontTextWidth(font, text, textWidthCache)
139
- : 0;
144
+ const textW = text ? measureFontTextWidth(font, text, textWidthCache) : 0;
140
145
 
141
146
  let contentW = 0;
142
147
  let count = 0;
@@ -223,67 +228,6 @@ export interface ReferenceBadgeRect {
223
228
  h: number;
224
229
  }
225
230
 
226
- /**
227
- * Screen-space pill rect for a Form-A reference line's **badge**, or `null` when
228
- * the line has no pressable badge (no `badge`/`offAxisBadge`, not a value line, a
229
- * legacy off-axis badge while in range, or the canvas isn't laid out / range is
230
- * degenerate). Shares `badgeGeometry` + the edge classification with
231
- * {@link useReferenceLine}, so the hit-target tracks the rendered pill exactly.
232
- * Pure worklet — used by {@link useReferenceLinePress} for tap hit-testing.
233
- */
234
- export function computeReferenceBadgeRect(
235
- line: ReferenceLine,
236
- canvasWidth: number,
237
- canvasHeight: number,
238
- padding: ChartPadding,
239
- dMin: number,
240
- dMax: number,
241
- font: SkFont,
242
- formatValue: (value: number) => string,
243
- /** Live value override (e.g. a dragged value), defaulting to `line.value`. */
244
- valueOverride?: number,
245
- ): ReferenceBadgeRect | null {
246
- "worklet";
247
- const badge = resolveReferenceBadge(line);
248
- if (!badge) return null;
249
- if (referenceLineForm(line) !== "line" || line.value === undefined) return null;
250
- if (canvasWidth === 0 || canvasHeight === 0) return null;
251
-
252
- const chartTop = padding.top;
253
- const chartBottom = canvasHeight - padding.bottom;
254
- const chartH = chartBottom - chartTop;
255
- const valRange = dMax - dMin;
256
- if (chartH <= 0 || valRange <= 0) return null;
257
-
258
- const x1 = padding.left;
259
- const x2 = canvasWidth - padding.right;
260
- const v = valueOverride ?? line.value;
261
- const edge = classifyReferenceEdge(v, dMin, dMax);
262
-
263
- let y: number;
264
- let hasChevron: boolean;
265
- if (edge !== "in") {
266
- // Off-screen: pinned to the nearest edge with a chevron.
267
- hasChevron = true;
268
- y =
269
- edge === "above"
270
- ? chartTop + OFF_AXIS_EDGE_INSET
271
- : chartBottom - OFF_AXIS_EDGE_INSET;
272
- } else {
273
- // In range: a legacy off-axis-only badge shows no pill here → not pressable.
274
- if (!badge.inRange) return null;
275
- hasChevron = false;
276
- y = chartTop + ((dMax - v) / valRange) * chartH;
277
- }
278
-
279
- const text = referenceBadgeText(line, badge, v, formatValue, edge !== "in");
280
- const g = badgeGeometry(badge.position, badge.icon, text, hasChevron, x1, x2, font);
281
- const fm = font.getMetrics();
282
- const pillH = fm.descent - fm.ascent + BADGE_PILL_PAD_Y * 2;
283
- // The pill is vertically centered on the line's y (see ReferenceLineOverlay).
284
- return { x: g.pillX, y: y - pillH / 2, w: g.pillW, h: pillH };
285
- }
286
-
287
231
  /**
288
232
  * Derives screen-space layout for a single reference line or band. Supports all
289
233
  * three scalar/band `ReferenceLine` forms (horizontal line, horizontal value
@@ -313,158 +257,184 @@ export function useReferenceLine(
313
257
  gridEndGap = 0,
314
258
  /** Font used by the Y-axis labels; badge font overrides must not move the line. */
315
259
  yAxisFont: SkFont = font,
260
+ valueFont: SkFont = font,
261
+ valueAxis?: AxisBadgeConfig,
316
262
  ): SharedValue<ReferenceLineLayout> {
263
+ const textWidthCacheRef = useRef<TextWidthCache>({});
264
+ const valueWidthCacheRef = useRef<TextWidthCache>({});
265
+ const timeBand = line != null && referenceLineForm(line) === "time-band";
266
+ return useDerivedValue(() =>
267
+ computeReferenceLineLayout(
268
+ engine.canvasWidth.value,
269
+ engine.canvasHeight.value,
270
+ padding,
271
+ line,
272
+ formatValue,
273
+ font,
274
+ engine.displayMin.value,
275
+ engine.displayMax.value,
276
+ timeBand ? engine.timestamp.value : 0,
277
+ timeBand ? engine.displayWindow.value : 1,
278
+ dragValues?.value[index],
279
+ yAxisEntries?.value,
280
+ labelRightMargin,
281
+ gridEndGap,
282
+ yAxisFont,
283
+ valueFont,
284
+ valueAxis,
285
+ textWidthCacheRef.current,
286
+ valueWidthCacheRef.current,
287
+ ),
288
+ );
289
+ }
290
+
291
+ function computeBaseReferenceLineLayout(
292
+ w: number,
293
+ h: number,
294
+ padding: ChartPadding,
295
+ line: ReferenceLine | undefined,
296
+ formatValue: (v: number) => string,
297
+ font: SkFont,
298
+ dMin: number,
299
+ dMax: number,
300
+ now: number,
301
+ win: number,
302
+ valueOverride?: number,
303
+ yAxisEntries?: YAxisEntry[],
304
+ labelRightMargin?: number,
305
+ gridEndGap = 0,
306
+ yAxisFont: SkFont = font,
307
+ textWidthCache?: TextWidthCache,
308
+ ): ReferenceLineLayout {
309
+ "worklet";
317
310
  const form = line ? referenceLineForm(line) : "none";
318
- // Badge presentation depends only on the (stable) line props — resolve once.
319
311
  const badge = line ? resolveReferenceBadge(line) : null;
320
- const textWidthCacheRef = useRef<TextWidthCache>({});
312
+ if (!line || form === "none") return INVISIBLE;
321
313
 
322
- return useDerivedValue<ReferenceLineLayout>(() => {
323
- if (!line || form === "none") return INVISIBLE;
324
-
325
- const w = engine.canvasWidth.value;
326
- const h = engine.canvasHeight.value;
327
- if (w === 0 || h === 0) return INVISIBLE;
328
-
329
- const chartTop = padding.top;
330
- const chartBottom = h - padding.bottom;
331
- const chartH = chartBottom - chartTop;
332
- const x1 = padding.left;
333
- const x2 = w - padding.right;
334
- // Full-width lines/bands run edge-to-edge through the gutters (0..canvas);
335
- // the badge/label anchor (x1/x2) stays at the plot edges either way.
336
- const fullWidth = line.fullWidth ?? false;
337
- const lineX1 = fullWidth ? 0 : x1;
338
- const yAxisColumn =
339
- form === "line" &&
340
- !fullWidth &&
341
- yAxisEntries !== undefined &&
342
- labelRightMargin !== undefined
343
- ? rightAnchoredYAxisColumnLayout(
344
- w,
345
- yAxisEntries.value,
346
- yAxisFont,
347
- labelRightMargin,
348
- gridEndGap,
349
- )
350
- : null;
351
- const lineX2 = fullWidth ? w : (yAxisColumn?.gridEndX ?? x2);
352
-
353
- const fm = font.getMetrics();
354
- const baselineOffset = (fm.ascent + fm.descent) / 2;
355
-
356
- // ── Form D — vertical time band (independent of the value range) ─────────
357
- if (form === "time-band") {
358
- if (line.from === undefined || line.to === undefined) return INVISIBLE;
359
- const now = engine.timestamp.value;
360
- const win = engine.displayWindow.value;
361
- const winStart = now - win;
362
- const chartW = x2 - x1;
363
- if (win <= 0 || chartW <= 0) return INVISIBLE;
364
-
365
- let bx1 = x1 + ((line.from - winStart) / win) * chartW;
366
- let bx2 = x1 + ((line.to - winStart) / win) * chartW;
367
- if (bx2 < bx1) {
368
- const t = bx1;
369
- bx1 = bx2;
370
- bx2 = t;
371
- }
372
- if (bx2 < x1 || bx1 > x2) return INVISIBLE;
373
- if (bx1 < x1) bx1 = x1;
374
- if (bx2 > x2) bx2 = x2;
375
-
376
- const label = line.label ?? "";
377
- const labelX =
378
- line.labelPosition === "right"
379
- ? bx2 - 4 - measureFontTextWidth(font, label)
380
- : bx1 + 4;
381
- return {
382
- ...INVISIBLE,
383
- visible: true,
384
- y: chartTop,
385
- yBottom: chartBottom,
386
- x1: bx1,
387
- x2: bx2,
388
- // Time band is vertical and time-bounded — full-width does not apply.
389
- lineX1: bx1,
390
- lineX2: bx2,
391
- label,
392
- labelX,
393
- labelY: chartTop - fm.ascent + 2,
394
- };
314
+ if (w === 0 || h === 0) return INVISIBLE;
315
+
316
+ const chartTop = padding.top;
317
+ const chartBottom = h - padding.bottom;
318
+ const chartH = chartBottom - chartTop;
319
+ const x1 = padding.left;
320
+ const x2 = w - padding.right;
321
+ // Full-width lines/bands run edge-to-edge through the gutters (0..canvas);
322
+ // the badge/label anchor (x1/x2) stays at the plot edges either way.
323
+ const fullWidth = line.fullWidth ?? false;
324
+ const lineX1 = fullWidth ? 0 : x1;
325
+ const yAxisColumn =
326
+ form === "line" &&
327
+ !fullWidth &&
328
+ yAxisEntries !== undefined &&
329
+ labelRightMargin !== undefined
330
+ ? rightAnchoredYAxisColumnLayout(
331
+ w,
332
+ yAxisEntries,
333
+ yAxisFont,
334
+ labelRightMargin,
335
+ gridEndGap,
336
+ )
337
+ : null;
338
+ const lineX2 = fullWidth ? w : (yAxisColumn?.gridEndX ?? x2);
339
+
340
+ const fm = font.getMetrics();
341
+ const baselineOffset = (fm.ascent + fm.descent) / 2;
342
+
343
+ // ── Form D — vertical time band (independent of the value range) ─────────
344
+ if (form === "time-band") {
345
+ if (line.from === undefined || line.to === undefined) return INVISIBLE;
346
+ const winStart = now - win;
347
+ const chartW = x2 - x1;
348
+ if (win <= 0 || chartW <= 0) return INVISIBLE;
349
+
350
+ let bx1 = x1 + ((line.from - winStart) / win) * chartW;
351
+ let bx2 = x1 + ((line.to - winStart) / win) * chartW;
352
+ if (bx2 < bx1) {
353
+ const t = bx1;
354
+ bx1 = bx2;
355
+ bx2 = t;
395
356
  }
357
+ if (bx2 < x1 || bx1 > x2) return INVISIBLE;
358
+ if (bx1 < x1) bx1 = x1;
359
+ if (bx2 > x2) bx2 = x2;
360
+
361
+ const label = line.label ?? "";
362
+ const labelX =
363
+ line.labelPosition === "right"
364
+ ? bx2 - 4 - measureFontTextWidth(font, label)
365
+ : bx1 + 4;
366
+ return {
367
+ ...INVISIBLE,
368
+ visible: true,
369
+ y: chartTop,
370
+ yBottom: chartBottom,
371
+ x1: bx1,
372
+ x2: bx2,
373
+ // Time band is vertical and time-bounded — full-width does not apply.
374
+ lineX1: bx1,
375
+ lineX2: bx2,
376
+ label,
377
+ labelX,
378
+ labelY: chartTop - fm.ascent + 2,
379
+ };
380
+ }
396
381
 
397
- // Forms A / B project values onto y, so they need a non-degenerate range.
398
- const dMin = engine.displayMin.value;
399
- const dMax = engine.displayMax.value;
400
- const valRange = dMax - dMin;
401
- if (valRange <= 0) return INVISIBLE;
402
- const toY = (v: number) => chartTop + ((dMax - v) / valRange) * chartH;
403
-
404
- // ── Form C — horizontal value band ───────────────────────────────────────
405
- if (form === "value-band") {
406
- if (line.valueFrom === undefined || line.valueTo === undefined) {
407
- return INVISIBLE;
408
- }
409
- let yTop = toY(line.valueTo);
410
- let yBot = toY(line.valueFrom);
411
- if (yBot < yTop) {
412
- const t = yTop;
413
- yTop = yBot;
414
- yBot = t;
415
- }
416
- if (yBot < chartTop || yTop > chartBottom) return INVISIBLE;
417
- if (yTop < chartTop) yTop = chartTop;
418
- if (yBot > chartBottom) yBot = chartBottom;
419
-
420
- const label = line.label ?? "";
421
- const labelX =
422
- line.labelPosition === "right"
423
- ? x2 - 4 - measureFontTextWidth(font, label)
424
- : x1 + 4;
425
- return {
426
- ...INVISIBLE,
427
- visible: true,
428
- y: yTop,
429
- yBottom: yBot,
430
- x1,
431
- x2,
432
- lineX1,
433
- lineX2,
434
- label,
435
- labelX,
436
- labelY: yTop - fm.ascent + 2,
437
- };
382
+ // Forms A / B project values onto y, so they need a non-degenerate range.
383
+ const valRange = dMax - dMin;
384
+ if (valRange <= 0) return INVISIBLE;
385
+ const toY = (v: number) => chartTop + ((dMax - v) / valRange) * chartH;
386
+
387
+ // ── Form C — horizontal value band ───────────────────────────────────────
388
+ if (form === "value-band") {
389
+ if (line.valueFrom === undefined || line.valueTo === undefined) {
390
+ return INVISIBLE;
438
391
  }
392
+ let yTop = toY(line.valueTo);
393
+ let yBot = toY(line.valueFrom);
394
+ if (yBot < yTop) {
395
+ const t = yTop;
396
+ yTop = yBot;
397
+ yBot = t;
398
+ }
399
+ if (yBot < chartTop || yTop > chartBottom) return INVISIBLE;
400
+ if (yTop < chartTop) yTop = chartTop;
401
+ if (yBot > chartBottom) yBot = chartBottom;
402
+
403
+ const label = line.label ?? "";
404
+ const labelX =
405
+ line.labelPosition === "right"
406
+ ? x2 - 4 - measureFontTextWidth(font, label)
407
+ : x1 + 4;
408
+ return {
409
+ ...INVISIBLE,
410
+ visible: true,
411
+ y: yTop,
412
+ yBottom: yBot,
413
+ x1,
414
+ x2,
415
+ lineX1,
416
+ lineX2,
417
+ label,
418
+ labelX,
419
+ labelY: yTop - fm.ascent + 2,
420
+ };
421
+ }
439
422
 
440
- // ── Form A — horizontal line (with optional pill badge) ──────────────────
441
- if (line.value === undefined) return INVISIBLE;
442
- // Effective value: a live drag override (if present) else the static prop.
443
- const v =
444
- dragValues && dragValues.value[index] != null
445
- ? dragValues.value[index]
446
- : line.value;
447
- const edge = classifyReferenceEdge(v, dMin, dMax);
448
-
449
- // Off-screen: pin the badge to the nearest edge with a chevron (when a badge
450
- // is configured); otherwise cull the off-screen line (legacy behavior).
451
- if (edge !== "in") {
452
- if (!badge) return INVISIBLE;
453
- const above = edge === "above";
454
- const clampedY = above
455
- ? chartTop + OFF_AXIS_EDGE_INSET
456
- : chartBottom - OFF_AXIS_EDGE_INSET;
457
- const text = referenceBadgeText(line, badge, v, formatValue, true);
458
- const g = badgeGeometry(
459
- badge.position,
460
- badge.icon,
461
- text,
462
- true,
463
- x1,
464
- x2,
465
- font,
466
- textWidthCacheRef.current,
467
- );
423
+ // ── Form A — horizontal line (with optional pill badge) ──────────────────
424
+ if (line.value === undefined || chartH <= 0) return INVISIBLE;
425
+ // Effective value: a live drag override (if present) else the static prop.
426
+ const v = valueOverride ?? line.value;
427
+ const edge = classifyReferenceEdge(v, dMin, dMax);
428
+
429
+ // Off-screen: pin the badge to the nearest edge with a chevron (when a badge
430
+ // is configured); otherwise cull the off-screen line (legacy behavior).
431
+ if (edge !== "in") {
432
+ if (!badge && !line.valueBadge) return INVISIBLE;
433
+ const above = edge === "above";
434
+ const clampedY = above
435
+ ? chartTop + OFF_AXIS_EDGE_INSET
436
+ : chartBottom - OFF_AXIS_EDGE_INSET;
437
+ if (!badge)
468
438
  return {
469
439
  ...INVISIBLE,
470
440
  visible: true,
@@ -472,72 +442,58 @@ export function useReferenceLine(
472
442
  yBottom: clampedY,
473
443
  x1,
474
444
  x2,
475
- label: text,
476
- labelX: g.textX,
477
- labelY: clampedY - baselineOffset,
478
445
  offAxis: true,
479
446
  chevronUp: above,
480
- badge: true,
481
- pillX: g.pillX,
482
- pillW: g.pillW,
483
- iconX: g.iconX,
484
- icon: badge.icon,
485
- chevronCx: g.chevronCx,
486
- connStart: g.connStart,
487
- connEnd: g.connEnd,
488
- };
489
- }
490
-
491
- const y = toY(v);
492
-
493
- // In-range pill badge (the `badge` config, not the legacy off-axis-only flag).
494
- if (badge && badge.inRange) {
495
- const text = referenceBadgeText(line, badge, v, formatValue, false);
496
- const g = badgeGeometry(
497
- badge.position,
498
- badge.icon,
499
- text,
500
- false,
501
- x1,
502
- x2,
503
- font,
504
- textWidthCacheRef.current,
505
- );
506
- return {
507
- ...INVISIBLE,
508
- visible: true,
509
- y,
510
- yBottom: y,
511
- x1,
512
- x2,
513
- lineX1,
514
- lineX2,
515
- // Full-width: the edge-to-edge line replaces the dashed connector.
516
- drawLine: fullWidth,
517
- label: text,
518
- labelX: g.textX,
519
- labelY: y - baselineOffset,
520
- badge: true,
521
- pillX: g.pillX,
522
- pillW: g.pillW,
523
- iconX: g.iconX,
524
- icon: badge.icon,
525
- connStart: fullWidth ? -1 : g.connStart,
526
- connEnd: fullWidth ? -1 : g.connEnd,
527
447
  };
528
- }
529
-
530
- // Plain gutter label (no badge, or a legacy off-axis badge that's in range).
531
- let label = line.label ?? formatValue(v);
532
- if (line.showValue && line.label) label = `${line.label} ${formatValue(v)}`;
448
+ const text = referenceBadgeText(line, badge, v, formatValue, true);
449
+ const g = badgeGeometry(
450
+ badge.position,
451
+ badge.icon,
452
+ text,
453
+ true,
454
+ x1,
455
+ x2,
456
+ font,
457
+ textWidthCache,
458
+ );
459
+ return {
460
+ ...INVISIBLE,
461
+ visible: true,
462
+ y: clampedY,
463
+ yBottom: clampedY,
464
+ x1,
465
+ x2,
466
+ label: text,
467
+ labelX: g.textX,
468
+ labelY: clampedY - baselineOffset,
469
+ offAxis: true,
470
+ chevronUp: above,
471
+ badge: true,
472
+ pillX: g.pillX,
473
+ pillW: g.pillW,
474
+ iconX: g.iconX,
475
+ icon: badge.icon,
476
+ chevronCx: g.chevronCx,
477
+ connStart: g.connStart,
478
+ connEnd: g.connEnd,
479
+ };
480
+ }
533
481
 
534
- const pos = line.labelPosition ?? "right";
535
- let labelX: number;
536
- if (pos === "left") labelX = x1 + 4;
537
- else if (pos === "center")
538
- labelX = (x1 + x2) / 2 - measureFontTextWidth(font, label) / 2;
539
- else labelX = x2 + 4; // "right" — legacy gutter position
482
+ const y = toY(v);
540
483
 
484
+ // In-range pill badge (the `badge` config, not the legacy off-axis-only flag).
485
+ if (badge && badge.inRange) {
486
+ const text = referenceBadgeText(line, badge, v, formatValue, false);
487
+ const g = badgeGeometry(
488
+ badge.position,
489
+ badge.icon,
490
+ text,
491
+ false,
492
+ x1,
493
+ x2,
494
+ font,
495
+ textWidthCache,
496
+ );
541
497
  return {
542
498
  ...INVISIBLE,
543
499
  visible: true,
@@ -547,10 +503,230 @@ export function useReferenceLine(
547
503
  x2,
548
504
  lineX1,
549
505
  lineX2,
550
- drawLine: true,
551
- label,
552
- labelX,
506
+ // Full-width: the edge-to-edge line replaces the dashed connector.
507
+ drawLine: fullWidth,
508
+ label: text,
509
+ labelX: g.textX,
553
510
  labelY: y - baselineOffset,
511
+ badge: true,
512
+ pillX: g.pillX,
513
+ pillW: g.pillW,
514
+ iconX: g.iconX,
515
+ icon: badge.icon,
516
+ connStart: fullWidth ? -1 : g.connStart,
517
+ connEnd: fullWidth ? -1 : g.connEnd,
554
518
  };
555
- });
519
+ }
520
+
521
+ // Plain gutter label (no badge, or a legacy off-axis badge that's in range).
522
+ let label = line.label ?? (line.valueBadge ? "" : formatValue(v));
523
+ if (line.showValue && line.label) label = `${line.label} ${formatValue(v)}`;
524
+
525
+ const pos = line.labelPosition ?? (line.valueBadge ? "left" : "right");
526
+ let labelX: number;
527
+ if (pos === "left") labelX = x1 + 4;
528
+ else if (pos === "center")
529
+ labelX = (x1 + x2) / 2 - measureFontTextWidth(font, label) / 2;
530
+ else labelX = x2 + 4; // "right" — legacy gutter position
531
+
532
+ return {
533
+ ...INVISIBLE,
534
+ visible: true,
535
+ y,
536
+ yBottom: y,
537
+ x1,
538
+ x2,
539
+ lineX1,
540
+ lineX2,
541
+ drawLine: true,
542
+ label,
543
+ labelX,
544
+ labelY: y - baselineOffset,
545
+ };
546
+ }
547
+
548
+ /** Pure worklet shared by the drawing and tap-target calculations. */
549
+ export function computeReferenceLineLayout(
550
+ w: number,
551
+ h: number,
552
+ padding: ChartPadding,
553
+ line: ReferenceLine | undefined,
554
+ formatValue: (v: number) => string,
555
+ font: SkFont,
556
+ dMin: number,
557
+ dMax: number,
558
+ now: number,
559
+ win: number,
560
+ valueOverride?: number,
561
+ yAxisEntries?: YAxisEntry[],
562
+ labelRightMargin?: number,
563
+ gridEndGap = 0,
564
+ yAxisFont: SkFont = font,
565
+ valueFont: SkFont = font,
566
+ valueAxis?: AxisBadgeConfig,
567
+ textWidthCache?: TextWidthCache,
568
+ valueWidthCache?: TextWidthCache,
569
+ ): ReferenceLineLayout {
570
+ "worklet";
571
+ const base = computeBaseReferenceLineLayout(
572
+ w,
573
+ h,
574
+ padding,
575
+ line,
576
+ formatValue,
577
+ font,
578
+ dMin,
579
+ dMax,
580
+ now,
581
+ win,
582
+ valueOverride,
583
+ yAxisEntries,
584
+ labelRightMargin,
585
+ gridEndGap,
586
+ yAxisFont,
587
+ textWidthCache,
588
+ );
589
+ if (!line?.valueBadge || referenceLineForm(line) !== "line" || !base.visible)
590
+ return base;
591
+ const cfg = typeof line.valueBadge === "object" ? line.valueBadge : {};
592
+ const text = formatValue(valueOverride ?? line.value!);
593
+ const textW = measureFontTextWidth(valueFont, text, valueWidthCache);
594
+ const fm = valueFont.getMetrics();
595
+ const height = fm.descent - fm.ascent + BADGE_PILL_PAD_Y * 2;
596
+ const chevronW = base.offAxis && !base.badge ? BADGE_CHEV_W + BADGE_GAP : 0;
597
+ const contentW = textW + chevronW;
598
+ const bounds =
599
+ cfg.position === "axis"
600
+ ? axisBadgeBounds(
601
+ w,
602
+ padding.right,
603
+ contentW,
604
+ valueFont.getSize(),
605
+ valueAxis,
606
+ )
607
+ : {
608
+ left: base.x2 - BADGE_EDGE_INSET - contentW - BADGE_PAD_X * 2,
609
+ right: base.x2 - BADGE_EDGE_INSET,
610
+ };
611
+ // An explicitly undersized inset must not produce an inverted pill.
612
+ const pillW = Math.max(0, bounds.right - bounds.left);
613
+ const x = bounds.left + (cfg.offsetX ?? 0);
614
+ const y = base.y + (cfg.offsetY ?? 0);
615
+ let textX = x + (pillW - contentW) / 2 + chevronW;
616
+ // Match live-badge text alignment when its column mode is enabled.
617
+ if (
618
+ cfg.position === "axis" &&
619
+ valueAxis?.alignTextWithYAxis &&
620
+ !valueAxis.float &&
621
+ labelRightMargin !== undefined &&
622
+ yAxisEntries
623
+ ) {
624
+ const column = rightAnchoredYAxisColumnLayout(
625
+ w,
626
+ yAxisEntries,
627
+ yAxisFont,
628
+ labelRightMargin,
629
+ );
630
+ const pad = valueAxis?.metrics?.padX ?? BADGE_PAD_X;
631
+ const maxX = x + pillW - pad - textW;
632
+ if (x + pad + chevronW <= maxX) {
633
+ textX = Math.min(
634
+ Math.max(
635
+ Math.min(column.labelX, w - labelRightMargin - textW),
636
+ x + pad + chevronW,
637
+ ),
638
+ maxX,
639
+ );
640
+ }
641
+ }
642
+ const result = {
643
+ ...base,
644
+ drawLine: Boolean(line.fullWidth && !base.offAxis),
645
+ valueBadge: {
646
+ x,
647
+ y: y - height / 2,
648
+ w: pillW,
649
+ h: height,
650
+ text,
651
+ textX,
652
+ textY: y - (fm.ascent + fm.descent) / 2,
653
+ chevronCx: chevronW ? x + BADGE_PAD_X + BADGE_CHEV_W / 2 : -1,
654
+ },
655
+ };
656
+ const badge = resolveReferenceBadge(line);
657
+ const offsetX = badge?.offsetX ?? 0;
658
+ // Right/center name pills keep their requested anchor until they would overlap
659
+ // the value pill; then make room immediately to its left.
660
+ if (base.badge) {
661
+ const shift = Math.min(0, x - 8 - (base.pillX + base.pillW + offsetX));
662
+ result.pillX += shift;
663
+ result.labelX += shift;
664
+ if (result.iconX >= 0) result.iconX += shift;
665
+ if (result.chevronCx >= 0) result.chevronCx += shift;
666
+ }
667
+ // The name badge's transform is applied by the existing connector group.
668
+ result.connStart = base.badge
669
+ ? result.pillX + result.pillW + 4
670
+ : base.label
671
+ ? base.labelX + measureFontTextWidth(font, base.label, textWidthCache) + 4 - offsetX
672
+ : base.x1 - offsetX;
673
+ result.connEnd = x - 4 - offsetX;
674
+ if (line.fullWidth && !base.offAxis) result.connStart = -1;
675
+ if (result.connEnd <= result.connStart) result.connStart = -1;
676
+ return result;
677
+ }
678
+
679
+ export function referenceBadgeRect(
680
+ layout: ReferenceLineLayout,
681
+ font: SkFont,
682
+ line: ReferenceLine,
683
+ ): ReferenceBadgeRect | null {
684
+ "worklet";
685
+ if (!layout.visible || !layout.badge) return null;
686
+ const fm = font.getMetrics();
687
+ const h = fm.descent - fm.ascent + BADGE_PILL_PAD_Y * 2;
688
+ const badge = resolveReferenceBadge(line);
689
+ return {
690
+ x: layout.pillX + (badge?.offsetX ?? 0),
691
+ y: layout.y - h / 2 + (badge?.offsetY ?? 0),
692
+ w: layout.pillW,
693
+ h,
694
+ };
695
+ }
696
+
697
+ /**
698
+ * Screen-space pill rect for a Form-A reference line's **badge**, or `null` when
699
+ * the line has no pressable badge (no `badge`/`offAxisBadge`, not a value line, a
700
+ * legacy off-axis badge while in range, or the canvas isn't laid out / range is
701
+ * degenerate). Shares `badgeGeometry` + the edge classification with
702
+ * {@link useReferenceLine}, so the hit-target tracks the rendered pill exactly.
703
+ * Pure worklet — used by {@link useReferenceLinePress} for tap hit-testing.
704
+ */
705
+ export function computeReferenceBadgeRect(
706
+ line: ReferenceLine,
707
+ canvasWidth: number,
708
+ canvasHeight: number,
709
+ padding: ChartPadding,
710
+ dMin: number,
711
+ dMax: number,
712
+ font: SkFont,
713
+ formatValue: (value: number) => string,
714
+ /** Live value override (e.g. a dragged value), defaulting to `line.value`. */
715
+ valueOverride?: number,
716
+ ): ReferenceBadgeRect | null {
717
+ "worklet";
718
+ const layout = computeReferenceLineLayout(
719
+ canvasWidth,
720
+ canvasHeight,
721
+ padding,
722
+ line,
723
+ formatValue,
724
+ font,
725
+ dMin,
726
+ dMax,
727
+ 0,
728
+ 1,
729
+ valueOverride,
730
+ );
731
+ return referenceBadgeRect(layout, font, line);
556
732
  }