@danielmcsheehy/isopleth 0.0.0-stage → 0.1.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.
- package/dist/auto.d.ts +61 -0
- package/dist/auto.d.ts.map +1 -0
- package/dist/auto.js +213 -0
- package/dist/auto.js.map +1 -0
- package/dist/backend/index.d.ts +24 -0
- package/dist/backend/index.d.ts.map +1 -0
- package/dist/backend/index.js +42 -0
- package/dist/backend/index.js.map +1 -0
- package/dist/backend/js/index.d.ts +7 -0
- package/dist/backend/js/index.d.ts.map +1 -0
- package/dist/backend/js/index.js +33 -0
- package/dist/backend/js/index.js.map +1 -0
- package/dist/backend/js/insights.d.ts +30 -0
- package/dist/backend/js/insights.d.ts.map +1 -0
- package/dist/backend/js/insights.js +663 -0
- package/dist/backend/js/insights.js.map +1 -0
- package/dist/backend/js/kernels.d.ts +22 -0
- package/dist/backend/js/kernels.d.ts.map +1 -0
- package/dist/backend/js/kernels.js +562 -0
- package/dist/backend/js/kernels.js.map +1 -0
- package/dist/backend/js/stats.d.ts +26 -0
- package/dist/backend/js/stats.d.ts.map +1 -0
- package/dist/backend/js/stats.js +237 -0
- package/dist/backend/js/stats.js.map +1 -0
- package/dist/backend/types.d.ts +139 -0
- package/dist/backend/types.d.ts.map +1 -0
- package/dist/backend/types.js +12 -0
- package/dist/backend/types.js.map +1 -0
- package/dist/backend/wasm-wrap.d.ts +9 -0
- package/dist/backend/wasm-wrap.d.ts.map +1 -0
- package/dist/backend/wasm-wrap.js +52 -0
- package/dist/backend/wasm-wrap.js.map +1 -0
- package/dist/backend/wasm.d.ts +30 -0
- package/dist/backend/wasm.d.ts.map +1 -0
- package/dist/backend/wasm.js +29 -0
- package/dist/backend/wasm.js.map +1 -0
- package/dist/channel.d.ts +41 -0
- package/dist/channel.d.ts.map +1 -0
- package/dist/channel.js +205 -0
- package/dist/channel.js.map +1 -0
- package/dist/chart.d.ts +69 -0
- package/dist/chart.d.ts.map +1 -0
- package/dist/chart.js +68 -0
- package/dist/chart.js.map +1 -0
- package/dist/compile/index.d.ts +21 -0
- package/dist/compile/index.d.ts.map +1 -0
- package/dist/compile/index.js +350 -0
- package/dist/compile/index.js.map +1 -0
- package/dist/compile/palette.d.ts +41 -0
- package/dist/compile/palette.d.ts.map +1 -0
- package/dist/compile/palette.js +102 -0
- package/dist/compile/palette.js.map +1 -0
- package/dist/compile/resolve.d.ts +39 -0
- package/dist/compile/resolve.d.ts.map +1 -0
- package/dist/compile/resolve.js +154 -0
- package/dist/compile/resolve.js.map +1 -0
- package/dist/compile/scales.d.ts +47 -0
- package/dist/compile/scales.d.ts.map +1 -0
- package/dist/compile/scales.js +341 -0
- package/dist/compile/scales.js.map +1 -0
- package/dist/compile/series.d.ts +65 -0
- package/dist/compile/series.d.ts.map +1 -0
- package/dist/compile/series.js +868 -0
- package/dist/compile/series.js.map +1 -0
- package/dist/editor/index.d.ts +46 -0
- package/dist/editor/index.d.ts.map +1 -0
- package/dist/editor/index.js +342 -0
- package/dist/editor/index.js.map +1 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/insights/index.d.ts +21 -0
- package/dist/insights/index.d.ts.map +1 -0
- package/dist/insights/index.js +461 -0
- package/dist/insights/index.js.map +1 -0
- package/dist/interval.d.ts +19 -0
- package/dist/interval.d.ts.map +1 -0
- package/dist/interval.js +166 -0
- package/dist/interval.js.map +1 -0
- package/dist/marks.d.ts +62 -0
- package/dist/marks.d.ts.map +1 -0
- package/dist/marks.js +176 -0
- package/dist/marks.js.map +1 -0
- package/dist/render.d.ts +21 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +99 -0
- package/dist/render.js.map +1 -0
- package/dist/spec.d.ts +56 -0
- package/dist/spec.d.ts.map +1 -0
- package/dist/spec.js +191 -0
- package/dist/spec.js.map +1 -0
- package/dist/theme.d.ts +82 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/theme.js +136 -0
- package/dist/theme.js.map +1 -0
- package/dist/transforms/basic.d.ts +35 -0
- package/dist/transforms/basic.d.ts.map +1 -0
- package/dist/transforms/basic.js +174 -0
- package/dist/transforms/basic.js.map +1 -0
- package/dist/transforms/group.d.ts +32 -0
- package/dist/transforms/group.d.ts.map +1 -0
- package/dist/transforms/group.js +419 -0
- package/dist/transforms/group.js.map +1 -0
- package/dist/transforms/interval.d.ts +24 -0
- package/dist/transforms/interval.d.ts.map +1 -0
- package/dist/transforms/interval.js +104 -0
- package/dist/transforms/interval.js.map +1 -0
- package/dist/transforms/map.d.ts +48 -0
- package/dist/transforms/map.d.ts.map +1 -0
- package/dist/transforms/map.js +192 -0
- package/dist/transforms/map.js.map +1 -0
- package/dist/transforms/select.d.ts +9 -0
- package/dist/transforms/select.d.ts.map +1 -0
- package/dist/transforms/select.js +50 -0
- package/dist/transforms/select.js.map +1 -0
- package/dist/transforms/stack.d.ts +23 -0
- package/dist/transforms/stack.d.ts.map +1 -0
- package/dist/transforms/stack.js +306 -0
- package/dist/transforms/stack.js.map +1 -0
- package/dist/types.d.ts +369 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +11 -0
- package/dist/types.js.map +1 -0
- package/package.json +65 -4
- package/README.md +0 -3
|
@@ -0,0 +1,868 @@
|
|
|
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.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)
|
|
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
|
+
const stackId = `${rm.mark.id}:${ctx.facet}:${k}:band`;
|
|
422
|
+
const base = {
|
|
423
|
+
...baseSeries(rm, g, ctx, k),
|
|
424
|
+
...common,
|
|
425
|
+
id: `${rm.mark.id}:${ctx.facet}:${k}:base`,
|
|
426
|
+
name: `${seriesName(rm, g)} (base)`,
|
|
427
|
+
data: g.index.map((i) => pair(i, Number.isFinite(toNumber(B1[i])) ? toNumber(B1[i]) : null)),
|
|
428
|
+
stack: stackId,
|
|
429
|
+
stackStrategy: "all",
|
|
430
|
+
itemStyle: { color: "transparent", opacity: 0 },
|
|
431
|
+
silent: true,
|
|
432
|
+
tooltip: { show: false },
|
|
433
|
+
emphasis: { disabled: true },
|
|
434
|
+
label: undefined,
|
|
435
|
+
};
|
|
436
|
+
const seg = {
|
|
437
|
+
...baseSeries(rm, g, ctx, k),
|
|
438
|
+
...common,
|
|
439
|
+
data: g.index.map((i) => {
|
|
440
|
+
const v1 = toNumber(B1[i]);
|
|
441
|
+
const v2 = toNumber(B2[i]);
|
|
442
|
+
const h = Number.isFinite(v1) && Number.isFinite(v2) ? v2 - v1 : NaN;
|
|
443
|
+
return withHighlight(pair(i, Number.isFinite(h) ? h : null), rm, i, ctx);
|
|
444
|
+
}),
|
|
445
|
+
stack: stackId,
|
|
446
|
+
stackStrategy: "all",
|
|
447
|
+
};
|
|
448
|
+
applySeriesStyle(seg, rm, g, ctx);
|
|
449
|
+
series.push(base, seg);
|
|
450
|
+
}
|
|
451
|
+
if (!rm.mark.generated && o.legend !== false && (g.value !== undefined || o.name))
|
|
452
|
+
legend.push(seriesName(rm, g));
|
|
453
|
+
});
|
|
454
|
+
return { series, legend, tooltip: "axis" };
|
|
455
|
+
}
|
|
456
|
+
// ------------------------------------------------------------------ rect ---
|
|
457
|
+
/** Rectangles with quantitative edges on both axes, drawn with a custom series. */
|
|
458
|
+
function compileRect(rm, I, ctx) {
|
|
459
|
+
const o = rm.mark.options;
|
|
460
|
+
const { x: xs, y: ys } = ctx.scales;
|
|
461
|
+
const X1 = rm.channels.x1;
|
|
462
|
+
const X2 = rm.channels.x2;
|
|
463
|
+
const Y1 = rm.channels.y1;
|
|
464
|
+
const Y2 = rm.channels.y2 ?? rm.channels.y;
|
|
465
|
+
const ordinalX = xs.type === "ordinal";
|
|
466
|
+
const ordinalY = ys.type === "ordinal";
|
|
467
|
+
// Degrade to bars when one axis is ordinal.
|
|
468
|
+
if (ordinalX && !ordinalY && (X1 === undefined || X2 === undefined))
|
|
469
|
+
return compileBar(rm, I, ctx, false);
|
|
470
|
+
if (ordinalY && !ordinalX && (Y1 === undefined || Y2 === undefined))
|
|
471
|
+
return compileBar(rm, I, ctx, true);
|
|
472
|
+
if (!X1 || !X2 || !Y2)
|
|
473
|
+
return { series: [], legend: [], tooltip: "item" };
|
|
474
|
+
const colorDim = continuousColorDim(rm);
|
|
475
|
+
const inset = o.inset ?? ctx.theme.mark.gap / 2;
|
|
476
|
+
const series = [];
|
|
477
|
+
const legend = [];
|
|
478
|
+
const { names: tipNames, values: tipValues } = tooltipDims(rm);
|
|
479
|
+
groupSeries(rm, I).forEach((g, k) => {
|
|
480
|
+
const color = colorFor(rm, g, ctx, "fill");
|
|
481
|
+
const data = g.index.map((i) => {
|
|
482
|
+
const row = {
|
|
483
|
+
value: [
|
|
484
|
+
coord(xs, X1[i]),
|
|
485
|
+
coord(xs, X2[i]),
|
|
486
|
+
Y1 ? (coord(ys, Y1[i]) ?? 0) : 0,
|
|
487
|
+
coord(ys, Y2[i]),
|
|
488
|
+
colorDim ? toNumber(colorDim[i]) : null,
|
|
489
|
+
...tipValues.map((v) => (isMissing(v[i]) ? null : formatValue(v[i]))),
|
|
490
|
+
],
|
|
491
|
+
};
|
|
492
|
+
const h = ctx.highlights.get(rm.mark.id)?.get(i);
|
|
493
|
+
if (h?.color)
|
|
494
|
+
row.itemStyle = { color: h.color };
|
|
495
|
+
return row;
|
|
496
|
+
});
|
|
497
|
+
const s = {
|
|
498
|
+
...baseSeries(rm, g, ctx, k),
|
|
499
|
+
type: "custom",
|
|
500
|
+
data,
|
|
501
|
+
encode: { x: [0, 1], y: [2, 3], tooltip: colorDim ? [4] : [3], ...(tipNames.length ? { itemName: 5 } : {}) },
|
|
502
|
+
dimensions: ["x1", "x2", "y1", "y2", rm.labels.fill ?? "value", ...tipNames],
|
|
503
|
+
itemStyle: { ...(color ? { color } : {}), opacity: o.fillOpacity ?? o.opacity ?? 1, ...(rm.constants.stroke ? { borderColor: rm.constants.stroke, borderWidth: o.strokeWidth ?? 1 } : {}) },
|
|
504
|
+
clip: true,
|
|
505
|
+
renderItem: makeRectRenderer(inset, Boolean(colorDim)),
|
|
506
|
+
emphasis: { focus: "series" },
|
|
507
|
+
};
|
|
508
|
+
if (colorDim)
|
|
509
|
+
ctx.visualTargets.push({ seriesId: s.id, dimension: 4 });
|
|
510
|
+
applySeriesStyle(s, rm, g, ctx);
|
|
511
|
+
series.push(s);
|
|
512
|
+
if (!rm.mark.generated && o.legend !== false && (g.value !== undefined || o.name))
|
|
513
|
+
legend.push(s.name);
|
|
514
|
+
});
|
|
515
|
+
return { series, legend, tooltip: "item" };
|
|
516
|
+
}
|
|
517
|
+
function makeRectRenderer(inset, visual) {
|
|
518
|
+
return (params, api) => {
|
|
519
|
+
const x1 = api.value(0);
|
|
520
|
+
const x2 = api.value(1);
|
|
521
|
+
const y1 = api.value(2);
|
|
522
|
+
const y2 = api.value(3);
|
|
523
|
+
if ([x1, x2, y1, y2].some((v) => v === null || v === undefined || Number.isNaN(v)))
|
|
524
|
+
return null;
|
|
525
|
+
const a = api.coord([x1, y2]);
|
|
526
|
+
const b = api.coord([x2, y1]);
|
|
527
|
+
const left = Math.min(a[0], b[0]) + inset;
|
|
528
|
+
const top = Math.min(a[1], b[1]) + inset;
|
|
529
|
+
const width = Math.max(0, Math.abs(b[0] - a[0]) - 2 * inset);
|
|
530
|
+
const height = Math.max(0, Math.abs(b[1] - a[1]) - 2 * inset);
|
|
531
|
+
const style = { fill: api.visual("color"), opacity: api.visual("opacity") ?? 1, stroke: api.visual("borderColor"), lineWidth: api.visual("borderWidth") ?? 0 };
|
|
532
|
+
void visual;
|
|
533
|
+
return { type: "rect", shape: { x: left, y: top, width, height }, style, emphasis: { style: { opacity: 0.8 } } };
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
// ------------------------------------------------------------------ cell ---
|
|
537
|
+
function compileCell(rm, I, ctx) {
|
|
538
|
+
const o = rm.mark.options;
|
|
539
|
+
const { x: xs, y: ys } = ctx.scales;
|
|
540
|
+
const X = rm.channels.x;
|
|
541
|
+
const Y = rm.channels.y;
|
|
542
|
+
const fill = rm.channels.fill ?? rm.channels.stroke;
|
|
543
|
+
const continuous = fill && isNumeric(fill);
|
|
544
|
+
const constant = rm.constants.fill;
|
|
545
|
+
const { names: tipNames, values: tipValues } = tooltipDims(rm);
|
|
546
|
+
const data = I.map((i) => {
|
|
547
|
+
const row = { value: [coord(xs, X[i]), coord(ys, Y[i]), fill ? (continuous ? toNumber(fill[i]) : formatValue(fill[i])) : 1, ...tipValues.map((v) => formatValue(v[i]))] };
|
|
548
|
+
const cellColor = fill ? ctx.scales.color.colorOf(fill[i]) : constant;
|
|
549
|
+
if (fill && !continuous)
|
|
550
|
+
row.itemStyle = { color: cellColor };
|
|
551
|
+
else if (constant)
|
|
552
|
+
row.itemStyle = { color: constant };
|
|
553
|
+
if (o.label && cellColor)
|
|
554
|
+
row.label = { color: inkOn(cellColor) };
|
|
555
|
+
const h = ctx.highlights.get(rm.mark.id)?.get(i);
|
|
556
|
+
if (h?.color)
|
|
557
|
+
row.itemStyle = { ...(row.itemStyle ?? {}), borderColor: h.color, borderWidth: 2 };
|
|
558
|
+
return row;
|
|
559
|
+
});
|
|
560
|
+
const g = { key: null, value: undefined, index: I };
|
|
561
|
+
const s = {
|
|
562
|
+
...baseSeries(rm, g, ctx, 0),
|
|
563
|
+
type: "heatmap",
|
|
564
|
+
data,
|
|
565
|
+
dimensions: ["x", "y", rm.labels.fill ?? "value", ...tipNames],
|
|
566
|
+
encode: { x: 0, y: 1, value: 2, tooltip: [2, ...tipNames.map((_, j) => 3 + j)] },
|
|
567
|
+
label: o.label ? { show: true, fontSize: 11, fontFamily: ctx.theme.font, formatter: (p) => formatValue(p.value[2]) } : { show: false },
|
|
568
|
+
itemStyle: { borderColor: ctx.theme.background, borderWidth: o.inset ?? ctx.theme.mark.gap, borderRadius: 3, opacity: o.fillOpacity ?? o.opacity ?? 1 },
|
|
569
|
+
emphasis: { itemStyle: { shadowBlur: 6, shadowColor: "rgba(0,0,0,0.3)" } },
|
|
570
|
+
};
|
|
571
|
+
if (continuous)
|
|
572
|
+
ctx.visualTargets.push({ seriesId: s.id, dimension: 2 });
|
|
573
|
+
return { series: [s], legend: [], tooltip: "item" };
|
|
574
|
+
}
|
|
575
|
+
// ------------------------------------------------------------------- dot ---
|
|
576
|
+
function compileDot(rm, I, ctx) {
|
|
577
|
+
const o = rm.mark.options;
|
|
578
|
+
const { x: xs, y: ys, r } = ctx.scales;
|
|
579
|
+
const X = rm.channels.x;
|
|
580
|
+
const Y = rm.channels.y;
|
|
581
|
+
if (!X && !Y)
|
|
582
|
+
return { series: [], legend: [], tooltip: "item" };
|
|
583
|
+
const R = rm.channels.r;
|
|
584
|
+
const colorDim = continuousColorDim(rm);
|
|
585
|
+
const { names: tipNames, values: tipValues } = tooltipDims(rm);
|
|
586
|
+
const series = [];
|
|
587
|
+
const legend = [];
|
|
588
|
+
groupSeries(rm, I).forEach((g, k) => {
|
|
589
|
+
const color = colorFor(rm, g, ctx, "fill") ?? colorFor(rm, g, ctx, "stroke");
|
|
590
|
+
const data = g.index.map((i) => {
|
|
591
|
+
const row = {
|
|
592
|
+
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])))],
|
|
593
|
+
};
|
|
594
|
+
return withHighlight(row, rm, i, ctx);
|
|
595
|
+
});
|
|
596
|
+
const constR = typeof o.r === "number" ? o.r : undefined;
|
|
597
|
+
const s = {
|
|
598
|
+
...baseSeries(rm, g, ctx, k),
|
|
599
|
+
type: "scatter",
|
|
600
|
+
data,
|
|
601
|
+
dimensions: ["x", "y", rm.labels.r ?? "r", rm.labels.fill ?? rm.labels.stroke ?? "color", ...tipNames],
|
|
602
|
+
encode: { x: 0, y: 1, tooltip: [0, 1, ...(R ? [2] : []), ...(colorDim ? [3] : []), ...tipNames.map((_, j) => 4 + j)] },
|
|
603
|
+
symbol: o.symbol ?? "circle",
|
|
604
|
+
symbolSize: R ? (value) => r.sizeOf(value[2]) * 2 : (constR ?? ctx.theme.mark.dotRadius) * 2,
|
|
605
|
+
itemStyle: {
|
|
606
|
+
...(color ? { color } : {}),
|
|
607
|
+
opacity: o.fillOpacity ?? o.opacity ?? 1,
|
|
608
|
+
// Surface ring so dots stay legible where they overlap.
|
|
609
|
+
borderColor: ctx.theme.background,
|
|
610
|
+
borderWidth: ctx.theme.mark.gap / 2,
|
|
611
|
+
...(rm.constants.stroke ? { borderColor: rm.constants.stroke, borderWidth: o.strokeWidth ?? 1 } : {}),
|
|
612
|
+
...(rm.constants.fill === "none" ? { color: "transparent", borderColor: typeof color === "string" ? color : undefined, borderWidth: o.strokeWidth ?? 1.5 } : {}),
|
|
613
|
+
},
|
|
614
|
+
emphasis: { focus: "series", scale: 1.4 },
|
|
615
|
+
blur: { itemStyle: { opacity: 0.2 } },
|
|
616
|
+
...(data.length > 5000 ? { large: true, largeThreshold: 5000 } : {}),
|
|
617
|
+
};
|
|
618
|
+
if (colorDim)
|
|
619
|
+
ctx.visualTargets.push({ seriesId: s.id, dimension: 3 });
|
|
620
|
+
applySeriesStyle(s, rm, g, ctx);
|
|
621
|
+
series.push(s);
|
|
622
|
+
if (!rm.mark.generated && o.legend !== false && (g.value !== undefined || o.name))
|
|
623
|
+
legend.push(s.name);
|
|
624
|
+
});
|
|
625
|
+
return { series, legend, tooltip: "item" };
|
|
626
|
+
}
|
|
627
|
+
// ------------------------------------------------------------------ rule ---
|
|
628
|
+
function compileRule(rm, I, ctx, vertical) {
|
|
629
|
+
const o = rm.mark.options;
|
|
630
|
+
const { x: xs, y: ys } = ctx.scales;
|
|
631
|
+
const P = vertical ? rm.channels.x : rm.channels.y; // position of the rule
|
|
632
|
+
const S1 = vertical ? rm.channels.y1 : rm.channels.x1;
|
|
633
|
+
const S2 = vertical ? (rm.channels.y2 ?? rm.channels.y) : (rm.channels.x2 ?? rm.channels.x);
|
|
634
|
+
if (!P)
|
|
635
|
+
return { series: [], legend: [], tooltip: "axis" };
|
|
636
|
+
const ps = vertical ? xs : ys;
|
|
637
|
+
const ss = vertical ? ys : xs;
|
|
638
|
+
const T = rm.channels.text ?? rm.channels.title;
|
|
639
|
+
const color = rm.constants.stroke ?? rm.constants.fill ?? (rm.channels.stroke && ctx.scales.color.kind === "categorical" ? undefined : ctx.theme.textMuted);
|
|
640
|
+
const lineStyle = { width: o.strokeWidth ?? 1, type: dashType(o), opacity: o.strokeOpacity ?? o.opacity ?? 1, ...(color ? { color } : {}) };
|
|
641
|
+
const g = { key: null, value: undefined, index: I };
|
|
642
|
+
if (S2 === undefined && S1 === undefined) {
|
|
643
|
+
// Full-span rules → markLine on a helper series.
|
|
644
|
+
const data = I.map((i) => {
|
|
645
|
+
const v = coord(ps, P[i]);
|
|
646
|
+
if (v === null)
|
|
647
|
+
return null;
|
|
648
|
+
const c = rm.channels.stroke ? ctx.scales.color.colorOf(rm.channels.stroke[i]) : undefined;
|
|
649
|
+
const item = vertical ? { xAxis: v } : { yAxis: v };
|
|
650
|
+
if (T && !isMissing(T[i]))
|
|
651
|
+
item.label = { show: true, formatter: formatValue(T[i]), position: vertical ? "insideEndTop" : "insideEndTop" };
|
|
652
|
+
if (c)
|
|
653
|
+
item.lineStyle = { color: c };
|
|
654
|
+
return item;
|
|
655
|
+
}).filter(Boolean);
|
|
656
|
+
const s = {
|
|
657
|
+
...baseSeries(rm, g, ctx, 0),
|
|
658
|
+
type: "line",
|
|
659
|
+
data: [],
|
|
660
|
+
silent: true,
|
|
661
|
+
tooltip: { show: false },
|
|
662
|
+
markLine: { silent: true, symbol: "none", animation: ctx.animation, lineStyle, label: { show: Boolean(T), fontSize: 11 }, data, z: 2 + (o.z2 ?? 0) },
|
|
663
|
+
};
|
|
664
|
+
return { series: [s], legend: [], tooltip: "axis" };
|
|
665
|
+
}
|
|
666
|
+
// Segments → custom series lines.
|
|
667
|
+
const data = I.map((i) => {
|
|
668
|
+
const p = coord(ps, P[i]);
|
|
669
|
+
const a = S1 ? coord(ss, S1[i]) : 0;
|
|
670
|
+
const b = coord(ss, S2[i]);
|
|
671
|
+
return { value: vertical ? [p, a, b] : [a, b, p], ...(T && !isMissing(T[i]) ? { name: formatValue(T[i]) } : {}) };
|
|
672
|
+
});
|
|
673
|
+
const s = {
|
|
674
|
+
...baseSeries(rm, g, ctx, 0),
|
|
675
|
+
type: "custom",
|
|
676
|
+
data,
|
|
677
|
+
encode: vertical ? { x: 0, y: [1, 2] } : { x: [0, 1], y: 2 },
|
|
678
|
+
clip: true,
|
|
679
|
+
renderItem: (_params, api) => {
|
|
680
|
+
const v = [api.value(0), api.value(1), api.value(2)];
|
|
681
|
+
if (v.some((x) => x === null || Number.isNaN(x)))
|
|
682
|
+
return null;
|
|
683
|
+
const p1 = vertical ? api.coord([v[0], v[1]]) : api.coord([v[0], v[2]]);
|
|
684
|
+
const p2 = vertical ? api.coord([v[0], v[2]]) : api.coord([v[1], v[2]]);
|
|
685
|
+
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" } };
|
|
686
|
+
},
|
|
687
|
+
itemStyle: { opacity: lineStyle.opacity },
|
|
688
|
+
};
|
|
689
|
+
return { series: [s], legend: [], tooltip: "item" };
|
|
690
|
+
}
|
|
691
|
+
// ------------------------------------------------------------------ text ---
|
|
692
|
+
function compileText(rm, I, ctx) {
|
|
693
|
+
const o = rm.mark.options;
|
|
694
|
+
const { x: xs, y: ys } = ctx.scales;
|
|
695
|
+
const X = rm.channels.x;
|
|
696
|
+
const Y = rm.channels.y;
|
|
697
|
+
const T = rm.channels.text;
|
|
698
|
+
const color = rm.constants.fill ?? rm.constants.stroke;
|
|
699
|
+
const data = I.filter((i) => !isMissing(T[i])).map((i) => ({
|
|
700
|
+
value: [X ? coord(xs, X[i]) : 0, Y ? coord(ys, Y[i]) : 0],
|
|
701
|
+
label: { formatter: typeof o.label === "function" ? o.label(rm.data[i], i) : formatValue(T[i]) },
|
|
702
|
+
...(rm.channels.fill ? { itemStyle: { color: ctx.scales.color.colorOf(rm.channels.fill[i]) } } : {}),
|
|
703
|
+
}));
|
|
704
|
+
const g = { key: null, value: undefined, index: I };
|
|
705
|
+
const s = {
|
|
706
|
+
...baseSeries(rm, g, ctx, 0),
|
|
707
|
+
type: "scatter",
|
|
708
|
+
data,
|
|
709
|
+
symbolSize: 1,
|
|
710
|
+
silent: o.silent ?? true,
|
|
711
|
+
tooltip: { show: false },
|
|
712
|
+
itemStyle: { color: "transparent" },
|
|
713
|
+
label: {
|
|
714
|
+
show: true,
|
|
715
|
+
position: o.position ?? "top",
|
|
716
|
+
offset: [o.dx ?? 0, o.dy ?? 0],
|
|
717
|
+
fontSize: o.fontSize ?? 11,
|
|
718
|
+
color: color ?? ctx.theme.text,
|
|
719
|
+
fontFamily: ctx.theme.font,
|
|
720
|
+
fontWeight: o.fontWeight,
|
|
721
|
+
textBorderColor: withOpacity(ctx.theme.background, 0.85),
|
|
722
|
+
textBorderWidth: 2,
|
|
723
|
+
},
|
|
724
|
+
labelLayout: { hideOverlap: true },
|
|
725
|
+
};
|
|
726
|
+
return { series: [s], legend: [], tooltip: "item" };
|
|
727
|
+
}
|
|
728
|
+
// ------------------------------------------------------------ difference ---
|
|
729
|
+
function compileDifference(rm, I, ctx) {
|
|
730
|
+
const o = rm.mark.options;
|
|
731
|
+
const { x: xs, y: ys } = ctx.scales;
|
|
732
|
+
const X1 = rm.channels.x1;
|
|
733
|
+
const X2 = rm.channels.x2;
|
|
734
|
+
const Y1 = rm.channels.y1;
|
|
735
|
+
const Y2 = rm.channels.y2;
|
|
736
|
+
const series = [];
|
|
737
|
+
const positive = o.positiveFill ?? ctx.theme.insight.positive;
|
|
738
|
+
const negative = o.negativeFill ?? ctx.theme.insight.negative;
|
|
739
|
+
const fillOpacity = o.fillOpacity ?? 0.5;
|
|
740
|
+
groupSeries(rm, I).forEach((g, k) => {
|
|
741
|
+
// Comparison series resampled onto the metric's x positions (needed for shiftX).
|
|
742
|
+
const xm = g.index.map((i) => toNumber(X2[i]));
|
|
743
|
+
const ym = g.index.map((i) => toNumber(Y2[i]));
|
|
744
|
+
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]);
|
|
745
|
+
const sameX = g.index.every((i) => toNumber(X1[i]) === toNumber(X2[i]));
|
|
746
|
+
const yc = sameX ? g.index.map((i) => toNumber(Y1[i])) : xm.map((x) => interpolate(cmpPts, x));
|
|
747
|
+
// Insert crossing points so the fills meet exactly where the lines cross.
|
|
748
|
+
const px = [];
|
|
749
|
+
const pm = [];
|
|
750
|
+
const pc = [];
|
|
751
|
+
for (let j = 0; j < xm.length; j++) {
|
|
752
|
+
if (j > 0) {
|
|
753
|
+
const d0 = ym[j - 1] - yc[j - 1];
|
|
754
|
+
const d1 = ym[j] - yc[j];
|
|
755
|
+
if (Number.isFinite(d0) && Number.isFinite(d1) && d0 * d1 < 0) {
|
|
756
|
+
const t = d0 / (d0 - d1);
|
|
757
|
+
px.push(xm[j - 1] + t * (xm[j] - xm[j - 1]));
|
|
758
|
+
const yv = ym[j - 1] + t * (ym[j] - ym[j - 1]);
|
|
759
|
+
pm.push(yv);
|
|
760
|
+
pc.push(yv);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
px.push(xm[j]);
|
|
764
|
+
pm.push(ym[j]);
|
|
765
|
+
pc.push(yc[j]);
|
|
766
|
+
}
|
|
767
|
+
const asCoord = (v) => (xs.type === "temporal" || xs.type === "quantitative" ? v : coord(xs, X2[g.index[0]]));
|
|
768
|
+
const posCoords = px.map((x) => asCoord(x));
|
|
769
|
+
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 });
|
|
770
|
+
// Positive: metric above comparison; negative: below. Crossing points were inserted so each band collapses to zero height on the other side.
|
|
771
|
+
series.push(mk("positive", pc.map((c, j) => Math.min(c, pm[j])), pm, positive, o.positiveFillOpacity ?? fillOpacity, "pos"));
|
|
772
|
+
series.push(mk("negative", pm, pc.map((c, j) => Math.max(c, pm[j])), negative, o.negativeFillOpacity ?? fillOpacity, "neg"));
|
|
773
|
+
const stroke = rm.constants.stroke ?? ctx.theme.text;
|
|
774
|
+
series.push({
|
|
775
|
+
...baseSeries(rm, g, ctx, k),
|
|
776
|
+
type: "line",
|
|
777
|
+
data: xm.map((x, j) => [asCoord(x), Number.isFinite(ym[j]) ? ym[j] : null]),
|
|
778
|
+
showSymbol: false,
|
|
779
|
+
lineStyle: { width: o.strokeWidth ?? ctx.theme.mark.lineWidth, color: stroke, opacity: o.strokeOpacity ?? 1, cap: "round", join: "round" },
|
|
780
|
+
itemStyle: { color: stroke },
|
|
781
|
+
emphasis: { focus: "series" },
|
|
782
|
+
});
|
|
783
|
+
if (!sameX || o.stroke === undefined) {
|
|
784
|
+
series.push({
|
|
785
|
+
...baseSeries(rm, g, ctx, k),
|
|
786
|
+
id: `${rm.mark.id}:${ctx.facet}:${k}:cmp`,
|
|
787
|
+
name: `${seriesName(rm, g)} (comparison)`,
|
|
788
|
+
type: "line",
|
|
789
|
+
data: xm.map((x, j) => [asCoord(x), Number.isFinite(yc[j]) ? yc[j] : null]),
|
|
790
|
+
showSymbol: false,
|
|
791
|
+
lineStyle: { width: 1, color: stroke, opacity: 0.5, type: "dashed" },
|
|
792
|
+
itemStyle: { color: stroke },
|
|
793
|
+
tooltip: { show: false },
|
|
794
|
+
silent: true,
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
});
|
|
798
|
+
return { series, legend: [], tooltip: "axis" };
|
|
799
|
+
}
|
|
800
|
+
function interpolate(pts, x) {
|
|
801
|
+
if (pts.length === 0 || !Number.isFinite(x))
|
|
802
|
+
return NaN;
|
|
803
|
+
if (x < pts[0][0] || x > pts[pts.length - 1][0])
|
|
804
|
+
return NaN;
|
|
805
|
+
let lo = 0;
|
|
806
|
+
let hi = pts.length - 1;
|
|
807
|
+
while (hi - lo > 1) {
|
|
808
|
+
const mid = (lo + hi) >> 1;
|
|
809
|
+
if (pts[mid][0] <= x)
|
|
810
|
+
lo = mid;
|
|
811
|
+
else
|
|
812
|
+
hi = mid;
|
|
813
|
+
}
|
|
814
|
+
const [x0, y0] = pts[lo];
|
|
815
|
+
const [x1, y1] = pts[hi];
|
|
816
|
+
if (x1 === x0)
|
|
817
|
+
return y0;
|
|
818
|
+
return y0 + ((y1 - y0) * (x - x0)) / (x1 - x0);
|
|
819
|
+
}
|
|
820
|
+
// ---------------------------------------------------------------- driver ---
|
|
821
|
+
export function compileMark(rm, facet, ctx) {
|
|
822
|
+
const I = rm.facets[facet] ?? [];
|
|
823
|
+
const type = rm.mark.type;
|
|
824
|
+
const c = { ...ctx, facet };
|
|
825
|
+
switch (type) {
|
|
826
|
+
case "line":
|
|
827
|
+
case "lineY":
|
|
828
|
+
case "lineX":
|
|
829
|
+
return rm.channels.x && rm.channels.y ? compileLine(rm, I, c) : empty();
|
|
830
|
+
case "area":
|
|
831
|
+
case "areaY":
|
|
832
|
+
return compileArea(rm, I, c, false);
|
|
833
|
+
case "areaX":
|
|
834
|
+
return compileArea(rm, I, c, true);
|
|
835
|
+
case "barY":
|
|
836
|
+
return compileBar(rm, I, c, false);
|
|
837
|
+
case "barX":
|
|
838
|
+
return compileBar(rm, I, c, true);
|
|
839
|
+
case "rectY":
|
|
840
|
+
case "rectX":
|
|
841
|
+
case "rect":
|
|
842
|
+
return compileRect(rm, I, c);
|
|
843
|
+
case "cell":
|
|
844
|
+
return compileCell(rm, I, c);
|
|
845
|
+
case "dot":
|
|
846
|
+
return compileDot(rm, I, c);
|
|
847
|
+
case "ruleX":
|
|
848
|
+
return compileRule(rm, I, c, true);
|
|
849
|
+
case "ruleY":
|
|
850
|
+
return compileRule(rm, I, c, false);
|
|
851
|
+
case "text":
|
|
852
|
+
return compileText(rm, I, c);
|
|
853
|
+
case "differenceY":
|
|
854
|
+
case "differenceX":
|
|
855
|
+
return compileDifference(rm, I, c);
|
|
856
|
+
default:
|
|
857
|
+
return empty();
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
function empty() {
|
|
861
|
+
return { series: [], legend: [], tooltip: "axis" };
|
|
862
|
+
}
|
|
863
|
+
/** Used by facets/axes: does this mark draw bars (needs `boundaryGap`)? */
|
|
864
|
+
export function isBarLike(mark) {
|
|
865
|
+
return mark.type === "barY" || mark.type === "barX" || mark.type === "cell" || mark.type === "rectY" || mark.type === "rectX";
|
|
866
|
+
}
|
|
867
|
+
export { withOpacity };
|
|
868
|
+
//# sourceMappingURL=series.js.map
|