@opendata-ai/openchart-vanilla 8.0.0-rc.8 → 8.0.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 (147) hide show
  1. package/dist/{chunk-W34KN7FD.js → chunk-2NAQRP66.js} +2 -2
  2. package/dist/{chunk-UZ7J3NOF.js → chunk-6BUCYCLD.js} +7314 -5605
  3. package/dist/chunk-6BUCYCLD.js.map +1 -0
  4. package/dist/chunk-DCODCLWI.js +234 -0
  5. package/dist/chunk-DCODCLWI.js.map +1 -0
  6. package/dist/{chunk-5EOV5JQZ.js → chunk-EKAUQAAX.js} +50 -46
  7. package/dist/chunk-EKAUQAAX.js.map +1 -0
  8. package/dist/chunk-RM5XS6NR.js +113 -0
  9. package/dist/chunk-RM5XS6NR.js.map +1 -0
  10. package/dist/chunk-T32UHMZA.js +282 -0
  11. package/dist/chunk-T32UHMZA.js.map +1 -0
  12. package/dist/export-gif.d.ts +113 -0
  13. package/dist/export-gif.js +21 -0
  14. package/dist/export-gif.js.map +1 -0
  15. package/dist/index.d.ts +309 -27
  16. package/dist/index.js +2020 -836
  17. package/dist/index.js.map +1 -1
  18. package/dist/{mount-CUKQLeqD.d.ts → mount-CL-nA0f-.d.ts} +85 -2
  19. package/dist/simulation-worker.js +83 -2
  20. package/dist/static.d.ts +2 -0
  21. package/dist/static.js +8 -69
  22. package/dist/static.js.map +1 -1
  23. package/dist/story/index.d.ts +25 -9
  24. package/dist/story/index.js +9 -7
  25. package/dist/story/index.js.map +1 -1
  26. package/dist/styles.css +1 -1
  27. package/dist/styles.css.map +1 -1
  28. package/package.json +14 -6
  29. package/dist/chunk-5EOV5JQZ.js.map +0 -1
  30. package/dist/chunk-UZ7J3NOF.js.map +0 -1
  31. package/src/__test-fixtures__/dom.ts +0 -70
  32. package/src/__test-fixtures__/specs.ts +0 -214
  33. package/src/__tests__/animation.test.ts +0 -358
  34. package/src/__tests__/annotations-render.test.ts +0 -201
  35. package/src/__tests__/compound-labels.test.ts +0 -122
  36. package/src/__tests__/continuous-legend.test.ts +0 -91
  37. package/src/__tests__/crosshair.test.ts +0 -130
  38. package/src/__tests__/edit-events.test.ts +0 -950
  39. package/src/__tests__/events.test.ts +0 -387
  40. package/src/__tests__/export.test.ts +0 -241
  41. package/src/__tests__/font-race.test.ts +0 -199
  42. package/src/__tests__/gradient-ids.test.ts +0 -159
  43. package/src/__tests__/gradient-utils.test.ts +0 -125
  44. package/src/__tests__/known-bugs-render.test.ts +0 -183
  45. package/src/__tests__/map.test.ts +0 -636
  46. package/src/__tests__/mount.test.ts +0 -357
  47. package/src/__tests__/pattern-fills.test.ts +0 -105
  48. package/src/__tests__/resize-timing.test.ts +0 -218
  49. package/src/__tests__/sankey.test.ts +0 -133
  50. package/src/__tests__/selection-events.test.ts +0 -869
  51. package/src/__tests__/series-search.test.ts +0 -311
  52. package/src/__tests__/static.test.ts +0 -141
  53. package/src/__tests__/svg-renderer.test.ts +0 -895
  54. package/src/__tests__/table-keyboard.test.ts +0 -361
  55. package/src/__tests__/table-mount.test.ts +0 -639
  56. package/src/__tests__/tilemap.test.ts +0 -158
  57. package/src/__tests__/tooltip.test.ts +0 -337
  58. package/src/__tests__/transition.test.ts +0 -1792
  59. package/src/__tests__/update-scrolly-matrix.test.ts +0 -563
  60. package/src/__tests__/you-draw-it.test.ts +0 -273
  61. package/src/animation-vars.ts +0 -42
  62. package/src/animation.ts +0 -76
  63. package/src/barlist-mount.ts +0 -383
  64. package/src/barlist-renderer.ts +0 -267
  65. package/src/dom-helpers.ts +0 -17
  66. package/src/export.ts +0 -482
  67. package/src/gradient-utils.ts +0 -138
  68. package/src/graph/__tests__/canvas-renderer.test.ts +0 -713
  69. package/src/graph/__tests__/graph-mount.test.ts +0 -213
  70. package/src/graph/__tests__/interaction.test.ts +0 -205
  71. package/src/graph/__tests__/keyboard.test.ts +0 -653
  72. package/src/graph/__tests__/search.test.ts +0 -88
  73. package/src/graph/__tests__/simulation.test.ts +0 -233
  74. package/src/graph/__tests__/spatial-index.test.ts +0 -142
  75. package/src/graph/__tests__/zoom.test.ts +0 -203
  76. package/src/graph/canvas-renderer.ts +0 -747
  77. package/src/graph/interaction.ts +0 -367
  78. package/src/graph/keyboard.ts +0 -208
  79. package/src/graph/search.ts +0 -50
  80. package/src/graph/simulation-worker-url.ts +0 -12
  81. package/src/graph/simulation-worker.ts +0 -278
  82. package/src/graph/simulation.ts +0 -422
  83. package/src/graph/spatial-index.ts +0 -121
  84. package/src/graph/types.ts +0 -47
  85. package/src/graph/worker-protocol.ts +0 -70
  86. package/src/graph/zoom.ts +0 -134
  87. package/src/graph-mount.ts +0 -888
  88. package/src/index.ts +0 -76
  89. package/src/interactions/chart-events.ts +0 -139
  90. package/src/interactions/crosshair.ts +0 -233
  91. package/src/interactions/drag-handler.ts +0 -175
  92. package/src/interactions/editing-drags.ts +0 -515
  93. package/src/interactions/index.ts +0 -25
  94. package/src/interactions/keyboard-nav.ts +0 -111
  95. package/src/interactions/legend-interaction.ts +0 -46
  96. package/src/interactions/selection.ts +0 -280
  97. package/src/interactions/tooltip-events.ts +0 -72
  98. package/src/map-camera.ts +0 -111
  99. package/src/map-mount.ts +0 -792
  100. package/src/map-renderer.ts +0 -460
  101. package/src/map-transition.ts +0 -99
  102. package/src/measure-text.ts +0 -134
  103. package/src/mount.ts +0 -1382
  104. package/src/pattern-utils.ts +0 -110
  105. package/src/renderers/annotations.ts +0 -388
  106. package/src/renderers/axes.ts +0 -351
  107. package/src/renderers/brand.ts +0 -87
  108. package/src/renderers/chrome.ts +0 -196
  109. package/src/renderers/endpoint-labels.ts +0 -150
  110. package/src/renderers/legend.ts +0 -311
  111. package/src/renderers/marks.ts +0 -561
  112. package/src/renderers/metrics.ts +0 -63
  113. package/src/renderers/svg-dom.ts +0 -40
  114. package/src/renderers/table-cells.ts +0 -446
  115. package/src/resize-observer.ts +0 -46
  116. package/src/sankey-mount.ts +0 -622
  117. package/src/sankey-renderer.ts +0 -614
  118. package/src/series-search.ts +0 -392
  119. package/src/static.ts +0 -251
  120. package/src/story/__tests__/camera-math.test.ts +0 -145
  121. package/src/story/__tests__/create-chart-story.test.ts +0 -317
  122. package/src/story/__tests__/progress-math.test.ts +0 -95
  123. package/src/story/__tests__/scroll-driver.test.ts +0 -130
  124. package/src/story/__tests__/tween.test.ts +0 -159
  125. package/src/story/camera-math.ts +0 -125
  126. package/src/story/create-chart-story.ts +0 -353
  127. package/src/story/crossfade.ts +0 -70
  128. package/src/story/index.ts +0 -39
  129. package/src/story/progress-math.ts +0 -85
  130. package/src/story/resolve-camera-target.ts +0 -119
  131. package/src/story/scroll-driver.ts +0 -206
  132. package/src/story/story-camera.ts +0 -49
  133. package/src/story/tween.ts +0 -112
  134. package/src/story/types.ts +0 -76
  135. package/src/svg-ids.ts +0 -23
  136. package/src/svg-renderer.ts +0 -383
  137. package/src/table-keyboard.ts +0 -266
  138. package/src/table-mount.ts +0 -567
  139. package/src/table-renderer.ts +0 -414
  140. package/src/text-edit-overlay.ts +0 -255
  141. package/src/theme-tokens.ts +0 -51
  142. package/src/tilemap-mount.ts +0 -511
  143. package/src/tilemap-renderer.ts +0 -396
  144. package/src/tooltip.ts +0 -177
  145. package/src/transition.ts +0 -2816
  146. package/src/you-draw-it.ts +0 -480
  147. /package/dist/{chunk-W34KN7FD.js.map → chunk-2NAQRP66.js.map} +0 -0
