@danielmcsheehy/isopleth 0.0.0-stage → 0.2.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 (129) hide show
  1. package/dist/auto.d.ts +61 -0
  2. package/dist/auto.d.ts.map +1 -0
  3. package/dist/auto.js +213 -0
  4. package/dist/auto.js.map +1 -0
  5. package/dist/backend/index.d.ts +24 -0
  6. package/dist/backend/index.d.ts.map +1 -0
  7. package/dist/backend/index.js +42 -0
  8. package/dist/backend/index.js.map +1 -0
  9. package/dist/backend/js/index.d.ts +7 -0
  10. package/dist/backend/js/index.d.ts.map +1 -0
  11. package/dist/backend/js/index.js +33 -0
  12. package/dist/backend/js/index.js.map +1 -0
  13. package/dist/backend/js/insights.d.ts +30 -0
  14. package/dist/backend/js/insights.d.ts.map +1 -0
  15. package/dist/backend/js/insights.js +663 -0
  16. package/dist/backend/js/insights.js.map +1 -0
  17. package/dist/backend/js/kernels.d.ts +22 -0
  18. package/dist/backend/js/kernels.d.ts.map +1 -0
  19. package/dist/backend/js/kernels.js +562 -0
  20. package/dist/backend/js/kernels.js.map +1 -0
  21. package/dist/backend/js/stats.d.ts +26 -0
  22. package/dist/backend/js/stats.d.ts.map +1 -0
  23. package/dist/backend/js/stats.js +237 -0
  24. package/dist/backend/js/stats.js.map +1 -0
  25. package/dist/backend/types.d.ts +139 -0
  26. package/dist/backend/types.d.ts.map +1 -0
  27. package/dist/backend/types.js +12 -0
  28. package/dist/backend/types.js.map +1 -0
  29. package/dist/backend/wasm-wrap.d.ts +9 -0
  30. package/dist/backend/wasm-wrap.d.ts.map +1 -0
  31. package/dist/backend/wasm-wrap.js +52 -0
  32. package/dist/backend/wasm-wrap.js.map +1 -0
  33. package/dist/backend/wasm.d.ts +30 -0
  34. package/dist/backend/wasm.d.ts.map +1 -0
  35. package/dist/backend/wasm.js +29 -0
  36. package/dist/backend/wasm.js.map +1 -0
  37. package/dist/channel.d.ts +41 -0
  38. package/dist/channel.d.ts.map +1 -0
  39. package/dist/channel.js +205 -0
  40. package/dist/channel.js.map +1 -0
  41. package/dist/chart.d.ts +70 -0
  42. package/dist/chart.d.ts.map +1 -0
  43. package/dist/chart.js +68 -0
  44. package/dist/chart.js.map +1 -0
  45. package/dist/compile/index.d.ts +28 -0
  46. package/dist/compile/index.d.ts.map +1 -0
  47. package/dist/compile/index.js +391 -0
  48. package/dist/compile/index.js.map +1 -0
  49. package/dist/compile/palette.d.ts +41 -0
  50. package/dist/compile/palette.d.ts.map +1 -0
  51. package/dist/compile/palette.js +102 -0
  52. package/dist/compile/palette.js.map +1 -0
  53. package/dist/compile/resolve.d.ts +41 -0
  54. package/dist/compile/resolve.d.ts.map +1 -0
  55. package/dist/compile/resolve.js +155 -0
  56. package/dist/compile/resolve.js.map +1 -0
  57. package/dist/compile/scales.d.ts +49 -0
  58. package/dist/compile/scales.d.ts.map +1 -0
  59. package/dist/compile/scales.js +348 -0
  60. package/dist/compile/scales.js.map +1 -0
  61. package/dist/compile/series.d.ts +65 -0
  62. package/dist/compile/series.d.ts.map +1 -0
  63. package/dist/compile/series.js +948 -0
  64. package/dist/compile/series.js.map +1 -0
  65. package/dist/editor/index.d.ts +46 -0
  66. package/dist/editor/index.d.ts.map +1 -0
  67. package/dist/editor/index.js +342 -0
  68. package/dist/editor/index.js.map +1 -0
  69. package/dist/index.d.ts +47 -0
  70. package/dist/index.d.ts.map +1 -0
  71. package/dist/index.js +41 -0
  72. package/dist/index.js.map +1 -0
  73. package/dist/insights/index.d.ts +21 -0
  74. package/dist/insights/index.d.ts.map +1 -0
  75. package/dist/insights/index.js +461 -0
  76. package/dist/insights/index.js.map +1 -0
  77. package/dist/interval.d.ts +19 -0
  78. package/dist/interval.d.ts.map +1 -0
  79. package/dist/interval.js +166 -0
  80. package/dist/interval.js.map +1 -0
  81. package/dist/isopleth.browser.js +44 -0
  82. package/dist/isopleth.browser.js.LEGAL.txt +28 -0
  83. package/dist/isopleth.browser.js.map +7 -0
  84. package/dist/marks.d.ts +62 -0
  85. package/dist/marks.d.ts.map +1 -0
  86. package/dist/marks.js +176 -0
  87. package/dist/marks.js.map +1 -0
  88. package/dist/render.d.ts +21 -0
  89. package/dist/render.d.ts.map +1 -0
  90. package/dist/render.js +117 -0
  91. package/dist/render.js.map +1 -0
  92. package/dist/spec.d.ts +56 -0
  93. package/dist/spec.d.ts.map +1 -0
  94. package/dist/spec.js +191 -0
  95. package/dist/spec.js.map +1 -0
  96. package/dist/theme.d.ts +82 -0
  97. package/dist/theme.d.ts.map +1 -0
  98. package/dist/theme.js +136 -0
  99. package/dist/theme.js.map +1 -0
  100. package/dist/transforms/basic.d.ts +35 -0
  101. package/dist/transforms/basic.d.ts.map +1 -0
  102. package/dist/transforms/basic.js +174 -0
  103. package/dist/transforms/basic.js.map +1 -0
  104. package/dist/transforms/group.d.ts +32 -0
  105. package/dist/transforms/group.d.ts.map +1 -0
  106. package/dist/transforms/group.js +419 -0
  107. package/dist/transforms/group.js.map +1 -0
  108. package/dist/transforms/interval.d.ts +24 -0
  109. package/dist/transforms/interval.d.ts.map +1 -0
  110. package/dist/transforms/interval.js +104 -0
  111. package/dist/transforms/interval.js.map +1 -0
  112. package/dist/transforms/map.d.ts +48 -0
  113. package/dist/transforms/map.d.ts.map +1 -0
  114. package/dist/transforms/map.js +192 -0
  115. package/dist/transforms/map.js.map +1 -0
  116. package/dist/transforms/select.d.ts +9 -0
  117. package/dist/transforms/select.d.ts.map +1 -0
  118. package/dist/transforms/select.js +50 -0
  119. package/dist/transforms/select.js.map +1 -0
  120. package/dist/transforms/stack.d.ts +23 -0
  121. package/dist/transforms/stack.d.ts.map +1 -0
  122. package/dist/transforms/stack.js +306 -0
  123. package/dist/transforms/stack.js.map +1 -0
  124. package/dist/types.d.ts +369 -0
  125. package/dist/types.d.ts.map +1 -0
  126. package/dist/types.js +11 -0
  127. package/dist/types.js.map +1 -0
  128. package/package.json +70 -4
  129. package/README.md +0 -3
