@opendata-ai/openchart-vanilla 8.0.0-rc.9 → 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 (150) hide show
  1. package/dist/{chunk-SPILUE5S.js → chunk-2NAQRP66.js} +2 -2
  2. package/dist/{chunk-IZQBT6QM.js → chunk-6BUCYCLD.js} +7283 -5881
  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-QW7ZB6NE.js → chunk-EKAUQAAX.js} +49 -45
  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-IZQBT6QM.js.map +0 -1
  30. package/dist/chunk-QW7ZB6NE.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 -1477
  39. package/src/__tests__/editable-prop.test.ts +0 -174
  40. package/src/__tests__/events.test.ts +0 -387
  41. package/src/__tests__/export.test.ts +0 -241
  42. package/src/__tests__/font-race.test.ts +0 -199
  43. package/src/__tests__/gradient-ids.test.ts +0 -159
  44. package/src/__tests__/gradient-utils.test.ts +0 -125
  45. package/src/__tests__/invert.test.ts +0 -187
  46. package/src/__tests__/known-bugs-render.test.ts +0 -183
  47. package/src/__tests__/map.test.ts +0 -636
  48. package/src/__tests__/mount.test.ts +0 -357
  49. package/src/__tests__/pattern-fills.test.ts +0 -105
  50. package/src/__tests__/resize-timing.test.ts +0 -218
  51. package/src/__tests__/sankey.test.ts +0 -133
  52. package/src/__tests__/selection-events.test.ts +0 -1062
  53. package/src/__tests__/series-search.test.ts +0 -311
  54. package/src/__tests__/static.test.ts +0 -141
  55. package/src/__tests__/svg-renderer.test.ts +0 -895
  56. package/src/__tests__/table-keyboard.test.ts +0 -361
  57. package/src/__tests__/table-mount.test.ts +0 -639
  58. package/src/__tests__/tilemap.test.ts +0 -158
  59. package/src/__tests__/tooltip.test.ts +0 -337
  60. package/src/__tests__/transition.test.ts +0 -1792
  61. package/src/__tests__/update-scrolly-matrix.test.ts +0 -563
  62. package/src/__tests__/you-draw-it.test.ts +0 -273
  63. package/src/animation-vars.ts +0 -42
  64. package/src/animation.ts +0 -76
  65. package/src/barlist-mount.ts +0 -383
  66. package/src/barlist-renderer.ts +0 -267
  67. package/src/dom-helpers.ts +0 -17
  68. package/src/export.ts +0 -482
  69. package/src/gradient-utils.ts +0 -138
  70. package/src/graph/__tests__/canvas-renderer.test.ts +0 -713
  71. package/src/graph/__tests__/graph-mount.test.ts +0 -213
  72. package/src/graph/__tests__/interaction.test.ts +0 -205
  73. package/src/graph/__tests__/keyboard.test.ts +0 -653
  74. package/src/graph/__tests__/search.test.ts +0 -88
  75. package/src/graph/__tests__/simulation.test.ts +0 -233
  76. package/src/graph/__tests__/spatial-index.test.ts +0 -142
  77. package/src/graph/__tests__/zoom.test.ts +0 -203
  78. package/src/graph/canvas-renderer.ts +0 -747
  79. package/src/graph/interaction.ts +0 -367
  80. package/src/graph/keyboard.ts +0 -208
  81. package/src/graph/search.ts +0 -50
  82. package/src/graph/simulation-worker-url.ts +0 -12
  83. package/src/graph/simulation-worker.ts +0 -278
  84. package/src/graph/simulation.ts +0 -422
  85. package/src/graph/spatial-index.ts +0 -121
  86. package/src/graph/types.ts +0 -47
  87. package/src/graph/worker-protocol.ts +0 -70
  88. package/src/graph/zoom.ts +0 -134
  89. package/src/graph-mount.ts +0 -888
  90. package/src/index.ts +0 -76
  91. package/src/interactions/chart-events.ts +0 -139
  92. package/src/interactions/crosshair.ts +0 -233
  93. package/src/interactions/drag-handler.ts +0 -175
  94. package/src/interactions/editing-drags.ts +0 -649
  95. package/src/interactions/index.ts +0 -27
  96. package/src/interactions/invert.ts +0 -54
  97. package/src/interactions/keyboard-nav.ts +0 -111
  98. package/src/interactions/legend-interaction.ts +0 -46
  99. package/src/interactions/selection.ts +0 -286
  100. package/src/interactions/tooltip-events.ts +0 -72
  101. package/src/map-camera.ts +0 -111
  102. package/src/map-mount.ts +0 -797
  103. package/src/map-renderer.ts +0 -476
  104. package/src/map-transition.ts +0 -99
  105. package/src/measure-text.ts +0 -134
  106. package/src/mount.ts +0 -1434
  107. package/src/pattern-utils.ts +0 -110
  108. package/src/renderers/annotations.ts +0 -388
  109. package/src/renderers/axes.ts +0 -351
  110. package/src/renderers/brand.ts +0 -87
  111. package/src/renderers/chrome.ts +0 -196
  112. package/src/renderers/endpoint-labels.ts +0 -150
  113. package/src/renderers/legend.ts +0 -312
  114. package/src/renderers/marks.ts +0 -561
  115. package/src/renderers/metrics.ts +0 -63
  116. package/src/renderers/svg-dom.ts +0 -40
  117. package/src/renderers/table-cells.ts +0 -446
  118. package/src/resize-observer.ts +0 -46
  119. package/src/sankey-mount.ts +0 -622
  120. package/src/sankey-renderer.ts +0 -614
  121. package/src/series-search.ts +0 -392
  122. package/src/static.ts +0 -251
  123. package/src/story/__tests__/camera-math.test.ts +0 -145
  124. package/src/story/__tests__/create-chart-story.test.ts +0 -317
  125. package/src/story/__tests__/progress-math.test.ts +0 -95
  126. package/src/story/__tests__/scroll-driver.test.ts +0 -130
  127. package/src/story/__tests__/tween.test.ts +0 -159
  128. package/src/story/camera-math.ts +0 -125
  129. package/src/story/create-chart-story.ts +0 -353
  130. package/src/story/crossfade.ts +0 -70
  131. package/src/story/index.ts +0 -39
  132. package/src/story/progress-math.ts +0 -85
  133. package/src/story/resolve-camera-target.ts +0 -119
  134. package/src/story/scroll-driver.ts +0 -206
  135. package/src/story/story-camera.ts +0 -49
  136. package/src/story/tween.ts +0 -112
  137. package/src/story/types.ts +0 -76
  138. package/src/svg-ids.ts +0 -23
  139. package/src/svg-renderer.ts +0 -383
  140. package/src/table-keyboard.ts +0 -266
  141. package/src/table-mount.ts +0 -567
  142. package/src/table-renderer.ts +0 -414
  143. package/src/text-edit-overlay.ts +0 -389
  144. package/src/theme-tokens.ts +0 -51
  145. package/src/tilemap-mount.ts +0 -511
  146. package/src/tilemap-renderer.ts +0 -396
  147. package/src/tooltip.ts +0 -177
  148. package/src/transition.ts +0 -2816
  149. package/src/you-draw-it.ts +0 -480
  150. /package/dist/{chunk-SPILUE5S.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,312 +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 =
191
- legend.position === 'top' || legend.position === 'top-left' || legend.position === 'bottom';
192
- const positions = 'entryPositions' in legend ? legend.entryPositions : undefined;
193
- let offsetX = legend.bounds.x;
194
- let offsetY = legend.bounds.y;
195
-
196
- for (let i = 0; i < legend.entries.length; i++) {
197
- const entry = legend.entries[i];
198
-
199
- const pos = positions?.[i];
200
- if (pos) {
201
- offsetX = pos.x;
202
- offsetY = pos.y;
203
- } else if (isHorizontal && i > 0) {
204
- const labelWidth = estimateTextWidth(
205
- entry.label,
206
- legend.labelStyle.fontSize,
207
- legend.labelStyle.fontWeight,
208
- );
209
- const entryWidth = legend.swatchSize + legend.swatchGap + labelWidth + legend.entryGap;
210
- if (offsetX + entryWidth > legend.bounds.x + legend.bounds.width) {
211
- offsetX = legend.bounds.x;
212
- offsetY += legend.swatchSize + 6;
213
- }
214
- }
215
- const entryG = createSVGElement('g');
216
- entryG.setAttribute('class', 'oc-legend-entry');
217
- entryG.setAttribute('role', 'listitem');
218
- entryG.setAttribute('data-legend-index', String(i));
219
- entryG.setAttribute('data-legend-label', entry.label);
220
- if (entry.overflow) {
221
- entryG.setAttribute('data-legend-overflow', 'true');
222
- entryG.setAttribute('aria-label', entry.label);
223
- entryG.setAttribute('opacity', '0.5');
224
- } else {
225
- entryG.setAttribute(
226
- 'aria-label',
227
- `${entry.label}: ${entry.active !== false ? 'visible' : 'hidden'}`,
228
- );
229
- entryG.setAttribute('style', 'cursor: pointer');
230
-
231
- // Apply dimming for inactive entries
232
- if (entry.active === false) {
233
- entryG.setAttribute('opacity', '0.3');
234
- }
235
- }
236
-
237
- // Swatch: bare colored mark matching the chart type.
238
- // Circle for point/scatter, line segment for line/area, filled rect for bar.
239
- const midX = offsetX + legend.swatchSize / 2;
240
- const midY = offsetY + legend.swatchSize / 2;
241
- if (entry.shape === 'circle') {
242
- const circle = createSVGElement('circle');
243
- setAttrs(circle, {
244
- cx: midX,
245
- cy: midY,
246
- r: legend.swatchSize / 2,
247
- fill: entry.color,
248
- });
249
- entryG.appendChild(circle);
250
- } else if (entry.shape === 'line') {
251
- const lineWidth = legend.swatchSize;
252
- const line = createSVGElement('line');
253
- line.setAttribute('class', 'oc-legend-swatch-line');
254
- setAttrs(line, {
255
- x1: offsetX,
256
- y1: midY,
257
- x2: offsetX + lineWidth,
258
- y2: midY,
259
- stroke: entry.color,
260
- 'stroke-width': 2,
261
- 'stroke-linecap': 'round',
262
- });
263
- entryG.appendChild(line);
264
- } else {
265
- const rectSize = Math.round(legend.swatchSize * 0.6);
266
- const rect = createSVGElement('rect');
267
- rect.setAttribute('class', 'oc-legend-swatch-rect');
268
- setAttrs(rect, {
269
- x: offsetX + (legend.swatchSize - rectSize) / 2,
270
- y: midY - rectSize / 2,
271
- width: rectSize,
272
- height: rectSize,
273
- rx: 2,
274
- ry: 2,
275
- fill: entry.color,
276
- });
277
- entryG.appendChild(rect);
278
- }
279
-
280
- // Label
281
- const label = createSVGElement('text');
282
- setAttrs(label, {
283
- x: offsetX + legend.swatchSize + legend.swatchGap,
284
- y: offsetY + legend.swatchSize / 2,
285
- 'dominant-baseline': 'central',
286
- });
287
- applyTextStyle(label, legend.labelStyle);
288
- label.textContent = entry.label;
289
- entryG.appendChild(label);
290
-
291
- g.appendChild(entryG);
292
-
293
- if (!pos) {
294
- if (isHorizontal) {
295
- const labelWidth = estimateTextWidth(
296
- entry.label,
297
- legend.labelStyle.fontSize,
298
- legend.labelStyle.fontWeight,
299
- );
300
- const entryWidth = legend.swatchSize + legend.swatchGap + labelWidth + legend.entryGap;
301
- offsetX += entryWidth;
302
- } else {
303
- offsetY +=
304
- 'rowHeight' in legend && legend.rowHeight
305
- ? legend.rowHeight
306
- : legend.swatchSize + legend.entryGap;
307
- }
308
- }
309
- }
310
-
311
- parent.appendChild(g);
312
- }