@@ -1,110 +0,0 @@
1
- /**
2
- * SVG pattern utilities: creates <pattern> elements from ResolvedFillPattern
3
- * specs (engine output for `mark.fillPattern: 'auto'`) and resolves mark
4
- * fills to url(#id) refs. Mirrors the gradient defs lifecycle in
5
- * gradient-utils.ts, including nextSvgId-based deterministic IDs.
6
- */
7
-
8
- import type { ResolvedFillPattern } from '@opendata-ai/openchart-core';
9
- import { nextSvgId } from './svg-ids';
10
-
11
- const SVG_NS = 'http://www.w3.org/2000/svg';
12
-
13
- /** Pattern tile size in px (userSpaceOnUse, so zoom-stable within the SVG). */
14
- const TILE = 6;
15
-
16
- /**
17
- * Opacity of the pattern geometry over the series base color. Full-contrast
18
- * lines read as stripes and fight the data; a settled overlay keeps the
19
- * series color dominant while the texture stays clearly legible.
20
- */
21
- const LINE_OPACITY = 0.55;
22
-
23
- /** Stable dedup key: one SVG element per unique (type, base, line) combo. */
24
- function patternKey(p: ResolvedFillPattern): string {
25
- return `${p.type}|${p.base}|${p.line}`;
26
- }
27
-
28
- /** Create one <pattern> tile: base-color rect + line-colored geometry. */
29
- function createPatternElement(p: ResolvedFillPattern, id: string): SVGElement {
30
- const el = document.createElementNS(SVG_NS, 'pattern');
31
- el.setAttribute('id', id);
32
- el.setAttribute('patternUnits', 'userSpaceOnUse');
33
- el.setAttribute('width', String(TILE));
34
- el.setAttribute('height', String(TILE));
35
-
36
- const bg = document.createElementNS(SVG_NS, 'rect');
37
- bg.setAttribute('width', String(TILE));
38
- bg.setAttribute('height', String(TILE));
39
- bg.setAttribute('fill', p.base);
40
- el.appendChild(bg);
41
-
42
- if (p.type === 'dot') {
43
- const dot = document.createElementNS(SVG_NS, 'circle');
44
- dot.setAttribute('cx', String(TILE / 2));
45
- dot.setAttribute('cy', String(TILE / 2));
46
- dot.setAttribute('r', '1.2');
47
- dot.setAttribute('fill', p.line);
48
- dot.setAttribute('fill-opacity', String(LINE_OPACITY));
49
- el.appendChild(dot);
50
- return el;
51
- }
52
-
53
- // Stroked shapes. Diagonal paths include half-tile companions so the
54
- // hatch stays continuous across tile seams.
55
- const d =
56
- p.type === 'diagonal'
57
- ? `M0,${TILE} l${TILE},-${TILE} M-1.5,1.5 l3,-3 M${TILE - 1.5},${TILE + 1.5} l3,-3`
58
- : p.type === 'crosshatch'
59
- ? `M0,0 l${TILE},${TILE} M${TILE},0 l-${TILE},${TILE}`
60
- : `M${TILE / 2},0 V${TILE}`;
61
-
62
- const path = document.createElementNS(SVG_NS, 'path');
63
- path.setAttribute('d', d);
64
- path.setAttribute('stroke', p.line);
65
- path.setAttribute('stroke-width', '1');
66
- path.setAttribute('stroke-opacity', String(LINE_OPACITY));
67
- el.appendChild(path);
68
- return el;
69
- }
70
-
71
- /**
72
- * Scan marks for resolved fill patterns, create <pattern> elements in the
73
- * provided <defs> node, and return a map from pattern key to element ID.
74
- *
75
- * Identical patterns share one SVG element within a chart. IDs come from
76
- * `nextSvgId` (shared counter with gradients and clip-paths) so they are
77
- * deterministic at generation time and globally unique across charts.
78
- */
79
- export function buildPatternDefs(
80
- marks: Array<{ pattern?: ResolvedFillPattern }>,
81
- defs: SVGElement,
82
- ): Map<string, string> {
83
- const map = new Map<string, string>();
84
-
85
- for (const mark of marks) {
86
- const pattern = mark.pattern;
87
- if (!pattern) continue;
88
- const key = patternKey(pattern);
89
- if (!map.has(key)) {
90
- const id = nextSvgId('oc-pattern');
91
- defs.appendChild(createPatternElement(pattern, id));
92
- map.set(key, id);
93
- }
94
- }
95
-
96
- return map;
97
- }
98
-
99
- /**
100
- * Resolve a mark's pattern to an SVG fill string ("url(#id)"), falling back
101
- * to the pattern's base color if the def is missing (defensive; buildPatternDefs
102
- * runs over the same mark list first).
103
- */
104
- export function resolvePatternFill(
105
- pattern: ResolvedFillPattern,
106
- patternMap: Map<string, string>,
107
- ): string {
108
- const id = patternMap.get(patternKey(pattern));
109
- return id ? `url(#${id})` : pattern.base;
110
- }
@@ -1,388 +0,0 @@
1
- /**
2
- * Annotation rendering: range rects, reference lines, labels with connectors.
3
- */
4
-
5
- import type { ChartLayout, Point, ResolvedAnnotation } from '@opendata-ai/openchart-core';
6
- import {
7
- ARROWHEAD_LENGTH,
8
- BOLD_SPAN_FONT_WEIGHT,
9
- computeArrowheadPoints,
10
- DEFAULT_ANNOTATION_FONT_SIZE,
11
- DEFAULT_LINE_HEIGHT,
12
- parseAnnotationSpans,
13
- } from '@opendata-ai/openchart-engine';
14
- import { applyTextStyle, createSVGElement, setAttrs } from './svg-dom';
15
-
16
- /** Stroke width shared by every connector voice except the drop-line hairline. */
17
- const CONNECTOR_STROKE_WIDTH = 1.25;
18
-
19
- /**
20
- * Render an open-V arrowhead at the given tip with tangent direction. A stroked
21
- * polyline, not a filled triangle — the hand-annotated look.
22
- */
23
- function renderArrowhead(
24
- parent: SVGElement,
25
- tipX: number,
26
- tipY: number,
27
- tangentX: number,
28
- tangentY: number,
29
- stroke: string,
30
- ): void {
31
- const head = computeArrowheadPoints(tipX, tipY, tangentX, tangentY);
32
- const arrow = createSVGElement('polyline');
33
- // `oc-annotation-arrowhead` is what edit mode selects on to find the tip. Both
34
- // the straight and the curved connector draw their head through here, and both
35
- // keep the shared `oc-annotation-connector` class for styling -- without the
36
- // dedicated class the drag code has to select `polyline.oc-annotation-connector`
37
- // and rely on no other polyline existing in the group.
38
- arrow.setAttribute('class', 'oc-annotation-connector oc-annotation-arrowhead');
39
- setAttrs(arrow, {
40
- points: [
41
- `${head.baseLeft.x},${head.baseLeft.y}`,
42
- `${head.tip.x},${head.tip.y}`,
43
- `${head.baseRight.x},${head.baseRight.y}`,
44
- ].join(' '),
45
- fill: 'none',
46
- stroke,
47
- 'stroke-width': CONNECTOR_STROKE_WIDTH,
48
- 'stroke-linecap': 'round',
49
- 'stroke-linejoin': 'round',
50
- });
51
- parent.appendChild(arrow);
52
- }
53
-
54
- /**
55
- * Render a curved connector from a label to a data point: a single quadratic
56
- * with one decisive bend, no S-swoop. The control point leans along the axis the
57
- * connector left the text block on, so a vertical exit sweeps out and down while
58
- * a horizontal exit runs flat then dives.
59
- */
60
- function renderCurvedConnector(
61
- parent: SVGElement,
62
- from: Point,
63
- to: Point,
64
- stroke: string,
65
- showArrow: boolean,
66
- exit: 'horizontal' | 'vertical' | undefined,
67
- ): void {
68
- const dx = to.x - from.x;
69
- const dy = to.y - from.y;
70
-
71
- const vertical = exit ? exit === 'vertical' : Math.abs(dy) >= Math.abs(dx);
72
- const cpx = from.x + dx * (vertical ? 0.2 : 0.6);
73
- const cpy = from.y + dy * (vertical ? 0.6 : 0.2);
74
-
75
- // Tangent at the tip runs from the control point to the tip.
76
- const tx = to.x - cpx;
77
- const ty = to.y - cpy;
78
- const tLen = Math.sqrt(tx * tx + ty * ty) || 1;
79
-
80
- // When arrowed, stop the stroke at the arrowhead base so it doesn't poke
81
- // through the open V.
82
- const endX = showArrow ? to.x - (tx / tLen) * ARROWHEAD_LENGTH : to.x;
83
- const endY = showArrow ? to.y - (ty / tLen) * ARROWHEAD_LENGTH : to.y;
84
-
85
- const path = createSVGElement('path');
86
- path.setAttribute('class', 'oc-annotation-connector');
87
- setAttrs(path, {
88
- d: `M ${from.x} ${from.y} Q ${cpx} ${cpy} ${endX} ${endY}`,
89
- fill: 'none',
90
- stroke,
91
- 'stroke-width': CONNECTOR_STROKE_WIDTH,
92
- 'stroke-linecap': 'round',
93
- });
94
- parent.appendChild(path);
95
-
96
- if (showArrow) {
97
- renderArrowhead(parent, to.x, to.y, tx, ty, stroke);
98
- }
99
- }
100
-
101
- /**
102
- * Fill a `<text>` with annotation copy: one `<tspan>` per `**bold**` span, lines
103
- * split on `\n`. The first span of each line carries the `x` reset and the `dy`
104
- * line advance, so multi-line and inline-bold compose. The engine measures these
105
- * same spans at these same weights, so bounds match what lands on screen.
106
- */
107
- function fillRichText(text: SVGElement, content: string, x: number, lineHeight: number): void {
108
- const lines = content.split('\n');
109
- for (let i = 0; i < lines.length; i++) {
110
- const spans = parseAnnotationSpans(lines[i]);
111
- for (let s = 0; s < spans.length; s++) {
112
- const tspan = createSVGElement('tspan');
113
- if (s === 0) {
114
- setAttrs(tspan, { x, dy: i === 0 ? 0 : lineHeight });
115
- }
116
- if (spans[s].bold) {
117
- tspan.setAttribute('font-weight', String(BOLD_SPAN_FONT_WEIGHT));
118
- }
119
- tspan.textContent = spans[s].text;
120
- text.appendChild(tspan);
121
- }
122
- }
123
- }
124
-
125
- function renderAnnotation(
126
- parent: SVGElement,
127
- annotation: ResolvedAnnotation,
128
- index: number,
129
- bgColor?: string,
130
- ): void {
131
- const g = createSVGElement('g');
132
- g.setAttribute('class', `oc-annotation oc-annotation-${annotation.type}`);
133
- // Edit mode reads this back as `spec.annotations[index]`, so it has to be the
134
- // SPEC index, not the render position. `layout.annotations` is both filtered
135
- // (an annotation outside the domain resolves to nothing and is dropped) and
136
- // re-sorted by `zIndex`, so the two diverge: with a zIndex set, position 0 can
137
- // be spec annotation 1, and dragging one label moves the other. Fall back to the
138
- // position for a hand-built layout that carries no `specIndex`.
139
- g.setAttribute('data-annotation-index', String(annotation.specIndex ?? index));
140
- if (annotation.id) {
141
- g.setAttribute('data-annotation-id', annotation.id);
142
- }
143
-
144
- // Range rect
145
- if (annotation.rect) {
146
- const rect = createSVGElement('rect');
147
- rect.setAttribute('class', 'oc-annotation-range');
148
- // Range fills cover large chart-area regions; if they intercept pointer
149
- // events the voronoi tooltip overlay below stops receiving mousemove
150
- // inside the range, creating hover dead zones. The label still receives
151
- // events for annotation click handlers.
152
- rect.setAttribute('pointer-events', 'none');
153
- setAttrs(rect, {
154
- x: annotation.rect.x,
155
- y: annotation.rect.y,
156
- width: annotation.rect.width,
157
- height: annotation.rect.height,
158
- });
159
- if (annotation.fill) rect.setAttribute('fill', annotation.fill);
160
- if (annotation.opacity !== undefined) {
161
- rect.setAttribute('fill-opacity', String(annotation.opacity));
162
- }
163
- g.appendChild(rect);
164
- }
165
-
166
- // Reference line
167
- if (annotation.line) {
168
- const line = createSVGElement('line');
169
- line.setAttribute('class', 'oc-annotation-line');
170
- setAttrs(line, {
171
- x1: annotation.line.start.x,
172
- y1: annotation.line.start.y,
173
- x2: annotation.line.end.x,
174
- y2: annotation.line.end.y,
175
- 'stroke-width': annotation.strokeWidth ?? 1,
176
- });
177
- if (annotation.stroke) line.setAttribute('stroke', annotation.stroke);
178
- if (annotation.strokeDasharray) {
179
- line.setAttribute('stroke-dasharray', annotation.strokeDasharray);
180
- }
181
- g.appendChild(line);
182
- }
183
-
184
- // Footnote marker: annotation was demoted by auto-thinning. Render a numbered
185
- // dot at the data point instead of the full label text.
186
- if (annotation.footnoteIndex != null && annotation.label) {
187
- // Prefer the data point. A suppressed connector (too short to be worth
188
- // drawing) still leaves a resolved dot there, so fall through to it before
189
- // giving up on the label position.
190
- const cx =
191
- annotation.label.connector?.endpoint?.x ??
192
- annotation.label.connector?.to.x ??
193
- annotation.dot?.x ??
194
- annotation.label.x;
195
- const cy =
196
- annotation.label.connector?.endpoint?.y ??
197
- annotation.label.connector?.to.y ??
198
- annotation.dot?.y ??
199
- annotation.label.y;
200
- const r = 8;
201
- const circle = createSVGElement('circle');
202
- circle.setAttribute('class', 'oc-annotation-footnote-marker');
203
- setAttrs(circle, {
204
- cx,
205
- cy,
206
- r,
207
- fill: bgColor ?? '#ffffff',
208
- stroke: annotation.label.style.fill ?? '#666',
209
- 'stroke-width': 1.5,
210
- });
211
- g.appendChild(circle);
212
-
213
- const num = createSVGElement('text');
214
- num.setAttribute('class', 'oc-annotation-footnote-number');
215
- setAttrs(num, {
216
- x: cx,
217
- y: cy,
218
- 'dominant-baseline': 'central',
219
- 'text-anchor': 'middle',
220
- });
221
- applyTextStyle(num, {
222
- ...annotation.label.style,
223
- fontSize: 9,
224
- fontWeight: 600,
225
- });
226
- num.textContent = String(annotation.footnoteIndex);
227
- g.appendChild(num);
228
- parent.appendChild(g);
229
- return;
230
- }
231
-
232
- // Label with optional connector line
233
- if (annotation.label?.visible) {
234
- // Render connector first (behind the label text)
235
- if (annotation.label.connector) {
236
- const c = annotation.label.connector;
237
- if (c.style === 'curve') {
238
- renderCurvedConnector(g, c.from, c.to, c.stroke, c.arrow, c.exit);
239
- } else if (c.style === 'drop-line') {
240
- // Quiet voice: hairline, slightly recessed, snapped to the pixel grid.
241
- const connector = createSVGElement('line');
242
- connector.setAttribute('class', 'oc-annotation-connector oc-annotation-drop-line');
243
- setAttrs(connector, {
244
- x1: c.from.x,
245
- y1: c.from.y,
246
- x2: c.to.x,
247
- y2: c.to.y,
248
- stroke: c.stroke,
249
- 'stroke-width': 1,
250
- 'stroke-opacity': 0.7,
251
- 'shape-rendering': 'crispEdges',
252
- });
253
- g.appendChild(connector);
254
- } else if (c.arrow) {
255
- const dx = c.to.x - c.from.x;
256
- const dy = c.to.y - c.from.y;
257
- const dist = Math.sqrt(dx * dx + dy * dy) || 1;
258
- const ux = dx / dist;
259
- const uy = dy / dist;
260
- const lineEndX = c.to.x - ux * ARROWHEAD_LENGTH;
261
- const lineEndY = c.to.y - uy * ARROWHEAD_LENGTH;
262
-
263
- const connector = createSVGElement('line');
264
- connector.setAttribute('class', 'oc-annotation-connector');
265
- setAttrs(connector, {
266
- x1: c.from.x,
267
- y1: c.from.y,
268
- x2: lineEndX,
269
- y2: lineEndY,
270
- stroke: c.stroke,
271
- 'stroke-width': CONNECTOR_STROKE_WIDTH,
272
- 'stroke-linecap': 'round',
273
- });
274
- g.appendChild(connector);
275
-
276
- renderArrowhead(g, c.to.x, c.to.y, dx, dy, c.stroke);
277
- } else {
278
- const connector = createSVGElement('line');
279
- connector.setAttribute('class', 'oc-annotation-connector');
280
- setAttrs(connector, {
281
- x1: c.from.x,
282
- y1: c.from.y,
283
- x2: c.to.x,
284
- y2: c.to.y,
285
- stroke: c.stroke,
286
- 'stroke-width': CONNECTOR_STROKE_WIDTH,
287
- 'stroke-linecap': 'round',
288
- });
289
- g.appendChild(connector);
290
- }
291
- }
292
-
293
- // Endpoint marker: rendered AFTER the connector but BEFORE the label text so
294
- // it sits on top of the leader and under any text halo. The engine resolves
295
- // dot.x/y at the data point (never the pulled-back connector tip) and picks
296
- // the stroke so marker and leader read as one system.
297
- if (annotation.dot) {
298
- const dot = createSVGElement('circle');
299
- dot.setAttribute('class', 'oc-annotation-dot');
300
- setAttrs(dot, {
301
- cx: annotation.dot.x,
302
- cy: annotation.dot.y,
303
- r: annotation.dot.radius,
304
- fill: annotation.dot.fill,
305
- stroke: annotation.dot.stroke,
306
- 'stroke-width': annotation.dot.strokeWidth,
307
- });
308
- g.appendChild(dot);
309
- }
310
-
311
- const text = createSVGElement('text');
312
- text.setAttribute('class', 'oc-annotation-label');
313
- setAttrs(text, { x: annotation.label.x, y: annotation.label.y });
314
- applyTextStyle(text, annotation.label.style);
315
-
316
- const fontSize = annotation.label.style.fontSize ?? DEFAULT_ANNOTATION_FONT_SIZE;
317
- const lineHeight = fontSize * (annotation.label.style.lineHeight ?? DEFAULT_LINE_HEIGHT);
318
-
319
- fillRichText(text, annotation.label.text, annotation.label.x, lineHeight);
320
-
321
- // Render background rect behind text if specified, otherwise use
322
- // paint-order stroke halo to knock out lines behind text
323
- if (annotation.label.background && annotation.label.bounds) {
324
- // Size the plate from the engine's measured bounds, full stop. There used to
325
- // be a fallback branch here that re-derived the box from `fontSize * 0.55`
326
- // per character and center-aligned it for multi-line text. It was
327
- // unreachable (every resolver stamps `label.bounds`), but it was a third
328
- // copy of text geometry living in the renderer, and a *stale* one: it
329
- // center-aligned, which the redesign explicitly does not do. Dead code that
330
- // contradicts live code is the drift trap, not protection from it.
331
- const pad = 3;
332
- const b = annotation.label.bounds;
333
- const bgX = b.x - pad;
334
- const bgY = b.y - pad;
335
- const bgW = b.width + pad * 2;
336
- const bgH = b.height + pad * 2;
337
-
338
- const bgRect = createSVGElement('rect');
339
- bgRect.setAttribute('class', 'oc-annotation-bg');
340
- setAttrs(bgRect, {
341
- x: bgX,
342
- y: bgY,
343
- width: bgW,
344
- height: bgH,
345
- fill: annotation.label.background,
346
- rx: 2,
347
- });
348
- g.appendChild(bgRect);
349
- } else if (bgColor && annotation.label.halo !== false) {
350
- text.style.paintOrder = 'stroke';
351
- text.style.stroke = bgColor;
352
- text.style.strokeWidth = `${Math.round(fontSize * 0.3)}px`;
353
- text.style.strokeLinejoin = 'round';
354
- }
355
-
356
- g.appendChild(text);
357
-
358
- // Optional muted subtitle, positioned by the engine below the primary label.
359
- if (annotation.subtitle) {
360
- const sub = createSVGElement('text');
361
- sub.setAttribute('class', 'oc-annotation-subtitle');
362
- setAttrs(sub, { x: annotation.subtitle.x, y: annotation.subtitle.y });
363
- applyTextStyle(sub, annotation.subtitle.style);
364
- const subFontSize = annotation.subtitle.style.fontSize ?? DEFAULT_ANNOTATION_FONT_SIZE;
365
- const subLineHeight =
366
- subFontSize * (annotation.subtitle.style.lineHeight ?? DEFAULT_LINE_HEIGHT);
367
- fillRichText(sub, annotation.subtitle.text, annotation.subtitle.x, subLineHeight);
368
- g.appendChild(sub);
369
- }
370
- }
371
-
372
- parent.appendChild(g);
373
- }
374
-
375
- export function renderAnnotations(parent: SVGElement, layout: ChartLayout): void {
376
- if (layout.annotations.length === 0) return;
377
-
378
- const g = createSVGElement('g');
379
- g.setAttribute('class', 'oc-annotations');
380
-
381
- // Annotations are already sorted by zIndex from the engine, so render in order
382
- const bgColor = layout.theme.colors.background;
383
- for (let i = 0; i < layout.annotations.length; i++) {
384
- renderAnnotation(g, layout.annotations[i], i, bgColor);
385
- }
386
-
387
- parent.appendChild(g);
388
- }