@opendata-ai/openchart-vanilla 8.0.0-rc.2 → 8.0.0-rc.21

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 (145) hide show
  1. package/dist/{chunk-W34KN7FD.js → chunk-2NAQRP66.js} +2 -2
  2. package/dist/chunk-DCODCLWI.js +234 -0
  3. package/dist/chunk-DCODCLWI.js.map +1 -0
  4. package/dist/{chunk-5EOV5JQZ.js → chunk-EKAUQAAX.js} +50 -46
  5. package/dist/chunk-EKAUQAAX.js.map +1 -0
  6. package/dist/{chunk-ENXSUXC5.js → chunk-KNT5AZ2O.js} +7309 -5490
  7. package/dist/chunk-KNT5AZ2O.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 +283 -24
  16. package/dist/index.js +2012 -763
  17. package/dist/index.js.map +1 -1
  18. package/dist/{mount-CUKQLeqD.d.ts → mount-Dvv8NYIv.d.ts} +117 -3
  19. package/dist/simulation-worker.js +83 -2
  20. package/dist/static.js +7 -69
  21. package/dist/static.js.map +1 -1
  22. package/dist/story/index.d.ts +3 -2
  23. package/dist/story/index.js +5 -3
  24. package/dist/story/index.js.map +1 -1
  25. package/dist/styles.css +1 -1
  26. package/dist/styles.css.map +1 -1
  27. package/package.json +14 -6
  28. package/dist/chunk-5EOV5JQZ.js.map +0 -1
  29. package/dist/chunk-ENXSUXC5.js.map +0 -1
  30. package/src/__test-fixtures__/dom.ts +0 -70
  31. package/src/__test-fixtures__/specs.ts +0 -214
  32. package/src/__tests__/animation.test.ts +0 -358
  33. package/src/__tests__/annotations-render.test.ts +0 -201
  34. package/src/__tests__/compound-labels.test.ts +0 -122
  35. package/src/__tests__/continuous-legend.test.ts +0 -91
  36. package/src/__tests__/crosshair.test.ts +0 -130
  37. package/src/__tests__/edit-events.test.ts +0 -902
  38. package/src/__tests__/events.test.ts +0 -387
  39. package/src/__tests__/export.test.ts +0 -241
  40. package/src/__tests__/font-race.test.ts +0 -199
  41. package/src/__tests__/gradient-ids.test.ts +0 -159
  42. package/src/__tests__/gradient-utils.test.ts +0 -125
  43. package/src/__tests__/known-bugs-render.test.ts +0 -183
  44. package/src/__tests__/map.test.ts +0 -293
  45. package/src/__tests__/mount.test.ts +0 -357
  46. package/src/__tests__/pattern-fills.test.ts +0 -105
  47. package/src/__tests__/resize-timing.test.ts +0 -218
  48. package/src/__tests__/sankey.test.ts +0 -133
  49. package/src/__tests__/selection-events.test.ts +0 -869
  50. package/src/__tests__/series-search.test.ts +0 -311
  51. package/src/__tests__/static.test.ts +0 -141
  52. package/src/__tests__/svg-renderer.test.ts +0 -895
  53. package/src/__tests__/table-keyboard.test.ts +0 -361
  54. package/src/__tests__/table-mount.test.ts +0 -639
  55. package/src/__tests__/tilemap.test.ts +0 -158
  56. package/src/__tests__/tooltip.test.ts +0 -337
  57. package/src/__tests__/transition.test.ts +0 -1777
  58. package/src/__tests__/you-draw-it.test.ts +0 -273
  59. package/src/animation-vars.ts +0 -42
  60. package/src/animation.ts +0 -76
  61. package/src/barlist-mount.ts +0 -383
  62. package/src/barlist-renderer.ts +0 -267
  63. package/src/dom-helpers.ts +0 -17
  64. package/src/export.ts +0 -482
  65. package/src/gradient-utils.ts +0 -138
  66. package/src/graph/__tests__/canvas-renderer.test.ts +0 -713
  67. package/src/graph/__tests__/graph-mount.test.ts +0 -213
  68. package/src/graph/__tests__/interaction.test.ts +0 -205
  69. package/src/graph/__tests__/keyboard.test.ts +0 -653
  70. package/src/graph/__tests__/search.test.ts +0 -88
  71. package/src/graph/__tests__/simulation.test.ts +0 -233
  72. package/src/graph/__tests__/spatial-index.test.ts +0 -142
  73. package/src/graph/__tests__/zoom.test.ts +0 -203
  74. package/src/graph/canvas-renderer.ts +0 -747
  75. package/src/graph/interaction.ts +0 -367
  76. package/src/graph/keyboard.ts +0 -208
  77. package/src/graph/search.ts +0 -50
  78. package/src/graph/simulation-worker-url.ts +0 -12
  79. package/src/graph/simulation-worker.ts +0 -278
  80. package/src/graph/simulation.ts +0 -422
  81. package/src/graph/spatial-index.ts +0 -121
  82. package/src/graph/types.ts +0 -47
  83. package/src/graph/worker-protocol.ts +0 -70
  84. package/src/graph/zoom.ts +0 -134
  85. package/src/graph-mount.ts +0 -888
  86. package/src/index.ts +0 -76
  87. package/src/interactions/chart-events.ts +0 -139
  88. package/src/interactions/crosshair.ts +0 -233
  89. package/src/interactions/drag-handler.ts +0 -175
  90. package/src/interactions/editing-drags.ts +0 -515
  91. package/src/interactions/index.ts +0 -25
  92. package/src/interactions/keyboard-nav.ts +0 -111
  93. package/src/interactions/legend-interaction.ts +0 -46
  94. package/src/interactions/selection.ts +0 -280
  95. package/src/interactions/tooltip-events.ts +0 -72
  96. package/src/map-camera.ts +0 -88
  97. package/src/map-mount.ts +0 -701
  98. package/src/map-renderer.ts +0 -338
  99. package/src/map-transition.ts +0 -83
  100. package/src/measure-text.ts +0 -134
  101. package/src/mount.ts +0 -1382
  102. package/src/pattern-utils.ts +0 -110
  103. package/src/renderers/annotations.ts +0 -388
  104. package/src/renderers/axes.ts +0 -351
  105. package/src/renderers/brand.ts +0 -87
  106. package/src/renderers/chrome.ts +0 -196
  107. package/src/renderers/endpoint-labels.ts +0 -150
  108. package/src/renderers/legend.ts +0 -311
  109. package/src/renderers/marks.ts +0 -561
  110. package/src/renderers/metrics.ts +0 -63
  111. package/src/renderers/svg-dom.ts +0 -40
  112. package/src/renderers/table-cells.ts +0 -446
  113. package/src/resize-observer.ts +0 -46
  114. package/src/sankey-mount.ts +0 -622
  115. package/src/sankey-renderer.ts +0 -614
  116. package/src/series-search.ts +0 -392
  117. package/src/static.ts +0 -251
  118. package/src/story/__tests__/camera-math.test.ts +0 -145
  119. package/src/story/__tests__/create-chart-story.test.ts +0 -317
  120. package/src/story/__tests__/progress-math.test.ts +0 -95
  121. package/src/story/__tests__/scroll-driver.test.ts +0 -130
  122. package/src/story/__tests__/tween.test.ts +0 -159
  123. package/src/story/camera-math.ts +0 -125
  124. package/src/story/create-chart-story.ts +0 -353
  125. package/src/story/crossfade.ts +0 -70
  126. package/src/story/index.ts +0 -39
  127. package/src/story/progress-math.ts +0 -85
  128. package/src/story/resolve-camera-target.ts +0 -119
  129. package/src/story/scroll-driver.ts +0 -206
  130. package/src/story/story-camera.ts +0 -49
  131. package/src/story/tween.ts +0 -112
  132. package/src/story/types.ts +0 -76
  133. package/src/svg-ids.ts +0 -23
  134. package/src/svg-renderer.ts +0 -383
  135. package/src/table-keyboard.ts +0 -266
  136. package/src/table-mount.ts +0 -567
  137. package/src/table-renderer.ts +0 -414
  138. package/src/text-edit-overlay.ts +0 -255
  139. package/src/theme-tokens.ts +0 -51
  140. package/src/tilemap-mount.ts +0 -511
  141. package/src/tilemap-renderer.ts +0 -396
  142. package/src/tooltip.ts +0 -177
  143. package/src/transition.ts +0 -2816
  144. package/src/you-draw-it.ts +0 -480
  145. /package/dist/{chunk-W34KN7FD.js.map → chunk-2NAQRP66.js.map} +0 -0