@@ -0,0 +1,948 @@
1
+ /** Mark → ECharts series compilers. */
2
+ import { formatValue, isMissing, keyof, toNumber } from "../channel.js";
3
+ import { fadeGradient, inkOn, withOpacity } from "./palette.js";
4
+ import { coord } from "./scales.js";
5
+ /** Split a facet's indices into series by the `z` channel (order of first appearance). */
6
+ export function groupSeries(rm, I) {
7
+ const Z = rm.channels.z ?? rm.channels.fill ?? rm.channels.stroke;
8
+ const colorIsContinuous = rm.channels.z === undefined && !rm.categoricalColor && ((rm.channels.fill && isNumeric(rm.channels.fill)) || (rm.channels.stroke && isNumeric(rm.channels.stroke)));
9
+ if (!Z || colorIsContinuous)
10
+ return [{ key: null, value: undefined, index: I.slice() }];
11
+ const groups = new Map();
12
+ for (const i of I) {
13
+ const k = keyof(Z[i]);
14
+ let g = groups.get(k);
15
+ if (!g)
16
+ groups.set(k, (g = { key: k, value: Z[i], index: [] }));
17
+ g.index.push(i);
18
+ }
19
+ return [...groups.values()];
20
+ }
21
+ function isNumeric(v) {
22
+ for (const x of v) {
23
+ if (x === null || x === undefined)
24
+ continue;
25
+ return typeof x === "number";
26
+ }
27
+ return false;
28
+ }
29
+ const curveSmooth = { basis: 0.5, natural: 0.4, "catmull-rom": 0.5, "monotone-x": 0.3, "bump-x": 0.5, linear: false };
30
+ const curveStep = { step: "middle", "step-before": "start", "step-after": "end" };
31
+ function dashType(o) {
32
+ const d = o.strokeDasharray;
33
+ if (!d)
34
+ return "solid";
35
+ if (Array.isArray(d))
36
+ return d;
37
+ const parts = String(d).split(/[\s,]+/).map(Number);
38
+ if (parts.length === 1)
39
+ return parts[0] <= 2 ? "dotted" : "dashed";
40
+ return parts;
41
+ }
42
+ function seriesName(rm, g) {
43
+ const o = rm.mark.options;
44
+ if (g.value !== undefined)
45
+ return formatValue(g.value);
46
+ return o.name ?? rm.labels.y ?? rm.labels.x ?? rm.mark.id;
47
+ }
48
+ function colorFor(rm, g, ctx, prefer) {
49
+ const o = rm.mark.options;
50
+ const c = rm.constants[prefer] ?? rm.constants[prefer === "fill" ? "stroke" : "fill"];
51
+ if (c !== undefined)
52
+ return c;
53
+ if (g.value !== undefined && ctx.scales.color.kind === "categorical")
54
+ return ctx.scales.color.colorOf(g.value);
55
+ if (o.id && rm.mark.generated)
56
+ return undefined;
57
+ return undefined;
58
+ }
59
+ function applySeriesStyle(series, rm, g, ctx) {
60
+ const st = ctx.seriesStyles.get(rm.mark.id)?.get(g.key);
61
+ if (!st)
62
+ return;
63
+ if (st.color) {
64
+ series.itemStyle = { ...(series.itemStyle ?? {}), color: st.color };
65
+ if (series.lineStyle)
66
+ series.lineStyle = { ...series.lineStyle, color: st.color };
67
+ if (series.areaStyle)
68
+ series.areaStyle = { ...series.areaStyle, color: st.color };
69
+ }
70
+ if (st.opacity !== undefined) {
71
+ if (series.lineStyle)
72
+ series.lineStyle = { ...series.lineStyle, opacity: st.opacity };
73
+ if (series.areaStyle)
74
+ series.areaStyle = { ...series.areaStyle, opacity: (series.areaStyle.opacity ?? 1) * st.opacity };
75
+ series.itemStyle = { ...(series.itemStyle ?? {}), opacity: st.opacity };
76
+ }
77
+ if (st.width !== undefined && series.lineStyle)
78
+ series.lineStyle = { ...series.lineStyle, width: st.width };
79
+ if (st.dashed && series.lineStyle)
80
+ series.lineStyle = { ...series.lineStyle, type: "dashed" };
81
+ series.z = (series.z ?? 2) + (st.width ? 1 : 0);
82
+ }
83
+ function baseSeries(rm, g, ctx, idx) {
84
+ const o = rm.mark.options;
85
+ return {
86
+ id: `${rm.mark.id}:${ctx.facet}:${idx}`,
87
+ name: seriesName(rm, g),
88
+ xAxisIndex: ctx.facet,
89
+ yAxisIndex: ctx.facet,
90
+ z: 2 + (o.z2 ?? 0),
91
+ silent: Boolean(o.silent),
92
+ animation: ctx.animation,
93
+ ...(o.tip === false || o.silent ? { tooltip: { show: false } } : {}),
94
+ ...(o.echarts ?? {}),
95
+ };
96
+ }
97
+ /** Row values for the tooltip: `title` and extra channels. */
98
+ function tooltipDims(rm) {
99
+ const names = [];
100
+ const values = [];
101
+ if (rm.channels.title) {
102
+ names.push("title");
103
+ values.push(rm.channels.title);
104
+ }
105
+ for (const [k, v] of Object.entries(rm.extra)) {
106
+ names.push(k);
107
+ values.push(v);
108
+ }
109
+ return { names, values };
110
+ }
111
+ function withHighlight(item, rm, i, ctx) {
112
+ const h = ctx.highlights.get(rm.mark.id)?.get(i);
113
+ if (!h)
114
+ return item;
115
+ const value = Array.isArray(item) ? item : item.value;
116
+ const out = Array.isArray(item) ? { value } : { ...item };
117
+ if (h.color)
118
+ out.itemStyle = { ...(out.itemStyle ?? {}), color: h.color, borderColor: h.color };
119
+ if (h.border)
120
+ out.itemStyle = { ...(out.itemStyle ?? {}), borderColor: h.border, borderWidth: 2 };
121
+ if (h.size)
122
+ out.symbolSize = h.size;
123
+ if (h.label)
124
+ out.label = { show: true, formatter: h.label, position: "top", color: h.color ?? ctx.theme.insight.anomaly, fontWeight: "bold" };
125
+ return out;
126
+ }
127
+ // ------------------------------------------------------------------ line ---
128
+ function compileLine(rm, I, ctx) {
129
+ const o = rm.mark.options;
130
+ const { x: xs, y: ys } = ctx.scales;
131
+ const X = rm.channels.x;
132
+ const Y = rm.channels.y;
133
+ const series = [];
134
+ const legend = [];
135
+ const colorDim = continuousColorDim(rm);
136
+ groupSeries(rm, I).forEach((g, k) => {
137
+ const color = colorFor(rm, g, ctx, "stroke");
138
+ const data = g.index.map((i) => {
139
+ const row = [coord(xs, X[i]), coord(ys, Y[i])];
140
+ if (colorDim)
141
+ row.push(toNumber(colorDim[i]));
142
+ return withHighlight(row, rm, i, ctx);
143
+ });
144
+ const s = {
145
+ ...baseSeries(rm, g, ctx, k),
146
+ type: "line",
147
+ data,
148
+ showSymbol: Boolean(o.symbols) || data.length === 1,
149
+ symbol: o.symbol ?? "circle",
150
+ symbolSize: o.r !== undefined && typeof o.r === "number" ? o.r * 2 : ctx.theme.mark.dotRadius * 2,
151
+ connectNulls: Boolean(o.connectNulls),
152
+ lineStyle: { width: o.strokeWidth ?? ctx.theme.mark.lineWidth, type: dashType(o), cap: "round", join: "round", opacity: o.strokeOpacity ?? o.opacity ?? 1, ...(color ? { color } : {}) },
153
+ itemStyle: { ...(color ? { color } : {}), borderColor: ctx.theme.background, borderWidth: ctx.theme.mark.gap },
154
+ emphasis: { focus: "series", lineStyle: { width: (o.strokeWidth ?? ctx.theme.mark.lineWidth) + 1 } },
155
+ blur: { lineStyle: { opacity: 0.25 } },
156
+ smooth: o.curve ? (curveSmooth[o.curve] ?? false) : false,
157
+ ...(o.curve && curveStep[o.curve] ? { step: curveStep[o.curve] } : {}),
158
+ ...(data.length > 5000 ? { sampling: "lttb" } : {}),
159
+ };
160
+ if (o.gradient && color && typeof color === "string" && !colorDim)
161
+ s.lineStyle.color = Array.isArray(o.gradient) ? horizontalGradient(o.gradient) : color;
162
+ if (colorDim)
163
+ ctx.visualTargets.push({ seriesId: s.id, dimension: 2 });
164
+ applySeriesStyle(s, rm, g, ctx);
165
+ series.push(s);
166
+ if (!rm.mark.generated && o.legend !== false && (g.value !== undefined || o.name))
167
+ legend.push(s.name);
168
+ });
169
+ return { series, legend, tooltip: "axis" };
170
+ }
171
+ function continuousColorDim(rm) {
172
+ if (rm.channels.z !== undefined || rm.categoricalColor)
173
+ return undefined;
174
+ const c = rm.channels.fill ?? rm.channels.stroke;
175
+ return c && isNumeric(c) ? c : undefined;
176
+ }
177
+ function horizontalGradient(colors) {
178
+ return { type: "linear", x: 0, y: 0, x2: 1, y2: 0, colorStops: colors.map((c, i) => ({ offset: colors.length === 1 ? 0 : i / (colors.length - 1), color: c })) };
179
+ }
180
+ // ------------------------------------------------------------------ area ---
181
+ /** Decide how to render stacked bands: native ECharts stacking or explicit base+band pairs. */
182
+ function stackMode(rm, groups) {
183
+ const st = rm.mark.options.__stacked;
184
+ if (groups.length <= 1)
185
+ return "single";
186
+ if (!st)
187
+ return "bands";
188
+ if (st.offset === null || st.offset === undefined || st.offset === "normalize") {
189
+ const order = st.order;
190
+ if (order === undefined || order === null || order === "sum" || order === "appearance" || order === "inside-out" || Array.isArray(order))
191
+ return "native";
192
+ }
193
+ return "bands";
194
+ }
195
+ /** Sort groups by their stacking position so native stacks accumulate in the right order. */
196
+ function sortGroupsByBase(groups, Y1) {
197
+ const pos = (g) => {
198
+ let s = 0;
199
+ let n = 0;
200
+ for (const i of g.index) {
201
+ const v = toNumber(Y1[i]);
202
+ if (Number.isFinite(v)) {
203
+ s += Math.abs(v);
204
+ n++;
205
+ }
206
+ }
207
+ return n ? s / n : 0;
208
+ };
209
+ return groups.slice().sort((a, b) => pos(a) - pos(b));
210
+ }
211
+ function isZeroBase(Y1, index) {
212
+ for (const i of index) {
213
+ const v = toNumber(Y1[i]);
214
+ if (Number.isFinite(v) && Math.abs(v) > 1e-12)
215
+ return false;
216
+ }
217
+ return true;
218
+ }
219
+ function compileArea(rm, I, ctx, horizontal) {
220
+ const o = rm.mark.options;
221
+ const { x: xs, y: ys } = ctx.scales;
222
+ // Position channel (x for areaY) and band channels (y1/y2 for areaY).
223
+ const P = horizontal ? rm.channels.y : rm.channels.x;
224
+ const B1 = horizontal ? rm.channels.x1 : rm.channels.y1;
225
+ const B2 = horizontal ? (rm.channels.x2 ?? rm.channels.x) : (rm.channels.y2 ?? rm.channels.y);
226
+ if (!B2)
227
+ return { series: [], legend: [], tooltip: "axis" };
228
+ const ps = horizontal ? ys : xs;
229
+ const bs = horizontal ? xs : ys;
230
+ let groups = groupSeries(rm, I);
231
+ const mode = stackMode(rm, groups);
232
+ if (mode === "native" && B1)
233
+ groups = sortGroupsByBase(groups, B1);
234
+ const multi = groups.length > 1;
235
+ const series = [];
236
+ const legend = [];
237
+ const stroke = rm.constants.stroke;
238
+ groups.forEach((g, k) => {
239
+ const color = colorFor(rm, g, ctx, "fill");
240
+ const fillOpacity = o.fillOpacity ?? o.opacity ?? (multi ? ctx.theme.mark.stackOpacity : ctx.theme.mark.areaOpacity);
241
+ const areaColor = o.gradient && typeof color === "string" ? (Array.isArray(o.gradient) ? verticalGradient(o.gradient) : multi ? fadeGradient(color, 0.95, 0.55) : fadeGradient(color, Math.max(0.45, fillOpacity * 3), 0.03)) : color;
242
+ const pair = (i, v) => (horizontal ? [coord(bs, v), coord(ps, P[i])] : [coord(ps, P[i]), coord(bs, v)]);
243
+ const lineStyle = stroke
244
+ ? { width: o.strokeWidth ?? ctx.theme.mark.lineWidth, color: stroke, opacity: o.strokeOpacity ?? 1, type: dashType(o), cap: "round", join: "round" }
245
+ : multi && mode === "native"
246
+ ? { width: ctx.theme.mark.gap / 2, color: ctx.theme.background, opacity: 1 }
247
+ : { width: 0, opacity: 0 };
248
+ const common = {
249
+ type: "line",
250
+ showSymbol: false,
251
+ connectNulls: Boolean(o.connectNulls),
252
+ smooth: o.curve ? (curveSmooth[o.curve] ?? false) : false,
253
+ ...(o.curve && curveStep[o.curve] ? { step: curveStep[o.curve] } : {}),
254
+ emphasis: { focus: "series" },
255
+ };
256
+ const zeroBase = !B1 || isZeroBase(B1, g.index);
257
+ if (mode === "native" || zeroBase) {
258
+ const data = g.index.map((i) => {
259
+ const v2 = toNumber(B2[i]);
260
+ const v1 = B1 ? toNumber(B1[i]) : 0;
261
+ const h = mode === "native" ? (Number.isFinite(v2) && Number.isFinite(v1) ? v2 - v1 : NaN) : v2;
262
+ return withHighlight(pair(i, Number.isFinite(h) ? h : null), rm, i, ctx);
263
+ });
264
+ const s = {
265
+ ...baseSeries(rm, g, ctx, k),
266
+ ...common,
267
+ data,
268
+ ...(mode === "native" ? { stack: rm.mark.id, stackStrategy: "samesign" } : {}),
269
+ lineStyle: { ...lineStyle, ...(color && !stroke && !(multi && mode === "native") ? { color } : {}) },
270
+ areaStyle: { color: areaColor, opacity: o.gradient ? 1 : fillOpacity, origin: "auto" },
271
+ itemStyle: color ? { color } : {},
272
+ emphasis: { focus: "series" },
273
+ blur: { areaStyle: { opacity: 0.2 } },
274
+ };
275
+ applySeriesStyle(s, rm, g, ctx);
276
+ series.push(s);
277
+ }
278
+ else {
279
+ // Explicit band between y1 and y2: an exact polygon (no stacking, so the axis extent is untouched).
280
+ const pos = g.index.map((i) => coord(ps, P[i]));
281
+ const lo = g.index.map((i) => toNumber(B1[i]));
282
+ const hi = g.index.map((i) => toNumber(B2[i]));
283
+ const band = bandSeries(baseSeries(rm, g, ctx, k), pos, lo, hi, horizontal, { color: areaColor ?? color, opacity: o.gradient ? 1 : fillOpacity });
284
+ applySeriesStyle(band, rm, g, ctx);
285
+ series.push(band);
286
+ if (stroke) {
287
+ series.push({
288
+ ...baseSeries(rm, g, ctx, k),
289
+ ...common,
290
+ id: `${rm.mark.id}:${ctx.facet}:${k}:edge`,
291
+ data: g.index.map((i) => pair(i, Number.isFinite(toNumber(B2[i])) ? toNumber(B2[i]) : null)),
292
+ lineStyle,
293
+ itemStyle: { color: stroke },
294
+ silent: true,
295
+ tooltip: { show: false },
296
+ });
297
+ }
298
+ }
299
+ if (!rm.mark.generated && o.legend !== false && (g.value !== undefined || o.name))
300
+ legend.push(seriesName(rm, g));
301
+ });
302
+ return { series, legend, tooltip: "axis" };
303
+ }
304
+ function verticalGradient(colors) {
305
+ return { type: "linear", x: 0, y: 0, x2: 0, y2: 1, colorStops: colors.map((c, i) => ({ offset: colors.length === 1 ? 0 : i / (colors.length - 1), color: c })) };
306
+ }
307
+ /**
308
+ * A filled band between two edges as a custom polygon series. Gaps (NaN on
309
+ * either edge) split the band into separate polygons. `pos` are axis
310
+ * coordinates along the independent axis; `lo`/`hi` the dependent values.
311
+ */
312
+ export function bandSeries(base, pos, lo, hi, horizontal, style) {
313
+ const data = pos.map((p, j) => [p, Number.isFinite(lo[j]) ? lo[j] : null, Number.isFinite(hi[j]) ? hi[j] : null]);
314
+ return {
315
+ ...base,
316
+ type: "custom",
317
+ data,
318
+ encode: horizontal ? { x: [1, 2], y: 0 } : { x: 0, y: [1, 2] },
319
+ clip: true,
320
+ silent: base.silent ?? true,
321
+ tooltip: { show: false },
322
+ itemStyle: { color: style.color, opacity: style.opacity },
323
+ renderItem: (params, api) => {
324
+ if (params.dataIndex !== 0)
325
+ return null;
326
+ const n = params.dataInsideLength ?? data.length;
327
+ const polygons = [];
328
+ let top = [];
329
+ let bottom = [];
330
+ const flush = () => {
331
+ if (top.length > 1)
332
+ polygons.push([...top, ...bottom.reverse()]);
333
+ top = [];
334
+ bottom = [];
335
+ };
336
+ for (let i = 0; i < n; i++) {
337
+ const p = api.value(0, i);
338
+ const a = api.value(1, i);
339
+ const b = api.value(2, i);
340
+ if (p === null || p === undefined || Number.isNaN(p) || a === null || b === null || Number.isNaN(a) || Number.isNaN(b)) {
341
+ flush();
342
+ continue;
343
+ }
344
+ top.push(horizontal ? api.coord([b, p]) : api.coord([p, b]));
345
+ bottom.push(horizontal ? api.coord([a, p]) : api.coord([p, a]));
346
+ }
347
+ flush();
348
+ if (polygons.length === 0)
349
+ return null;
350
+ const fill = api.visual("color");
351
+ const children = polygons.map((points) => ({ type: "polygon", shape: { points }, style: { fill, opacity: api.visual("opacity") ?? style.opacity, stroke: "none" } }));
352
+ return children.length === 1 ? children[0] : { type: "group", children };
353
+ },
354
+ };
355
+ }
356
+ // ------------------------------------------------------------------- bar ---
357
+ function compileBar(rm, I, ctx, horizontal) {
358
+ const o = rm.mark.options;
359
+ const { x: xs, y: ys } = ctx.scales;
360
+ const P = horizontal ? rm.channels.y : rm.channels.x;
361
+ const B1 = horizontal ? rm.channels.x1 : rm.channels.y1;
362
+ const B2 = horizontal ? (rm.channels.x2 ?? rm.channels.x) : (rm.channels.y2 ?? rm.channels.y);
363
+ const ps = horizontal ? ys : xs;
364
+ const bs = horizontal ? xs : ys;
365
+ if (!B2)
366
+ return { series: [], legend: [], tooltip: "axis" };
367
+ let groups = groupSeries(rm, I);
368
+ const dodge = Boolean(o.dodge);
369
+ const mode = dodge ? "single" : stackMode(rm, groups);
370
+ if (mode === "native" && B1)
371
+ groups = sortGroupsByBase(groups, B1);
372
+ const series = [];
373
+ const legend = [];
374
+ const colorDim = continuousColorDim(rm);
375
+ const bandsUsed = groups.some((g) => mode === "bands" && B1 && !isZeroBase(B1, g.index));
376
+ const pair = (i, v) => (horizontal ? [v, coord(ps, P[i])] : [coord(ps, P[i]), v]);
377
+ groups.forEach((g, k) => {
378
+ const color = colorFor(rm, g, ctx, "fill");
379
+ const zeroBase = !B1 || isZeroBase(B1, g.index);
380
+ const label = o.label ? { show: true, position: horizontal ? "right" : "top", formatter: typeof o.label === "string" ? o.label : undefined } : undefined;
381
+ const stackedMark = mode !== "single" && !dodge && groups.length > 1;
382
+ const r = ctx.theme.mark.barRadius;
383
+ const radius = o.borderRadius ?? (stackedMark ? 0 : horizontal ? [0, r, r, 0] : [r, r, 0, 0]);
384
+ const itemStyle = { ...(color ? { color } : {}), opacity: o.fillOpacity ?? o.opacity ?? 1, borderRadius: radius };
385
+ if (rm.constants.stroke) {
386
+ itemStyle.borderColor = rm.constants.stroke;
387
+ itemStyle.borderWidth = o.strokeWidth ?? 1;
388
+ }
389
+ else if (stackedMark) {
390
+ // The surface gap between stacked segments.
391
+ itemStyle.borderColor = ctx.theme.background;
392
+ itemStyle.borderWidth = ctx.theme.mark.gap / 2;
393
+ }
394
+ const common = {
395
+ type: "bar",
396
+ barCategoryGap: `${Math.round((ps.options.padding ?? 0.3) * 100)}%`,
397
+ barMaxWidth: o.barMaxWidth ?? ctx.theme.mark.barMaxWidth,
398
+ ...(bandsUsed ? { barGap: "-100%" } : dodge ? { barGap: "12%" } : {}),
399
+ label,
400
+ itemStyle,
401
+ emphasis: { focus: "series" },
402
+ blur: { itemStyle: { opacity: 0.25 } },
403
+ };
404
+ if (mode === "native" || zeroBase || dodge) {
405
+ const data = g.index.map((i) => {
406
+ const v2 = toNumber(B2[i]);
407
+ const v1 = B1 ? toNumber(B1[i]) : 0;
408
+ const h = mode === "native" ? (Number.isFinite(v2) && Number.isFinite(v1) ? v2 - v1 : NaN) : v2;
409
+ const row = pair(i, Number.isFinite(h) ? h : null);
410
+ if (colorDim)
411
+ row.push(toNumber(colorDim[i]));
412
+ return withHighlight(row, rm, i, ctx);
413
+ });
414
+ const s = { ...baseSeries(rm, g, ctx, k), ...common, data, ...(mode === "native" ? { stack: rm.mark.id, stackStrategy: "samesign" } : {}) };
415
+ if (colorDim)
416
+ ctx.visualTargets.push({ seriesId: s.id, dimension: 2 });
417
+ applySeriesStyle(s, rm, g, ctx);
418
+ series.push(s);
419
+ }
420
+ else {
421
+ // Floating bars (explicit x1/x2 or offset stacks): rects between the two edges, centred in the band.
422
+ // Native bars always grow from the axis origin, which would drag the extent to 0 (1970 on a time axis).
423
+ const data = g.index.map((i) => {
424
+ const v1 = toNumber(B1[i]);
425
+ const v2 = toNumber(B2[i]);
426
+ const row = [coord(ps, P[i]), Number.isFinite(v1) ? v1 : null, Number.isFinite(v2) ? v2 : null];
427
+ if (colorDim)
428
+ row.push(toNumber(colorDim[i]));
429
+ return withHighlight(row, rm, i, ctx);
430
+ });
431
+ const valueName = rm.labels[horizontal ? "x" : "y"] ?? "value";
432
+ const [c1, c2] = horizontal ? ["x1", "x2"] : ["y1", "y2"];
433
+ const s = {
434
+ ...baseSeries(rm, g, ctx, k),
435
+ type: "custom",
436
+ data,
437
+ dimensions: [rm.labels[horizontal ? "y" : "x"] ?? "key", rm.labels[c1] ?? `${valueName} start`, rm.labels[c2] ?? `${valueName} end`, rm.labels.fill ?? "color"],
438
+ encode: horizontal ? { y: 0, x: [1, 2], tooltip: [1, 2] } : { x: 0, y: [1, 2], tooltip: [1, 2] },
439
+ itemStyle,
440
+ clip: true,
441
+ renderItem: makeIntervalBarRenderer(horizontal, {
442
+ band: ps.type === "ordinal",
443
+ padding: ps.options.padding ?? 0.3,
444
+ maxWidth: Number(o.barMaxWidth ?? ctx.theme.mark.barMaxWidth),
445
+ radius: o.borderRadius ?? (stackedMark ? 0 : r),
446
+ label: o.label ? (typeof o.label === "string" ? o.label : true) : false,
447
+ labelColor: ctx.theme.text,
448
+ font: ctx.theme.font,
449
+ }),
450
+ emphasis: { focus: "series" },
451
+ blur: { itemStyle: { opacity: 0.25 } },
452
+ };
453
+ if (colorDim)
454
+ ctx.visualTargets.push({ seriesId: s.id, dimension: 3 });
455
+ applySeriesStyle(s, rm, g, ctx);
456
+ series.push(s);
457
+ }
458
+ if (!rm.mark.generated && o.legend !== false && (g.value !== undefined || o.name))
459
+ legend.push(seriesName(rm, g));
460
+ });
461
+ return { series, legend, tooltip: "axis" };
462
+ }
463
+ // ------------------------------------------------------------------ rect ---
464
+ /** Rectangles with quantitative edges on both axes, drawn with a custom series. */
465
+ function compileRect(rm, I, ctx) {
466
+ const o = rm.mark.options;
467
+ const { x: xs, y: ys } = ctx.scales;
468
+ const X1 = rm.channels.x1;
469
+ const X2 = rm.channels.x2;
470
+ const Y1 = rm.channels.y1;
471
+ const Y2 = rm.channels.y2 ?? rm.channels.y;
472
+ const ordinalX = xs.type === "ordinal";
473
+ const ordinalY = ys.type === "ordinal";
474
+ // Degrade to bars when one axis is ordinal.
475
+ if (ordinalX && !ordinalY && (X1 === undefined || X2 === undefined))
476
+ return compileBar(rm, I, ctx, false);
477
+ if (ordinalY && !ordinalX && (Y1 === undefined || Y2 === undefined))
478
+ return compileBar(rm, I, ctx, true);
479
+ if (!X1 || !X2 || !Y2)
480
+ return { series: [], legend: [], tooltip: "item" };
481
+ const colorDim = continuousColorDim(rm);
482
+ const inset = o.inset ?? ctx.theme.mark.gap / 2;
483
+ const series = [];
484
+ const legend = [];
485
+ const { names: tipNames, values: tipValues } = tooltipDims(rm);
486
+ groupSeries(rm, I).forEach((g, k) => {
487
+ const color = colorFor(rm, g, ctx, "fill");
488
+ const data = g.index.map((i) => {
489
+ const row = {
490
+ value: [
491
+ coord(xs, X1[i]),
492
+ coord(xs, X2[i]),
493
+ Y1 ? (coord(ys, Y1[i]) ?? 0) : 0,
494
+ coord(ys, Y2[i]),
495
+ colorDim ? toNumber(colorDim[i]) : null,
496
+ ...tipValues.map((v) => (isMissing(v[i]) ? null : formatValue(v[i]))),
497
+ ],
498
+ };
499
+ const h = ctx.highlights.get(rm.mark.id)?.get(i);
500
+ if (h?.color)
501
+ row.itemStyle = { color: h.color };
502
+ return row;
503
+ });
504
+ const s = {
505
+ ...baseSeries(rm, g, ctx, k),
506
+ type: "custom",
507
+ data,
508
+ encode: { x: [0, 1], y: [2, 3], tooltip: colorDim ? [4] : [3], ...(tipNames.length ? { itemName: 5 } : {}) },
509
+ dimensions: ["x1", "x2", "y1", "y2", rm.labels.fill ?? "value", ...tipNames],
510
+ itemStyle: { ...(color ? { color } : {}), opacity: o.fillOpacity ?? o.opacity ?? 1, ...(rm.constants.stroke ? { borderColor: rm.constants.stroke, borderWidth: o.strokeWidth ?? 1 } : {}) },
511
+ clip: true,
512
+ renderItem: makeRectRenderer(inset, Boolean(colorDim)),
513
+ emphasis: { focus: "series" },
514
+ };
515
+ if (colorDim)
516
+ ctx.visualTargets.push({ seriesId: s.id, dimension: 4 });
517
+ applySeriesStyle(s, rm, g, ctx);
518
+ series.push(s);
519
+ if (!rm.mark.generated && o.legend !== false && (g.value !== undefined || o.name))
520
+ legend.push(s.name);
521
+ });
522
+ return { series, legend, tooltip: "item" };
523
+ }
524
+ /** A bar spanning value dims 1–2, centred on position dim 0 and sized to the band like a native bar. */
525
+ function makeIntervalBarRenderer(horizontal, st) {
526
+ return (_params, api) => {
527
+ const p = api.value(0);
528
+ const a = api.value(1);
529
+ const b = api.value(2);
530
+ if ([p, a, b].some((v) => v === null || v === undefined || Number.isNaN(v)))
531
+ return null;
532
+ // Off a band axis there is no band to fill, so fall back to a fixed thickness.
533
+ const band = st.band ? api.size(horizontal ? [0, 1] : [1, 0])[horizontal ? 1 : 0] : 16;
534
+ const thick = Math.max(1, Math.min(band * (1 - st.padding), st.maxWidth));
535
+ const pa = api.coord(horizontal ? [a, p] : [p, a]);
536
+ const pb = api.coord(horizontal ? [b, p] : [p, b]);
537
+ const shape = horizontal
538
+ ? { x: Math.min(pa[0], pb[0]), y: pa[1] - thick / 2, width: Math.abs(pb[0] - pa[0]), height: thick, r: st.radius }
539
+ : { x: pa[0] - thick / 2, y: Math.min(pa[1], pb[1]), width: thick, height: Math.abs(pb[1] - pa[1]), r: st.radius };
540
+ const length = formatValue(b - a);
541
+ const text = st.label === false ? undefined : typeof st.label === "string" ? st.label.replace(/\{c\}/g, length) : length;
542
+ return {
543
+ type: "rect",
544
+ shape,
545
+ style: { fill: api.visual("color"), opacity: api.visual("opacity") ?? 1, stroke: api.visual("borderColor"), lineWidth: api.visual("borderWidth") ?? 0 },
546
+ ...(text === undefined
547
+ ? {}
548
+ : { textContent: { type: "text", style: { text, fill: st.labelColor, fontSize: 11, fontFamily: st.font } }, textConfig: { position: horizontal ? "right" : "top", distance: 4 } }),
549
+ };
550
+ };
551
+ }
552
+ function makeRectRenderer(inset, visual) {
553
+ return (params, api) => {
554
+ const x1 = api.value(0);
555
+ const x2 = api.value(1);
556
+ const y1 = api.value(2);
557
+ const y2 = api.value(3);
558
+ if ([x1, x2, y1, y2].some((v) => v === null || v === undefined || Number.isNaN(v)))
559
+ return null;
560
+ const a = api.coord([x1, y2]);
561
+ const b = api.coord([x2, y1]);
562
+ const left = Math.min(a[0], b[0]) + inset;
563
+ const top = Math.min(a[1], b[1]) + inset;
564
+ const width = Math.max(0, Math.abs(b[0] - a[0]) - 2 * inset);
565
+ const height = Math.max(0, Math.abs(b[1] - a[1]) - 2 * inset);
566
+ const style = { fill: api.visual("color"), opacity: api.visual("opacity") ?? 1, stroke: api.visual("borderColor"), lineWidth: api.visual("borderWidth") ?? 0 };
567
+ void visual;
568
+ return { type: "rect", shape: { x: left, y: top, width, height }, style, emphasis: { style: { opacity: 0.8 } } };
569
+ };
570
+ }
571
+ // ------------------------------------------------------------------ cell ---
572
+ /** Cells with a continuous fill: an ECharts heatmap coloured by the visualMap. */
573
+ function compileCell(rm, I, ctx) {
574
+ const o = rm.mark.options;
575
+ const { x: xs, y: ys } = ctx.scales;
576
+ const X = rm.channels.x;
577
+ const Y = rm.channels.y;
578
+ const fill = rm.channels.fill ?? rm.channels.stroke;
579
+ // ECharts heatmaps only colour through a visualMap, so categorical/constant cells are drawn as custom rects.
580
+ if (!fill || !isNumeric(fill) || rm.categoricalColor)
581
+ return compileCategoricalCell(rm, I, ctx);
582
+ const { names: tipNames, values: tipValues } = tooltipDims(rm);
583
+ const data = I.map((i) => {
584
+ const row = { value: [coord(xs, X[i]), coord(ys, Y[i]), toNumber(fill[i]), ...tipValues.map((v) => formatValue(v[i]))] };
585
+ const cellColor = ctx.scales.color.colorOf(fill[i]);
586
+ if (o.label && cellColor)
587
+ row.label = { color: inkOn(cellColor) };
588
+ const h = ctx.highlights.get(rm.mark.id)?.get(i);
589
+ if (h?.color)
590
+ row.itemStyle = { borderColor: h.color, borderWidth: 2 };
591
+ return row;
592
+ });
593
+ const g = { key: null, value: undefined, index: I };
594
+ const s = {
595
+ ...baseSeries(rm, g, ctx, 0),
596
+ type: "heatmap",
597
+ data,
598
+ dimensions: ["x", "y", rm.labels.fill ?? "value", ...tipNames],
599
+ encode: { x: 0, y: 1, value: 2, tooltip: [2, ...tipNames.map((_, j) => 3 + j)] },
600
+ label: o.label ? { show: true, fontSize: 11, fontFamily: ctx.theme.font, formatter: (p) => formatValue(p.value[2]) } : { show: false },
601
+ itemStyle: { borderColor: ctx.theme.background, borderWidth: o.inset ?? ctx.theme.mark.gap, borderRadius: 3, opacity: o.fillOpacity ?? o.opacity ?? 1 },
602
+ emphasis: { itemStyle: { shadowBlur: 6, shadowColor: "rgba(0,0,0,0.3)" } },
603
+ };
604
+ ctx.visualTargets.push({ seriesId: s.id, dimension: 2 });
605
+ return { series: [s], legend: [], tooltip: "item" };
606
+ }
607
+ function compileCategoricalCell(rm, I, ctx) {
608
+ const o = rm.mark.options;
609
+ const { x: xs, y: ys } = ctx.scales;
610
+ const X = rm.channels.x;
611
+ const Y = rm.channels.y;
612
+ const fill = rm.channels.fill ?? rm.channels.stroke;
613
+ const { names: tipNames, values: tipValues } = tooltipDims(rm);
614
+ const inset = (o.inset ?? ctx.theme.mark.gap) / 2;
615
+ const series = [];
616
+ const legend = [];
617
+ groupSeries(rm, I).forEach((g, k) => {
618
+ const color = colorFor(rm, g, ctx, "fill");
619
+ const ink = typeof color === "string" ? inkOn(color) : ctx.theme.text;
620
+ const borders = g.index.map((i) => ctx.highlights.get(rm.mark.id)?.get(i)?.color);
621
+ const data = g.index.map((i) => [coord(xs, X[i]), coord(ys, Y[i]), fill ? formatValue(fill[i]) : "", ...tipValues.map((v) => formatValue(v[i]))]);
622
+ const s = {
623
+ ...baseSeries(rm, g, ctx, k),
624
+ type: "custom",
625
+ data,
626
+ dimensions: ["x", "y", rm.labels.fill ?? "value", ...tipNames],
627
+ encode: { x: 0, y: 1, tooltip: [...(fill ? [2] : []), ...tipNames.map((_, j) => 3 + j)] },
628
+ itemStyle: { ...(color ? { color } : {}), opacity: o.fillOpacity ?? o.opacity ?? 1 },
629
+ clip: true,
630
+ renderItem: (params, api) => {
631
+ const x = api.value(0);
632
+ const y = api.value(1);
633
+ if (Number.isNaN(x) || Number.isNaN(y))
634
+ return null;
635
+ const c = api.coord([x, y]);
636
+ const [w, h] = api.size([1, 1]);
637
+ const border = borders[params.dataIndex];
638
+ const text = data[params.dataIndex][2];
639
+ return {
640
+ type: "rect",
641
+ shape: { x: c[0] - w / 2 + inset, y: c[1] - h / 2 + inset, width: Math.max(0, w - 2 * inset), height: Math.max(0, h - 2 * inset), r: 3 },
642
+ style: { fill: api.visual("color"), opacity: api.visual("opacity") ?? 1, ...(border ? { stroke: border, lineWidth: 2 } : {}) },
643
+ ...(o.label && text ? { textContent: { type: "text", style: { text, fill: ink, fontSize: 11, fontFamily: ctx.theme.font } }, textConfig: { position: "inside" } } : {}),
644
+ };
645
+ },
646
+ emphasis: { focus: "series" },
647
+ };
648
+ applySeriesStyle(s, rm, g, ctx);
649
+ series.push(s);
650
+ if (!rm.mark.generated && o.legend !== false && (g.value !== undefined || o.name))
651
+ legend.push(s.name);
652
+ });
653
+ return { series, legend, tooltip: "item" };
654
+ }
655
+ // ------------------------------------------------------------------- dot ---
656
+ function compileDot(rm, I, ctx) {
657
+ const o = rm.mark.options;
658
+ const { x: xs, y: ys, r } = ctx.scales;
659
+ const X = rm.channels.x;
660
+ const Y = rm.channels.y;
661
+ if (!X && !Y)
662
+ return { series: [], legend: [], tooltip: "item" };
663
+ const R = rm.channels.r;
664
+ const colorDim = continuousColorDim(rm);
665
+ const { names: tipNames, values: tipValues } = tooltipDims(rm);
666
+ const series = [];
667
+ const legend = [];
668
+ groupSeries(rm, I).forEach((g, k) => {
669
+ const color = colorFor(rm, g, ctx, "fill") ?? colorFor(rm, g, ctx, "stroke");
670
+ const data = g.index.map((i) => {
671
+ const row = {
672
+ value: [X ? coord(xs, X[i]) : 0, Y ? coord(ys, Y[i]) : 0, R ? toNumber(R[i]) : null, colorDim ? toNumber(colorDim[i]) : null, ...tipValues.map((v) => (isMissing(v[i]) ? null : formatValue(v[i])))],
673
+ };
674
+ return withHighlight(row, rm, i, ctx);
675
+ });
676
+ const constR = typeof o.r === "number" ? o.r : undefined;
677
+ const s = {
678
+ ...baseSeries(rm, g, ctx, k),
679
+ type: "scatter",
680
+ data,
681
+ dimensions: ["x", "y", rm.labels.r ?? "r", rm.labels.fill ?? rm.labels.stroke ?? "color", ...tipNames],
682
+ encode: { x: 0, y: 1, tooltip: [0, 1, ...(R ? [2] : []), ...(colorDim ? [3] : []), ...tipNames.map((_, j) => 4 + j)] },
683
+ symbol: o.symbol ?? "circle",
684
+ symbolSize: R ? (value) => r.sizeOf(value[2]) * 2 : (constR ?? ctx.theme.mark.dotRadius) * 2,
685
+ itemStyle: {
686
+ ...(color ? { color } : {}),
687
+ opacity: o.fillOpacity ?? o.opacity ?? 1,
688
+ // Surface ring so dots stay legible where they overlap.
689
+ borderColor: ctx.theme.background,
690
+ borderWidth: ctx.theme.mark.gap / 2,
691
+ ...(rm.constants.stroke ? { borderColor: rm.constants.stroke, borderWidth: o.strokeWidth ?? 1 } : {}),
692
+ ...(rm.constants.fill === "none" ? { color: "transparent", borderColor: typeof color === "string" ? color : undefined, borderWidth: o.strokeWidth ?? 1.5 } : {}),
693
+ },
694
+ emphasis: { focus: "series", scale: 1.4 },
695
+ blur: { itemStyle: { opacity: 0.2 } },
696
+ ...(data.length > 5000 ? { large: true, largeThreshold: 5000 } : {}),
697
+ };
698
+ if (colorDim)
699
+ ctx.visualTargets.push({ seriesId: s.id, dimension: 3 });
700
+ applySeriesStyle(s, rm, g, ctx);
701
+ series.push(s);
702
+ if (!rm.mark.generated && o.legend !== false && (g.value !== undefined || o.name))
703
+ legend.push(s.name);
704
+ });
705
+ return { series, legend, tooltip: "item" };
706
+ }
707
+ // ------------------------------------------------------------------ rule ---
708
+ function compileRule(rm, I, ctx, vertical) {
709
+ const o = rm.mark.options;
710
+ const { x: xs, y: ys } = ctx.scales;
711
+ const P = vertical ? rm.channels.x : rm.channels.y; // position of the rule
712
+ const S1 = vertical ? rm.channels.y1 : rm.channels.x1;
713
+ const S2 = vertical ? (rm.channels.y2 ?? rm.channels.y) : (rm.channels.x2 ?? rm.channels.x);
714
+ if (!P)
715
+ return { series: [], legend: [], tooltip: "axis" };
716
+ const ps = vertical ? xs : ys;
717
+ const ss = vertical ? ys : xs;
718
+ const T = rm.channels.text ?? rm.channels.title;
719
+ const color = rm.constants.stroke ?? rm.constants.fill ?? (rm.channels.stroke && ctx.scales.color.kind === "categorical" ? undefined : ctx.theme.textMuted);
720
+ const lineStyle = { width: o.strokeWidth ?? 1, type: dashType(o), opacity: o.strokeOpacity ?? o.opacity ?? 1, ...(color ? { color } : {}) };
721
+ const g = { key: null, value: undefined, index: I };
722
+ if (S2 === undefined && S1 === undefined) {
723
+ // Full-span rules → markLine on a helper series.
724
+ const data = I.map((i) => {
725
+ const v = coord(ps, P[i]);
726
+ if (v === null)
727
+ return null;
728
+ const c = rm.channels.stroke ? ctx.scales.color.colorOf(rm.channels.stroke[i]) : undefined;
729
+ const item = vertical ? { xAxis: v } : { yAxis: v };
730
+ if (T && !isMissing(T[i]))
731
+ item.label = { show: true, formatter: formatValue(T[i]), position: vertical ? "insideEndTop" : "insideEndTop" };
732
+ if (c)
733
+ item.lineStyle = { color: c };
734
+ return item;
735
+ }).filter(Boolean);
736
+ const s = {
737
+ ...baseSeries(rm, g, ctx, 0),
738
+ type: "line",
739
+ data: [],
740
+ silent: true,
741
+ tooltip: { show: false },
742
+ markLine: { silent: true, symbol: "none", animation: ctx.animation, lineStyle, label: { show: Boolean(T), fontSize: 11 }, data, z: 2 + (o.z2 ?? 0) },
743
+ };
744
+ return { series: [s], legend: [], tooltip: "axis" };
745
+ }
746
+ // Segments → custom series lines.
747
+ const data = I.map((i) => {
748
+ const p = coord(ps, P[i]);
749
+ const a = S1 ? coord(ss, S1[i]) : 0;
750
+ const b = coord(ss, S2[i]);
751
+ return { value: vertical ? [p, a, b] : [a, b, p], ...(T && !isMissing(T[i]) ? { name: formatValue(T[i]) } : {}) };
752
+ });
753
+ const s = {
754
+ ...baseSeries(rm, g, ctx, 0),
755
+ type: "custom",
756
+ data,
757
+ encode: vertical ? { x: 0, y: [1, 2] } : { x: [0, 1], y: 2 },
758
+ clip: true,
759
+ renderItem: (_params, api) => {
760
+ const v = [api.value(0), api.value(1), api.value(2)];
761
+ if (v.some((x) => x === null || Number.isNaN(x)))
762
+ return null;
763
+ const p1 = vertical ? api.coord([v[0], v[1]]) : api.coord([v[0], v[2]]);
764
+ const p2 = vertical ? api.coord([v[0], v[2]]) : api.coord([v[1], v[2]]);
765
+ return { type: "line", shape: { x1: p1[0], y1: p1[1], x2: p2[0], y2: p2[1] }, style: { stroke: lineStyle.color ?? api.visual("color"), lineWidth: lineStyle.width, opacity: lineStyle.opacity, lineDash: lineStyle.type === "dashed" ? [6, 4] : lineStyle.type === "dotted" ? [2, 3] : Array.isArray(lineStyle.type) ? lineStyle.type : undefined, fill: "none" } };
766
+ },
767
+ itemStyle: { opacity: lineStyle.opacity },
768
+ };
769
+ return { series: [s], legend: [], tooltip: "item" };
770
+ }
771
+ // ------------------------------------------------------------------ text ---
772
+ function compileText(rm, I, ctx) {
773
+ const o = rm.mark.options;
774
+ const { x: xs, y: ys } = ctx.scales;
775
+ const X = rm.channels.x;
776
+ const Y = rm.channels.y;
777
+ const T = rm.channels.text;
778
+ const color = rm.constants.fill ?? rm.constants.stroke;
779
+ const data = I.filter((i) => !isMissing(T[i])).map((i) => ({
780
+ value: [X ? coord(xs, X[i]) : 0, Y ? coord(ys, Y[i]) : 0],
781
+ label: { formatter: typeof o.label === "function" ? o.label(rm.data[i], i) : formatValue(T[i]) },
782
+ ...(rm.channels.fill ? { itemStyle: { color: ctx.scales.color.colorOf(rm.channels.fill[i]) } } : {}),
783
+ }));
784
+ const g = { key: null, value: undefined, index: I };
785
+ const s = {
786
+ ...baseSeries(rm, g, ctx, 0),
787
+ type: "scatter",
788
+ data,
789
+ symbolSize: 1,
790
+ silent: o.silent ?? true,
791
+ tooltip: { show: false },
792
+ itemStyle: { color: "transparent" },
793
+ label: {
794
+ show: true,
795
+ position: o.position ?? "top",
796
+ offset: [o.dx ?? 0, o.dy ?? 0],
797
+ fontSize: o.fontSize ?? 11,
798
+ color: color ?? ctx.theme.text,
799
+ fontFamily: ctx.theme.font,
800
+ fontWeight: o.fontWeight,
801
+ textBorderColor: withOpacity(ctx.theme.background, 0.85),
802
+ textBorderWidth: 2,
803
+ },
804
+ labelLayout: { hideOverlap: true },
805
+ };
806
+ return { series: [s], legend: [], tooltip: "item" };
807
+ }
808
+ // ------------------------------------------------------------ difference ---
809
+ function compileDifference(rm, I, ctx) {
810
+ const o = rm.mark.options;
811
+ const { x: xs, y: ys } = ctx.scales;
812
+ const X1 = rm.channels.x1;
813
+ const X2 = rm.channels.x2;
814
+ const Y1 = rm.channels.y1;
815
+ const Y2 = rm.channels.y2;
816
+ const series = [];
817
+ const positive = o.positiveFill ?? ctx.theme.insight.positive;
818
+ const negative = o.negativeFill ?? ctx.theme.insight.negative;
819
+ const fillOpacity = o.fillOpacity ?? 0.5;
820
+ groupSeries(rm, I).forEach((g, k) => {
821
+ // Comparison series resampled onto the metric's x positions (needed for shiftX).
822
+ const xm = g.index.map((i) => toNumber(X2[i]));
823
+ const ym = g.index.map((i) => toNumber(Y2[i]));
824
+ const cmpPts = g.index.map((i) => [toNumber(X1[i]), toNumber(Y1[i])]).filter((p) => Number.isFinite(p[0]) && Number.isFinite(p[1])).sort((a, b) => a[0] - b[0]);
825
+ const sameX = g.index.every((i) => toNumber(X1[i]) === toNumber(X2[i]));
826
+ const yc = sameX ? g.index.map((i) => toNumber(Y1[i])) : xm.map((x) => interpolate(cmpPts, x));
827
+ // Insert crossing points so the fills meet exactly where the lines cross.
828
+ const px = [];
829
+ const pm = [];
830
+ const pc = [];
831
+ for (let j = 0; j < xm.length; j++) {
832
+ if (j > 0) {
833
+ const d0 = ym[j - 1] - yc[j - 1];
834
+ const d1 = ym[j] - yc[j];
835
+ if (Number.isFinite(d0) && Number.isFinite(d1) && d0 * d1 < 0) {
836
+ const t = d0 / (d0 - d1);
837
+ px.push(xm[j - 1] + t * (xm[j] - xm[j - 1]));
838
+ const yv = ym[j - 1] + t * (ym[j] - ym[j - 1]);
839
+ pm.push(yv);
840
+ pc.push(yv);
841
+ }
842
+ }
843
+ px.push(xm[j]);
844
+ pm.push(ym[j]);
845
+ pc.push(yc[j]);
846
+ }
847
+ const asCoord = (v) => (xs.type === "temporal" || xs.type === "quantitative" ? v : coord(xs, X2[g.index[0]]));
848
+ const posCoords = px.map((x) => asCoord(x));
849
+ const mk = (name, lo, hi, color, opacity, idx) => bandSeries({ ...baseSeries(rm, g, ctx, k), id: `${rm.mark.id}:${ctx.facet}:${k}:${idx}`, name, z: 1 }, posCoords, lo, hi, false, { color, opacity });
850
+ // Positive: metric above comparison; negative: below. Crossing points were inserted so each band collapses to zero height on the other side.
851
+ series.push(mk("positive", pc.map((c, j) => Math.min(c, pm[j])), pm, positive, o.positiveFillOpacity ?? fillOpacity, "pos"));
852
+ series.push(mk("negative", pm, pc.map((c, j) => Math.max(c, pm[j])), negative, o.negativeFillOpacity ?? fillOpacity, "neg"));
853
+ const stroke = rm.constants.stroke ?? ctx.theme.text;
854
+ series.push({
855
+ ...baseSeries(rm, g, ctx, k),
856
+ type: "line",
857
+ data: xm.map((x, j) => [asCoord(x), Number.isFinite(ym[j]) ? ym[j] : null]),
858
+ showSymbol: false,
859
+ lineStyle: { width: o.strokeWidth ?? ctx.theme.mark.lineWidth, color: stroke, opacity: o.strokeOpacity ?? 1, cap: "round", join: "round" },
860
+ itemStyle: { color: stroke },
861
+ emphasis: { focus: "series" },
862
+ });
863
+ if (!sameX || o.stroke === undefined) {
864
+ series.push({
865
+ ...baseSeries(rm, g, ctx, k),
866
+ id: `${rm.mark.id}:${ctx.facet}:${k}:cmp`,
867
+ name: `${seriesName(rm, g)} (comparison)`,
868
+ type: "line",
869
+ data: xm.map((x, j) => [asCoord(x), Number.isFinite(yc[j]) ? yc[j] : null]),
870
+ showSymbol: false,
871
+ lineStyle: { width: 1, color: stroke, opacity: 0.5, type: "dashed" },
872
+ itemStyle: { color: stroke },
873
+ tooltip: { show: false },
874
+ silent: true,
875
+ });
876
+ }
877
+ });
878
+ return { series, legend: [], tooltip: "axis" };
879
+ }
880
+ function interpolate(pts, x) {
881
+ if (pts.length === 0 || !Number.isFinite(x))
882
+ return NaN;
883
+ if (x < pts[0][0] || x > pts[pts.length - 1][0])
884
+ return NaN;
885
+ let lo = 0;
886
+ let hi = pts.length - 1;
887
+ while (hi - lo > 1) {
888
+ const mid = (lo + hi) >> 1;
889
+ if (pts[mid][0] <= x)
890
+ lo = mid;
891
+ else
892
+ hi = mid;
893
+ }
894
+ const [x0, y0] = pts[lo];
895
+ const [x1, y1] = pts[hi];
896
+ if (x1 === x0)
897
+ return y0;
898
+ return y0 + ((y1 - y0) * (x - x0)) / (x1 - x0);
899
+ }
900
+ // ---------------------------------------------------------------- driver ---
901
+ export function compileMark(rm, facet, ctx) {
902
+ const I = rm.facets[facet] ?? [];
903
+ const type = rm.mark.type;
904
+ const c = { ...ctx, facet };
905
+ switch (type) {
906
+ case "line":
907
+ case "lineY":
908
+ case "lineX":
909
+ return rm.channels.x && rm.channels.y ? compileLine(rm, I, c) : empty();
910
+ case "area":
911
+ case "areaY":
912
+ return compileArea(rm, I, c, false);
913
+ case "areaX":
914
+ return compileArea(rm, I, c, true);
915
+ case "barY":
916
+ return compileBar(rm, I, c, false);
917
+ case "barX":
918
+ return compileBar(rm, I, c, true);
919
+ case "rectY":
920
+ case "rectX":
921
+ case "rect":
922
+ return compileRect(rm, I, c);
923
+ case "cell":
924
+ return compileCell(rm, I, c);
925
+ case "dot":
926
+ return compileDot(rm, I, c);
927
+ case "ruleX":
928
+ return compileRule(rm, I, c, true);
929
+ case "ruleY":
930
+ return compileRule(rm, I, c, false);
931
+ case "text":
932
+ return compileText(rm, I, c);
933
+ case "differenceY":
934
+ case "differenceX":
935
+ return compileDifference(rm, I, c);
936
+ default:
937
+ return empty();
938
+ }
939
+ }
940
+ function empty() {
941
+ return { series: [], legend: [], tooltip: "axis" };
942
+ }
943
+ /** Used by facets/axes: does this mark draw bars (needs `boundaryGap`)? */
944
+ export function isBarLike(mark) {
945
+ return mark.type === "barY" || mark.type === "barX" || mark.type === "cell" || mark.type === "rectY" || mark.type === "rectX";
946
+ }
947
+ export { withOpacity };
948
+ //# sourceMappingURL=series.js.map