@@ -1,150 +0,0 @@
1
- /**
2
- * Endpoint-labels rendering: per-series label columns for multi-series
3
- * line/area charts. Supports trailing (right) and optional leading (left)
4
- * columns when `ends: 'both'` is set.
5
- *
6
- * The engine resolves all positions, colors, and styles. This renderer is dumb:
7
- * it reads `layout.endpointLabels` and stamps SVG. Suppression logic lives in
8
- * the engine — when entries is empty, the renderer is a no-op.
9
- */
10
-
11
- import type {
12
- ChartLayout,
13
- EndpointLabelEntry,
14
- EndpointLabelsLayout,
15
- Rect,
16
- } from '@opendata-ai/openchart-core';
17
- import { applyTextStyle, createSVGElement, setAttrs } from './svg-dom';
18
-
19
- const LEADER_STROKE_WIDTH = 1;
20
- const LEADER_OPACITY = 0.45;
21
-
22
- function renderColumn(
23
- root: SVGElement,
24
- entries: EndpointLabelEntry[],
25
- bounds: Rect,
26
- ep: EndpointLabelsLayout,
27
- leaderAnchorX: number,
28
- side: 'trailing' | 'leading',
29
- ): void {
30
- const labelFontSize = ep.labelStyle.fontSize ?? 11;
31
- const labelLineHeight = labelFontSize * (ep.labelStyle.lineHeight ?? 1.25);
32
- const valueFontSize = ep.valueStyle.fontSize ?? 11;
33
-
34
- const chipX = bounds.x;
35
- const chipWidth = ep.swatchSize;
36
- const textX = chipX + chipWidth + ep.gap;
37
-
38
- for (let i = 0; i < entries.length; i++) {
39
- const entry = entries[i];
40
-
41
- const entryG = createSVGElement('g');
42
- entryG.setAttribute('class', 'oc-endpoint-label-entry');
43
- entryG.setAttribute('role', 'listitem');
44
- entryG.setAttribute('data-endpoint-index', String(i));
45
- entryG.setAttribute('data-endpoint-key', entry.seriesKey);
46
- entryG.setAttribute('data-endpoint-side', side);
47
- entryG.setAttribute('aria-label', `${entry.seriesKey}: ${entry.value}`);
48
-
49
- if (entry.showLeader) {
50
- const leader = createSVGElement('line');
51
- leader.setAttribute('class', 'oc-endpoint-leader');
52
- setAttrs(leader, {
53
- x1: chipX,
54
- y1: entry.labelY + labelFontSize / 2,
55
- x2: leaderAnchorX,
56
- y2: entry.dataY,
57
- stroke: entry.color,
58
- 'stroke-width': LEADER_STROKE_WIDTH,
59
- 'stroke-opacity': LEADER_OPACITY,
60
- });
61
- entryG.appendChild(leader);
62
- }
63
-
64
- const rowY = entry.labelY + labelFontSize / 2;
65
- const line = createSVGElement('line');
66
- line.setAttribute('class', 'oc-endpoint-swatch-line');
67
- setAttrs(line, {
68
- x1: chipX,
69
- y1: rowY,
70
- x2: chipX + chipWidth,
71
- y2: rowY,
72
- stroke: entry.color,
73
- 'stroke-width': 2,
74
- 'stroke-linecap': 'round',
75
- });
76
- entryG.appendChild(line);
77
-
78
- const label = createSVGElement('text');
79
- label.setAttribute('class', 'oc-endpoint-label');
80
- setAttrs(label, { x: textX, y: entry.labelY + labelFontSize });
81
- applyTextStyle(label, ep.labelStyle);
82
- (label as SVGElement & ElementCSSInlineStyle).style.setProperty('fill', entry.color);
83
-
84
- if (entry.labelLines.length <= 1) {
85
- label.textContent = entry.labelLines[0] ?? entry.seriesKey;
86
- } else {
87
- for (let li = 0; li < entry.labelLines.length; li++) {
88
- const tspan = createSVGElement('tspan');
89
- setAttrs(tspan, { x: textX, dy: li === 0 ? 0 : labelLineHeight });
90
- tspan.textContent = entry.labelLines[li];
91
- label.appendChild(tspan);
92
- }
93
- }
94
- entryG.appendChild(label);
95
-
96
- const lineCount = Math.max(entry.labelLines.length, 1);
97
- const valueY =
98
- entry.labelY +
99
- labelFontSize +
100
- (lineCount - 1) * labelLineHeight +
101
- ep.valueGap +
102
- valueFontSize;
103
- const value = createSVGElement('text');
104
- value.setAttribute('class', 'oc-endpoint-value');
105
- setAttrs(value, { x: textX, y: valueY });
106
- applyTextStyle(value, ep.valueStyle);
107
- value.textContent = entry.value;
108
- entryG.appendChild(value);
109
-
110
- if (entry.marker) {
111
- const marker = createSVGElement('circle');
112
- marker.setAttribute('class', 'oc-endpoint-marker');
113
- setAttrs(marker, {
114
- cx: entry.marker.x,
115
- cy: entry.marker.y,
116
- r: entry.marker.radius,
117
- fill: entry.marker.fill,
118
- stroke: entry.marker.stroke,
119
- 'stroke-width': entry.marker.strokeWidth,
120
- });
121
- entryG.appendChild(marker);
122
- }
123
-
124
- root.appendChild(entryG);
125
- }
126
- }
127
-
128
- export function renderEndpointLabels(parent: SVGElement, layout: ChartLayout): void {
129
- const ep = layout.endpointLabels;
130
- if (!ep || ep.entries.length === 0) return;
131
-
132
- const chartArea = layout.area;
133
-
134
- const root = createSVGElement('g');
135
- root.setAttribute('class', 'oc-endpoint-labels');
136
- root.setAttribute('role', 'list');
137
- root.setAttribute('aria-label', 'Endpoint labels');
138
-
139
- // Trailing column (right side).
140
- const chartRightX = chartArea.x + chartArea.width;
141
- renderColumn(root, ep.entries, ep.bounds, ep, chartRightX, 'trailing');
142
-
143
- // Leading column (left side) when `ends: 'both'`.
144
- if (ep.leading && ep.leading.length > 0 && ep.leadingBounds) {
145
- const chartLeftX = chartArea.x;
146
- renderColumn(root, ep.leading, ep.leadingBounds, ep, chartLeftX, 'leading');
147
- }
148
-
149
- parent.appendChild(root);
150
- }
@@ -1,311 +0,0 @@
1
- /**
2
- * Legend rendering: swatches + labels with wrap/overflow handling, plus the
3
- * continuous variant (gradient bar / binned swatch row) for quantitative
4
- * color scales.
5
- */
6
-
7
- import type {
8
- CategoricalLegendLayout,
9
- ContinuousLegendLayout,
10
- LegendLayout,
11
- SizeLegendLayout,
12
- } from '@opendata-ai/openchart-core';
13
- import { estimateTextWidth } from '@opendata-ai/openchart-core';
14
- import { nextSvgId } from '../svg-ids';
15
- import { applyTextStyle, createSVGElement, setAttrs } from './svg-dom';
16
-
17
- function isCategorical(legend: LegendLayout): legend is CategoricalLegendLayout {
18
- return !legend.type || legend.type === 'categorical';
19
- }
20
-
21
- /**
22
- * Render a continuous color legend: a gradient-filled bar (gradient mode) or
23
- * a contiguous swatch row (binned mode) with value labels below.
24
- *
25
- * The gradient ID comes from the shared `nextSvgId` counter like every other
26
- * gradient/clip-path def, so IDs stay deterministic at generation time.
27
- */
28
- function renderContinuousLegend(parent: SVGElement, legend: ContinuousLegendLayout): void {
29
- if (legend.bounds.width <= 0 || legend.bounds.height <= 0) return;
30
-
31
- const g = createSVGElement('g');
32
- g.setAttribute('class', 'oc-legend oc-legend--continuous');
33
- g.setAttribute('role', 'img');
34
- const first = legend.ticks[0];
35
- const last = legend.ticks[legend.ticks.length - 1];
36
- if (first && last) {
37
- g.setAttribute('aria-label', `Color scale from ${first.label} to ${last.label}`);
38
- } else {
39
- g.setAttribute('aria-label', 'Color scale');
40
- }
41
-
42
- if (legend.mode === 'gradient') {
43
- // linearGradient def + one rect. Defs may already exist (clip paths).
44
- let defs: SVGElement | null = parent.querySelector('defs');
45
- if (!defs) {
46
- defs = createSVGElement('defs');
47
- parent.insertBefore(defs, parent.firstChild);
48
- }
49
- const gradientId = nextSvgId('oc-legend-gradient');
50
- const grad = createSVGElement('linearGradient');
51
- grad.setAttribute('id', gradientId);
52
- setAttrs(grad, { x1: '0%', y1: '0%', x2: '100%', y2: '0%' });
53
- for (const stop of legend.colorStops) {
54
- const s = createSVGElement('stop');
55
- const attrs: Record<string, string | number> = {
56
- offset: `${stop.offset * 100}%`,
57
- 'stop-color': stop.color,
58
- };
59
- if (stop.opacity !== undefined) attrs['stop-opacity'] = stop.opacity;
60
- setAttrs(s, attrs);
61
- grad.appendChild(s);
62
- }
63
- defs.appendChild(grad);
64
-
65
- const bar = createSVGElement('rect');
66
- bar.setAttribute('class', 'oc-legend-gradient-bar');
67
- setAttrs(bar, {
68
- x: legend.bar.x,
69
- y: legend.bar.y,
70
- width: legend.bar.width,
71
- height: legend.bar.height,
72
- rx: 2,
73
- ry: 2,
74
- fill: `url(#${gradientId})`,
75
- });
76
- g.appendChild(bar);
77
- } else {
78
- // Binned: contiguous class swatches, square-cornered (Datawrapper-style).
79
- for (const bin of legend.bins) {
80
- const rect = createSVGElement('rect');
81
- rect.setAttribute('class', 'oc-legend-bin');
82
- setAttrs(rect, {
83
- x: bin.x,
84
- y: legend.bar.y,
85
- width: bin.width,
86
- height: legend.bar.height,
87
- fill: bin.color,
88
- });
89
- g.appendChild(rect);
90
- }
91
- }
92
-
93
- for (const tick of legend.ticks) {
94
- const label = createSVGElement('text');
95
- setAttrs(label, { x: tick.x, y: legend.labelY, 'text-anchor': tick.anchor });
96
- applyTextStyle(label, legend.labelStyle);
97
- label.textContent = tick.label;
98
- g.appendChild(label);
99
- }
100
-
101
- parent.appendChild(g);
102
- }
103
-
104
- /**
105
- * Render a size legend: graduated circles, nested (concentric, sharing a bottom
106
- * edge) so the ratio between magnitudes reads, not just their order.
107
- *
108
- * Emits **no** `data-legend-index`. That attribute is what
109
- * `wireLegendInteraction` sweeps up to wire click-to-toggle-series, and a size
110
- * circle is not a series -- clicking "500M" must not try to hide a series named
111
- * "500M". The circles are inert, and `aria-hidden` keeps them out of the AT tree
112
- * as decorative chrome (the values are already in each mark's aria label).
113
- */
114
- function renderSizeLegend(parent: SVGElement, legend: SizeLegendLayout): void {
115
- if (legend.circles.length === 0) return;
116
-
117
- const g = createSVGElement('g');
118
- g.setAttribute('class', 'oc-legend oc-legend--size');
119
- // Decorative, not announced. `role="img"` + a bare "Size legend" label would
120
- // tell a screen-reader user a key exists while conveying none of the values it
121
- // keys -- pure noise. The magnitudes are already in each mark's own aria label,
122
- // which is where a non-sighted reader actually gets them.
123
- g.setAttribute('aria-hidden', 'true');
124
-
125
- // Absolute coordinates, NOT a group `transform`. `getBBox()` reports a group's
126
- // box in its own local space and ignores the group's transform, so a
127
- // translated legend measures as if it sat at the origin -- which is exactly
128
- // how a size/color legend collision slipped past the rendered-invariant
129
- // overlap checks. Every other legend bakes absolute coords into its children;
130
- // this one does too, so the same measurement sees it where it really is.
131
- const ox = legend.bounds.x;
132
- const oy = legend.bounds.y;
133
- const labelX = ox + legend.circles[0].cx + legend.circles[0].radius + 8;
134
- const leaderX = ox + legend.circles[0].cx + legend.circles[0].radius + 4;
135
-
136
- for (const circle of legend.circles) {
137
- const c = createSVGElement('circle');
138
- setAttrs(c, {
139
- cx: ox + circle.cx,
140
- cy: oy + circle.cy,
141
- r: circle.radius,
142
- fill: 'none',
143
- stroke: legend.stroke,
144
- 'stroke-width': 1,
145
- });
146
- g.appendChild(c);
147
-
148
- // Leader line from the circle's top edge out to its label.
149
- const line = createSVGElement('line');
150
- setAttrs(line, {
151
- x1: ox + circle.cx,
152
- y1: oy + circle.cy - circle.radius,
153
- x2: leaderX,
154
- y2: oy + circle.cy - circle.radius,
155
- stroke: legend.stroke,
156
- 'stroke-width': 1,
157
- 'stroke-dasharray': '2 2',
158
- });
159
- g.appendChild(line);
160
-
161
- const label = createSVGElement('text');
162
- setAttrs(label, {
163
- x: labelX,
164
- y: oy + circle.labelY,
165
- });
166
- applyTextStyle(label, legend.labelStyle);
167
- label.textContent = circle.label;
168
- g.appendChild(label);
169
- }
170
-
171
- parent.appendChild(g);
172
- }
173
-
174
- export function renderLegend(parent: SVGElement, legend: LegendLayout): void {
175
- if (legend.type === 'continuous') {
176
- renderContinuousLegend(parent, legend);
177
- return;
178
- }
179
- if (legend.type === 'size') {
180
- renderSizeLegend(parent, legend);
181
- return;
182
- }
183
- if (!isCategorical(legend) || legend.entries.length === 0) return;
184
-
185
- const g = createSVGElement('g');
186
- g.setAttribute('class', 'oc-legend');
187
- g.setAttribute('role', 'list');
188
- g.setAttribute('aria-label', 'Chart legend');
189
-
190
- const isHorizontal = legend.position === 'top' || legend.position === 'bottom';
191
- const positions = 'entryPositions' in legend ? legend.entryPositions : undefined;
192
- let offsetX = legend.bounds.x;
193
- let offsetY = legend.bounds.y;
194
-
195
- for (let i = 0; i < legend.entries.length; i++) {
196
- const entry = legend.entries[i];
197
-
198
- const pos = positions?.[i];
199
- if (pos) {
200
- offsetX = pos.x;
201
- offsetY = pos.y;
202
- } else if (isHorizontal && i > 0) {
203
- const labelWidth = estimateTextWidth(
204
- entry.label,
205
- legend.labelStyle.fontSize,
206
- legend.labelStyle.fontWeight,
207
- );
208
- const entryWidth = legend.swatchSize + legend.swatchGap + labelWidth + legend.entryGap;
209
- if (offsetX + entryWidth > legend.bounds.x + legend.bounds.width) {
210
- offsetX = legend.bounds.x;
211
- offsetY += legend.swatchSize + 6;
212
- }
213
- }
214
- const entryG = createSVGElement('g');
215
- entryG.setAttribute('class', 'oc-legend-entry');
216
- entryG.setAttribute('role', 'listitem');
217
- entryG.setAttribute('data-legend-index', String(i));
218
- entryG.setAttribute('data-legend-label', entry.label);
219
- if (entry.overflow) {
220
- entryG.setAttribute('data-legend-overflow', 'true');
221
- entryG.setAttribute('aria-label', entry.label);
222
- entryG.setAttribute('opacity', '0.5');
223
- } else {
224
- entryG.setAttribute(
225
- 'aria-label',
226
- `${entry.label}: ${entry.active !== false ? 'visible' : 'hidden'}`,
227
- );
228
- entryG.setAttribute('style', 'cursor: pointer');
229
-
230
- // Apply dimming for inactive entries
231
- if (entry.active === false) {
232
- entryG.setAttribute('opacity', '0.3');
233
- }
234
- }
235
-
236
- // Swatch: bare colored mark matching the chart type.
237
- // Circle for point/scatter, line segment for line/area, filled rect for bar.
238
- const midX = offsetX + legend.swatchSize / 2;
239
- const midY = offsetY + legend.swatchSize / 2;
240
- if (entry.shape === 'circle') {
241
- const circle = createSVGElement('circle');
242
- setAttrs(circle, {
243
- cx: midX,
244
- cy: midY,
245
- r: legend.swatchSize / 2,
246
- fill: entry.color,
247
- });
248
- entryG.appendChild(circle);
249
- } else if (entry.shape === 'line') {
250
- const lineWidth = legend.swatchSize;
251
- const line = createSVGElement('line');
252
- line.setAttribute('class', 'oc-legend-swatch-line');
253
- setAttrs(line, {
254
- x1: offsetX,
255
- y1: midY,
256
- x2: offsetX + lineWidth,
257
- y2: midY,
258
- stroke: entry.color,
259
- 'stroke-width': 2,
260
- 'stroke-linecap': 'round',
261
- });
262
- entryG.appendChild(line);
263
- } else {
264
- const rectSize = Math.round(legend.swatchSize * 0.6);
265
- const rect = createSVGElement('rect');
266
- rect.setAttribute('class', 'oc-legend-swatch-rect');
267
- setAttrs(rect, {
268
- x: offsetX + (legend.swatchSize - rectSize) / 2,
269
- y: midY - rectSize / 2,
270
- width: rectSize,
271
- height: rectSize,
272
- rx: 2,
273
- ry: 2,
274
- fill: entry.color,
275
- });
276
- entryG.appendChild(rect);
277
- }
278
-
279
- // Label
280
- const label = createSVGElement('text');
281
- setAttrs(label, {
282
- x: offsetX + legend.swatchSize + legend.swatchGap,
283
- y: offsetY + legend.swatchSize / 2,
284
- 'dominant-baseline': 'central',
285
- });
286
- applyTextStyle(label, legend.labelStyle);
287
- label.textContent = entry.label;
288
- entryG.appendChild(label);
289
-
290
- g.appendChild(entryG);
291
-
292
- if (!pos) {
293
- if (isHorizontal) {
294
- const labelWidth = estimateTextWidth(
295
- entry.label,
296
- legend.labelStyle.fontSize,
297
- legend.labelStyle.fontWeight,
298
- );
299
- const entryWidth = legend.swatchSize + legend.swatchGap + labelWidth + legend.entryGap;
300
- offsetX += entryWidth;
301
- } else {
302
- offsetY +=
303
- 'rowHeight' in legend && legend.rowHeight
304
- ? legend.rowHeight
305
- : legend.swatchSize + legend.entryGap;
306
- }
307
- }
308
- }
309
-
310
- parent.appendChild(g);
311
- }