@aura-react-comp/charts 0.2.0 → 0.4.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/esm/charts/area-chart/index.d.ts +2 -2
- package/esm/charts/area-chart/index.js +28 -23
- package/esm/charts/bar-chart/index.d.ts +1 -1
- package/esm/charts/bar-chart/index.js +18 -21
- package/esm/charts/candlestick-chart/index.d.ts +2 -2
- package/esm/charts/candlestick-chart/index.js +243 -180
- package/esm/charts/combo-chart/index.d.ts +1 -1
- package/esm/charts/combo-chart/index.js +9 -20
- package/esm/charts/funnel-chart/index.d.ts +2 -2
- package/esm/charts/funnel-chart/index.js +134 -108
- package/esm/charts/gauge-chart/index.d.ts +1 -1
- package/esm/charts/gauge-chart/index.js +103 -105
- package/esm/charts/graph-chart/index.d.ts +3 -3
- package/esm/charts/graph-chart/index.js +155 -114
- package/esm/charts/heatmap-chart/index.d.ts +2 -2
- package/esm/charts/heatmap-chart/index.js +180 -135
- package/esm/charts/line-chart/index.d.ts +2 -2
- package/esm/charts/line-chart/index.js +29 -24
- package/esm/charts/pie-chart/index.d.ts +2 -2
- package/esm/charts/pie-chart/index.d.ts.map +1 -1
- package/esm/charts/pie-chart/index.js +199 -139
- package/esm/charts/radar-chart/index.d.ts +2 -2
- package/esm/charts/radar-chart/index.d.ts.map +1 -1
- package/esm/charts/radar-chart/index.js +155 -128
- package/esm/charts/ring-progress/index.d.ts +1 -1
- package/esm/charts/ring-progress/index.js +82 -82
- package/esm/charts/sankey-chart/index.d.ts +3 -3
- package/esm/charts/sankey-chart/index.js +172 -141
- package/esm/charts/scatter-chart/index.d.ts +3 -3
- package/esm/charts/scatter-chart/index.d.ts.map +1 -1
- package/esm/charts/scatter-chart/index.js +226 -159
- package/esm/charts/sparkline/index.d.ts +1 -1
- package/esm/charts/sparkline/index.js +82 -66
- package/esm/charts/tree-chart/index.d.ts +3 -3
- package/esm/charts/tree-chart/index.js +129 -102
- package/esm/charts/treemap-chart/index.d.ts +3 -3
- package/esm/charts/treemap-chart/index.js +163 -141
- package/esm/charts/waterfall-chart/index.d.ts +2 -2
- package/esm/charts/waterfall-chart/index.js +166 -121
- package/esm/components/Legend.d.ts +5 -4
- package/esm/components/Legend.d.ts.map +1 -1
- package/esm/components/Legend.js +80 -48
- package/esm/components/Tooltip.d.ts +2 -2
- package/esm/components/Tooltip.js +63 -71
- package/esm/components/chart/index.d.ts +5 -2
- package/esm/components/chart/index.d.ts.map +1 -1
- package/esm/components/chart/index.js +81 -102
- package/esm/components/chart.less +13 -0
- package/esm/core/animation.js +32 -43
- package/esm/core/export.d.ts +34 -0
- package/esm/core/export.d.ts.map +1 -0
- package/esm/core/export.js +390 -0
- package/esm/core/force.js +23 -45
- package/esm/core/format.js +49 -52
- package/esm/core/hierarchy.d.ts +1 -1
- package/esm/core/hierarchy.js +61 -92
- package/esm/core/layout.d.ts +1 -1
- package/esm/core/layout.js +33 -50
- package/esm/core/sampling.js +31 -53
- package/esm/core/sankey.js +36 -55
- package/esm/core/scale.js +73 -86
- package/esm/core/shape.js +102 -133
- package/esm/core/sync.d.ts +41 -0
- package/esm/core/sync.d.ts.map +1 -0
- package/esm/core/sync.js +120 -0
- package/esm/hooks/useChartExport.d.ts +19 -0
- package/esm/hooks/useChartExport.d.ts.map +1 -0
- package/esm/hooks/useChartExport.js +35 -0
- package/esm/hooks/useChartTheme.d.ts +6 -5
- package/esm/hooks/useChartTheme.d.ts.map +1 -1
- package/esm/hooks/useChartTheme.js +18 -18
- package/esm/hooks/useChartTransition.d.ts +1 -1
- package/esm/hooks/useChartTransition.js +59 -73
- package/esm/hooks/useElementSize.js +20 -28
- package/esm/hooks/usePlotPointer.d.ts +23 -0
- package/esm/hooks/usePlotPointer.d.ts.map +1 -0
- package/esm/hooks/usePlotPointer.js +94 -0
- package/esm/index.d.ts +56 -50
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +137 -47
- package/esm/internal/CartesianChart.d.ts +1 -1
- package/esm/internal/CartesianChart.d.ts.map +1 -1
- package/esm/internal/CartesianChart.js +667 -485
- package/esm/internal/model.d.ts +2 -2
- package/esm/internal/model.js +45 -60
- package/esm/scene/canvas.d.ts +1 -1
- package/esm/scene/canvas.js +101 -107
- package/esm/scene/svg.d.ts +1 -1
- package/esm/scene/svg.js +107 -99
- package/esm/scene/types.js +9 -15
- package/esm/style.css +10 -0
- package/esm/theme/index.d.ts +2 -2
- package/esm/theme/index.d.ts.map +1 -1
- package/esm/theme/index.js +145 -170
- package/esm/types.d.ts +7 -3
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
// src/core/export.ts
|
|
3
|
+
import { detectDarkMode } from "../theme";
|
|
4
|
+
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
5
|
+
var XLINK_NS = "http://www.w3.org/1999/xlink";
|
|
6
|
+
var VAR_RE = /var\(\s*(--[\w-]+)\s*(?:,\s*([^)]+))?\)/g;
|
|
7
|
+
var INLINE_ATTRS = [
|
|
8
|
+
"fill",
|
|
9
|
+
"stroke",
|
|
10
|
+
"color",
|
|
11
|
+
"stop-color",
|
|
12
|
+
"font-family",
|
|
13
|
+
"font-size"
|
|
14
|
+
];
|
|
15
|
+
function isTransparent(value) {
|
|
16
|
+
if (!value)
|
|
17
|
+
return true;
|
|
18
|
+
const v = value.trim().toLowerCase();
|
|
19
|
+
return v === "" || v === "transparent" || v === "none" || v === "rgba(0, 0, 0, 0)" || v === "rgba(0,0,0,0)";
|
|
20
|
+
}
|
|
21
|
+
function resolveCssVars(value, el, depth = 0) {
|
|
22
|
+
if (depth > 4 || !value.includes("var(") || typeof window === "undefined") {
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
const computed = window.getComputedStyle(el);
|
|
26
|
+
const rootComputed = typeof document !== "undefined" && document.documentElement ? window.getComputedStyle(document.documentElement) : null;
|
|
27
|
+
const replaced = value.replace(
|
|
28
|
+
VAR_RE,
|
|
29
|
+
(match, name, fallback) => {
|
|
30
|
+
const resolved = computed.getPropertyValue(name).trim() || (rootComputed == null ? void 0 : rootComputed.getPropertyValue(name).trim()) || "";
|
|
31
|
+
if (resolved)
|
|
32
|
+
return resolved;
|
|
33
|
+
return fallback !== void 0 ? fallback.trim() : match;
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
return replaced !== value && replaced.includes("var(") ? resolveCssVars(replaced, el, depth + 1) : replaced;
|
|
37
|
+
}
|
|
38
|
+
function resolveBackground(container, explicit, forceOpaque = false) {
|
|
39
|
+
if (explicit !== void 0) {
|
|
40
|
+
if (!isTransparent(explicit)) {
|
|
41
|
+
return resolveCssVars(explicit, container);
|
|
42
|
+
}
|
|
43
|
+
if (!forceOpaque)
|
|
44
|
+
return null;
|
|
45
|
+
} else if (typeof window !== "undefined") {
|
|
46
|
+
const style = window.getComputedStyle(container);
|
|
47
|
+
if (!isTransparent(style.backgroundColor)) {
|
|
48
|
+
return style.backgroundColor;
|
|
49
|
+
}
|
|
50
|
+
const tokenBg = style.getPropertyValue("--aura-bg").trim() || window.getComputedStyle(document.documentElement).getPropertyValue("--aura-bg").trim();
|
|
51
|
+
if (!isTransparent(tokenBg))
|
|
52
|
+
return tokenBg;
|
|
53
|
+
}
|
|
54
|
+
if (forceOpaque) {
|
|
55
|
+
return detectDarkMode(container) ? "#111827" : "#ffffff";
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
function collectLayers(container) {
|
|
60
|
+
const scope = container.querySelector(".aura-chart__plot") ?? container;
|
|
61
|
+
const pick = (root) => Array.from(
|
|
62
|
+
root.querySelectorAll("canvas, svg")
|
|
63
|
+
).filter((node) => !node.closest(".aura-chart__a11y"));
|
|
64
|
+
let layers = pick(scope);
|
|
65
|
+
if (layers.length === 0 && scope !== container) {
|
|
66
|
+
layers = pick(container);
|
|
67
|
+
}
|
|
68
|
+
return { scope, layers };
|
|
69
|
+
}
|
|
70
|
+
function measureBox(el) {
|
|
71
|
+
var _a;
|
|
72
|
+
const rect = el.getBoundingClientRect();
|
|
73
|
+
let width = rect.width;
|
|
74
|
+
let height = rect.height;
|
|
75
|
+
if (width <= 0 || height <= 0) {
|
|
76
|
+
const attrW = Number.parseFloat(el.getAttribute("width") ?? "");
|
|
77
|
+
const attrH = Number.parseFloat(el.getAttribute("height") ?? "");
|
|
78
|
+
if (width <= 0 && Number.isFinite(attrW) && attrW > 0)
|
|
79
|
+
width = attrW;
|
|
80
|
+
if (height <= 0 && Number.isFinite(attrH) && attrH > 0)
|
|
81
|
+
height = attrH;
|
|
82
|
+
}
|
|
83
|
+
if ((width <= 0 || height <= 0) && el instanceof HTMLCanvasElement) {
|
|
84
|
+
if (width <= 0)
|
|
85
|
+
width = el.clientWidth || el.width;
|
|
86
|
+
if (height <= 0)
|
|
87
|
+
height = el.clientHeight || el.height;
|
|
88
|
+
}
|
|
89
|
+
if ((width <= 0 || height <= 0) && el instanceof SVGSVGElement) {
|
|
90
|
+
const vb = (_a = el.getAttribute("viewBox")) == null ? void 0 : _a.trim().split(/[\s,]+/).map(Number);
|
|
91
|
+
if (vb && vb.length === 4) {
|
|
92
|
+
if (width <= 0 && Number.isFinite(vb[2]) && vb[2] > 0)
|
|
93
|
+
width = vb[2];
|
|
94
|
+
if (height <= 0 && Number.isFinite(vb[3]) && vb[3] > 0)
|
|
95
|
+
height = vb[3];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return { width: Math.max(0, width), height: Math.max(0, height) };
|
|
99
|
+
}
|
|
100
|
+
function buildStandaloneSvg(svg, container, background) {
|
|
101
|
+
const clone = svg.cloneNode(true);
|
|
102
|
+
const { width, height } = measureBox(svg);
|
|
103
|
+
clone.setAttribute("xmlns", SVG_NS);
|
|
104
|
+
if (width > 0)
|
|
105
|
+
clone.setAttribute("width", String(width));
|
|
106
|
+
if (height > 0)
|
|
107
|
+
clone.setAttribute("height", String(height));
|
|
108
|
+
if (!clone.getAttribute("viewBox") && width > 0 && height > 0) {
|
|
109
|
+
clone.setAttribute("viewBox", `0 0 ${width} ${height}`);
|
|
110
|
+
}
|
|
111
|
+
const origList = [svg, ...Array.from(svg.querySelectorAll("*"))];
|
|
112
|
+
const cloneList = [
|
|
113
|
+
clone,
|
|
114
|
+
...Array.from(clone.querySelectorAll("*"))
|
|
115
|
+
];
|
|
116
|
+
const hasComputed = typeof window !== "undefined";
|
|
117
|
+
const containerStyle = hasComputed ? window.getComputedStyle(container) : null;
|
|
118
|
+
let hasXlink = false;
|
|
119
|
+
for (let i = 0; i < origList.length; i += 1) {
|
|
120
|
+
const orig = origList[i];
|
|
121
|
+
const target = cloneList[i];
|
|
122
|
+
if (!target)
|
|
123
|
+
continue;
|
|
124
|
+
if (!hasXlink && target.hasAttribute("xlink:href")) {
|
|
125
|
+
hasXlink = true;
|
|
126
|
+
}
|
|
127
|
+
const computed = hasComputed ? window.getComputedStyle(orig) : null;
|
|
128
|
+
for (const attr of INLINE_ATTRS) {
|
|
129
|
+
const raw = target.getAttribute(attr);
|
|
130
|
+
if (!raw)
|
|
131
|
+
continue;
|
|
132
|
+
if (raw.includes("var(")) {
|
|
133
|
+
const resolved = resolveCssVars(raw, orig);
|
|
134
|
+
const fallback = computed == null ? void 0 : computed.getPropertyValue(attr).trim();
|
|
135
|
+
target.setAttribute(
|
|
136
|
+
attr,
|
|
137
|
+
resolved.includes("var(") && fallback ? fallback : resolved
|
|
138
|
+
);
|
|
139
|
+
} else if (raw === "currentColor" && (computed == null ? void 0 : computed.color)) {
|
|
140
|
+
target.setAttribute(attr, computed.color);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const inlineStyle = target.getAttribute("style");
|
|
144
|
+
if (inlineStyle && inlineStyle.includes("var(")) {
|
|
145
|
+
target.setAttribute("style", resolveCssVars(inlineStyle, orig));
|
|
146
|
+
}
|
|
147
|
+
const tag = orig.tagName.toLowerCase();
|
|
148
|
+
if (i === 0) {
|
|
149
|
+
const fontFamily = resolveCssVars(
|
|
150
|
+
(computed == null ? void 0 : computed.fontFamily) || (containerStyle == null ? void 0 : containerStyle.fontFamily) || "",
|
|
151
|
+
orig
|
|
152
|
+
) || "sans-serif";
|
|
153
|
+
const color = resolveCssVars((computed == null ? void 0 : computed.color) || (containerStyle == null ? void 0 : containerStyle.color) || "", orig) || "#111827";
|
|
154
|
+
if (!target.getAttribute("font-family")) {
|
|
155
|
+
target.setAttribute("font-family", fontFamily);
|
|
156
|
+
}
|
|
157
|
+
if (!target.getAttribute("color")) {
|
|
158
|
+
target.setAttribute("color", color);
|
|
159
|
+
}
|
|
160
|
+
} else if (tag === "text" || tag === "tspan") {
|
|
161
|
+
if (!target.getAttribute("font-family")) {
|
|
162
|
+
const ff = resolveCssVars(
|
|
163
|
+
(computed == null ? void 0 : computed.fontFamily) || (containerStyle == null ? void 0 : containerStyle.fontFamily) || "",
|
|
164
|
+
orig
|
|
165
|
+
) || "sans-serif";
|
|
166
|
+
target.setAttribute("font-family", ff);
|
|
167
|
+
}
|
|
168
|
+
if (!target.getAttribute("font-size") && (computed == null ? void 0 : computed.fontSize)) {
|
|
169
|
+
target.setAttribute("font-size", computed.fontSize);
|
|
170
|
+
}
|
|
171
|
+
if (!target.getAttribute("fill")) {
|
|
172
|
+
const fill = (computed == null ? void 0 : computed.fill) || (computed == null ? void 0 : computed.color) || (containerStyle == null ? void 0 : containerStyle.color);
|
|
173
|
+
if (fill)
|
|
174
|
+
target.setAttribute("fill", fill);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (hasXlink) {
|
|
179
|
+
clone.setAttribute("xmlns:xlink", XLINK_NS);
|
|
180
|
+
}
|
|
181
|
+
if (background && !isTransparent(background)) {
|
|
182
|
+
const rect = document.createElementNS(SVG_NS, "rect");
|
|
183
|
+
rect.setAttribute("x", "0");
|
|
184
|
+
rect.setAttribute("y", "0");
|
|
185
|
+
rect.setAttribute("width", width > 0 ? String(width) : "100%");
|
|
186
|
+
rect.setAttribute("height", height > 0 ? String(height) : "100%");
|
|
187
|
+
rect.setAttribute("fill", background);
|
|
188
|
+
clone.insertBefore(rect, clone.firstChild);
|
|
189
|
+
}
|
|
190
|
+
const markup = typeof XMLSerializer !== "undefined" ? new XMLSerializer().serializeToString(clone) : clone.outerHTML;
|
|
191
|
+
return { markup, width, height };
|
|
192
|
+
}
|
|
193
|
+
function loadSvgImage(markup) {
|
|
194
|
+
return new Promise((resolve, reject) => {
|
|
195
|
+
const img = new Image();
|
|
196
|
+
let objectUrl = null;
|
|
197
|
+
const cleanup = () => {
|
|
198
|
+
if (objectUrl && typeof URL !== "undefined" && URL.revokeObjectURL) {
|
|
199
|
+
URL.revokeObjectURL(objectUrl);
|
|
200
|
+
objectUrl = null;
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
img.onload = () => {
|
|
204
|
+
cleanup();
|
|
205
|
+
resolve(img);
|
|
206
|
+
};
|
|
207
|
+
img.onerror = () => {
|
|
208
|
+
cleanup();
|
|
209
|
+
reject(new Error("SVG 图像加载失败,无法导出位图"));
|
|
210
|
+
};
|
|
211
|
+
if (typeof Blob !== "undefined" && typeof URL !== "undefined" && typeof URL.createObjectURL === "function") {
|
|
212
|
+
objectUrl = URL.createObjectURL(
|
|
213
|
+
new Blob([markup], { type: "image/svg+xml;charset=utf-8" })
|
|
214
|
+
);
|
|
215
|
+
img.src = objectUrl;
|
|
216
|
+
} else {
|
|
217
|
+
img.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(markup)}`;
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
function dataUrlToBlob(dataUrl) {
|
|
222
|
+
if (typeof Blob === "undefined")
|
|
223
|
+
return null;
|
|
224
|
+
const comma = dataUrl.indexOf(",");
|
|
225
|
+
if (comma === -1)
|
|
226
|
+
return null;
|
|
227
|
+
const header = dataUrl.slice(0, comma);
|
|
228
|
+
const payload = dataUrl.slice(comma + 1);
|
|
229
|
+
const mimeMatch = /data:([^;,]+)/.exec(header);
|
|
230
|
+
const mime = mimeMatch ? mimeMatch[1] : "application/octet-stream";
|
|
231
|
+
if (header.includes(";base64") && typeof atob === "function") {
|
|
232
|
+
const binary = atob(payload);
|
|
233
|
+
const bytes = new Uint8Array(binary.length);
|
|
234
|
+
for (let i = 0; i < binary.length; i += 1) {
|
|
235
|
+
bytes[i] = binary.charCodeAt(i);
|
|
236
|
+
}
|
|
237
|
+
return new Blob([bytes], { type: mime });
|
|
238
|
+
}
|
|
239
|
+
return new Blob([decodeURIComponent(payload)], { type: mime });
|
|
240
|
+
}
|
|
241
|
+
function exportChartSvg(container, options) {
|
|
242
|
+
if (!container) {
|
|
243
|
+
throw new Error("图表导出失败:未提供有效的容器元素");
|
|
244
|
+
}
|
|
245
|
+
const { layers } = collectLayers(container);
|
|
246
|
+
const svg = layers.find(
|
|
247
|
+
(node) => node.tagName.toLowerCase() === "svg"
|
|
248
|
+
);
|
|
249
|
+
if (!svg) {
|
|
250
|
+
throw new Error("图表导出失败:未在容器中找到可导出的 SVG 元素");
|
|
251
|
+
}
|
|
252
|
+
const background = resolveBackground(container, options == null ? void 0 : options.backgroundColor);
|
|
253
|
+
return buildStandaloneSvg(svg, container, background).markup;
|
|
254
|
+
}
|
|
255
|
+
function exportChartDataUrl(container, options) {
|
|
256
|
+
if (!container) {
|
|
257
|
+
return Promise.reject(new Error("图表导出失败:未提供有效的容器元素"));
|
|
258
|
+
}
|
|
259
|
+
const format = (options == null ? void 0 : options.format) ?? "png";
|
|
260
|
+
const rawRatio = (options == null ? void 0 : options.pixelRatio) ?? 2;
|
|
261
|
+
const pixelRatio = typeof rawRatio === "number" && Number.isFinite(rawRatio) && rawRatio > 0 ? rawRatio : 2;
|
|
262
|
+
const quality = (options == null ? void 0 : options.quality) ?? 0.92;
|
|
263
|
+
const { scope, layers } = collectLayers(container);
|
|
264
|
+
if (layers.length === 0) {
|
|
265
|
+
return Promise.reject(
|
|
266
|
+
new Error("图表导出失败:未在容器中找到可导出的画布或 SVG 元素")
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
if (format === "svg") {
|
|
270
|
+
const hasSvg = layers.some((node) => node.tagName.toLowerCase() === "svg");
|
|
271
|
+
if (hasSvg) {
|
|
272
|
+
try {
|
|
273
|
+
const svgMarkup = exportChartSvg(container, options);
|
|
274
|
+
return Promise.resolve(
|
|
275
|
+
`data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgMarkup)}`
|
|
276
|
+
);
|
|
277
|
+
} catch (error) {
|
|
278
|
+
return Promise.reject(error);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
const canvas = layers[0];
|
|
282
|
+
const { width, height } = measureBox(canvas);
|
|
283
|
+
const w = width || canvas.width || 300;
|
|
284
|
+
const h = height || canvas.height || 150;
|
|
285
|
+
const bg2 = resolveBackground(container, options == null ? void 0 : options.backgroundColor);
|
|
286
|
+
const png = canvas.toDataURL("image/png");
|
|
287
|
+
const bgRect = bg2 ? `<rect x="0" y="0" width="${w}" height="${h}" fill="${bg2}"/>` : "";
|
|
288
|
+
const wrapped = `<svg xmlns="${SVG_NS}" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">${bgRect}<image href="${png}" width="${w}" height="${h}"/></svg>`;
|
|
289
|
+
return Promise.resolve(
|
|
290
|
+
`data:image/svg+xml;charset=utf-8,${encodeURIComponent(wrapped)}`
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
const scopeRect = scope.getBoundingClientRect();
|
|
294
|
+
const baseSize = scopeRect.width > 0 && scopeRect.height > 0 ? { width: scopeRect.width, height: scopeRect.height } : measureBox(layers[0]);
|
|
295
|
+
if (baseSize.width <= 0 || baseSize.height <= 0) {
|
|
296
|
+
return Promise.reject(new Error("图表导出失败:目标图表尺寸为 0"));
|
|
297
|
+
}
|
|
298
|
+
const offscreen = document.createElement("canvas");
|
|
299
|
+
offscreen.width = Math.max(1, Math.round(baseSize.width * pixelRatio));
|
|
300
|
+
offscreen.height = Math.max(1, Math.round(baseSize.height * pixelRatio));
|
|
301
|
+
const ctx = offscreen.getContext("2d");
|
|
302
|
+
if (!ctx) {
|
|
303
|
+
return Promise.reject(
|
|
304
|
+
new Error("图表导出失败:当前环境不支持 Canvas 2D 渲染上下文")
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
ctx.scale(pixelRatio, pixelRatio);
|
|
308
|
+
const bg = resolveBackground(
|
|
309
|
+
container,
|
|
310
|
+
options == null ? void 0 : options.backgroundColor,
|
|
311
|
+
format === "jpeg"
|
|
312
|
+
);
|
|
313
|
+
if (bg) {
|
|
314
|
+
ctx.fillStyle = bg;
|
|
315
|
+
ctx.fillRect(0, 0, baseSize.width, baseSize.height);
|
|
316
|
+
}
|
|
317
|
+
let chain = Promise.resolve();
|
|
318
|
+
for (const layer of layers) {
|
|
319
|
+
chain = chain.then(() => {
|
|
320
|
+
const layerRect = layer.getBoundingClientRect();
|
|
321
|
+
const layerBox = measureBox(layer);
|
|
322
|
+
const drawW = layerBox.width || baseSize.width;
|
|
323
|
+
const drawH = layerBox.height || baseSize.height;
|
|
324
|
+
const offsetX = scopeRect.width > 0 && layerRect.width > 0 ? layerRect.left - scopeRect.left : 0;
|
|
325
|
+
const offsetY = scopeRect.height > 0 && layerRect.height > 0 ? layerRect.top - scopeRect.top : 0;
|
|
326
|
+
if (layer.tagName.toLowerCase() === "canvas") {
|
|
327
|
+
ctx.drawImage(
|
|
328
|
+
layer,
|
|
329
|
+
offsetX,
|
|
330
|
+
offsetY,
|
|
331
|
+
drawW,
|
|
332
|
+
drawH
|
|
333
|
+
);
|
|
334
|
+
return void 0;
|
|
335
|
+
}
|
|
336
|
+
const { markup } = buildStandaloneSvg(
|
|
337
|
+
layer,
|
|
338
|
+
container,
|
|
339
|
+
null
|
|
340
|
+
);
|
|
341
|
+
return loadSvgImage(markup).then((img) => {
|
|
342
|
+
ctx.drawImage(img, offsetX, offsetY, drawW, drawH);
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
const mime = format === "jpeg" ? "image/jpeg" : "image/png";
|
|
347
|
+
return chain.then(
|
|
348
|
+
() => offscreen.toDataURL(mime, format === "jpeg" ? quality : void 0)
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
function downloadChart(container, options) {
|
|
352
|
+
var _a;
|
|
353
|
+
const format = (options == null ? void 0 : options.format) ?? "png";
|
|
354
|
+
const rawName = ((_a = options == null ? void 0 : options.filename) == null ? void 0 : _a.trim()) || `aura-chart.${format}`;
|
|
355
|
+
const extRe = /\.(png|jpe?g|svg)$/i;
|
|
356
|
+
const filename = extRe.test(rawName) ? rawName : `${rawName}.${format}`;
|
|
357
|
+
return exportChartDataUrl(container, options).then((dataUrl) => {
|
|
358
|
+
if (typeof document === "undefined")
|
|
359
|
+
return dataUrl;
|
|
360
|
+
let objectUrl = null;
|
|
361
|
+
const blob = dataUrlToBlob(dataUrl);
|
|
362
|
+
if (blob && typeof URL !== "undefined" && typeof URL.createObjectURL === "function") {
|
|
363
|
+
objectUrl = URL.createObjectURL(blob);
|
|
364
|
+
}
|
|
365
|
+
const link = document.createElement("a");
|
|
366
|
+
link.href = objectUrl ?? dataUrl;
|
|
367
|
+
link.download = filename;
|
|
368
|
+
link.style.display = "none";
|
|
369
|
+
document.body.appendChild(link);
|
|
370
|
+
try {
|
|
371
|
+
link.click();
|
|
372
|
+
} finally {
|
|
373
|
+
document.body.removeChild(link);
|
|
374
|
+
if (objectUrl && typeof URL !== "undefined" && typeof URL.revokeObjectURL === "function") {
|
|
375
|
+
const urlToRevoke = objectUrl;
|
|
376
|
+
setTimeout(() => {
|
|
377
|
+
if (typeof URL !== "undefined" && typeof URL.revokeObjectURL === "function") {
|
|
378
|
+
URL.revokeObjectURL(urlToRevoke);
|
|
379
|
+
}
|
|
380
|
+
}, 0);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return dataUrl;
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
export {
|
|
387
|
+
downloadChart,
|
|
388
|
+
exportChartDataUrl,
|
|
389
|
+
exportChartSvg
|
|
390
|
+
};
|
package/esm/core/force.js
CHANGED
|
@@ -1,26 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
// src/core/force.ts
|
|
2
3
|
function pseudo(seed) {
|
|
3
4
|
const value = Math.sin(seed * 12.9898) * 43758.5453;
|
|
4
5
|
return value - Math.floor(value);
|
|
5
6
|
}
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 力导向布局(Fruchterman–Reingold)。
|
|
9
|
-
*
|
|
10
|
-
* 把图当成物理系统:节点两两相斥、有连边的相互吸引,
|
|
11
|
-
* 每轮按「温度」限制最大位移,温度线性冷却到 0 后收敛。
|
|
12
|
-
*
|
|
13
|
-
* 初始位置用「圆环 + 确定性扰动」而不是随机散布:
|
|
14
|
-
* 随机数会让同一份数据在每次刷新时长得不一样,也不利于截图对比;
|
|
15
|
-
* 圆环本身已经足够均匀,能让迭代更快收敛。
|
|
16
|
-
*/
|
|
17
|
-
export function layoutForce(nodes, links, rect, options = {}) {
|
|
7
|
+
function layoutForce(nodes, links, rect, options = {}) {
|
|
18
8
|
const count = nodes.length;
|
|
19
|
-
if (count === 0)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
radius = 8
|
|
23
|
-
} = options;
|
|
9
|
+
if (count === 0)
|
|
10
|
+
return [];
|
|
11
|
+
const { iterations = 220, radius = 8 } = options;
|
|
24
12
|
const indexOf = new Map(nodes.map((node, index) => [node.id, index]));
|
|
25
13
|
const xs = new Array(count);
|
|
26
14
|
const ys = new Array(count);
|
|
@@ -29,16 +17,12 @@ export function layoutForce(nodes, links, rect, options = {}) {
|
|
|
29
17
|
for (const link of links) {
|
|
30
18
|
const a = indexOf.get(link.source);
|
|
31
19
|
const b = indexOf.get(link.target);
|
|
32
|
-
if (a ===
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
b
|
|
36
|
-
});
|
|
20
|
+
if (a === void 0 || b === void 0 || a === b)
|
|
21
|
+
continue;
|
|
22
|
+
valid.push({ a, b });
|
|
37
23
|
degrees[a] += 1;
|
|
38
24
|
degrees[b] += 1;
|
|
39
25
|
}
|
|
40
|
-
|
|
41
|
-
// 初始:圆环 + 轻微扰动(单节点时居中)
|
|
42
26
|
const cx = rect.x + rect.width / 2;
|
|
43
27
|
const cy = rect.y + rect.height / 2;
|
|
44
28
|
const ringRadius = Math.min(rect.width, rect.height) * 0.34;
|
|
@@ -54,12 +38,7 @@ export function layoutForce(nodes, links, rect, options = {}) {
|
|
|
54
38
|
ys[index] = cy + Math.sin(angle) * (ringRadius + jitter);
|
|
55
39
|
}
|
|
56
40
|
if (count === 1) {
|
|
57
|
-
return [{
|
|
58
|
-
id: nodes[0].id,
|
|
59
|
-
x: cx,
|
|
60
|
-
y: cy,
|
|
61
|
-
degree: 0
|
|
62
|
-
}];
|
|
41
|
+
return [{ id: nodes[0].id, x: cx, y: cy, degree: 0 }];
|
|
63
42
|
}
|
|
64
43
|
const area = Math.max(1, rect.width * rect.height);
|
|
65
44
|
const k = Math.sqrt(area / count);
|
|
@@ -68,15 +47,12 @@ export function layoutForce(nodes, links, rect, options = {}) {
|
|
|
68
47
|
for (let step = 0; step < iterations; step += 1) {
|
|
69
48
|
const dx = new Array(count).fill(0);
|
|
70
49
|
const dy = new Array(count).fill(0);
|
|
71
|
-
|
|
72
|
-
// 斥力:所有节点两两排斥
|
|
73
50
|
for (let i = 0; i < count; i += 1) {
|
|
74
51
|
for (let j = i + 1; j < count; j += 1) {
|
|
75
52
|
let deltaX = xs[i] - xs[j];
|
|
76
53
|
let deltaY = ys[i] - ys[j];
|
|
77
54
|
let distance = Math.hypot(deltaX, deltaY);
|
|
78
55
|
if (distance < 0.01) {
|
|
79
|
-
// 完全重合时给一个确定性的偏移方向,避免除零
|
|
80
56
|
deltaX = (pseudo(i + j + 1) - 0.5) * 0.1;
|
|
81
57
|
deltaY = (pseudo(i + j + 2) - 0.5) * 0.1;
|
|
82
58
|
distance = 0.01;
|
|
@@ -90,12 +66,7 @@ export function layoutForce(nodes, links, rect, options = {}) {
|
|
|
90
66
|
dy[j] -= uy * force;
|
|
91
67
|
}
|
|
92
68
|
}
|
|
93
|
-
|
|
94
|
-
// 引力:有连边的相互吸引
|
|
95
|
-
for (const {
|
|
96
|
-
a,
|
|
97
|
-
b
|
|
98
|
-
} of valid) {
|
|
69
|
+
for (const { a, b } of valid) {
|
|
99
70
|
let deltaX = xs[a] - xs[b];
|
|
100
71
|
let deltaY = ys[a] - ys[b];
|
|
101
72
|
let distance = Math.hypot(deltaX, deltaY);
|
|
@@ -112,15 +83,19 @@ export function layoutForce(nodes, links, rect, options = {}) {
|
|
|
112
83
|
dx[b] += ux * force;
|
|
113
84
|
dy[b] += uy * force;
|
|
114
85
|
}
|
|
115
|
-
|
|
116
|
-
// 按温度限制位移,并夹在画布内
|
|
117
86
|
for (let index = 0; index < count; index += 1) {
|
|
118
87
|
const length = Math.hypot(dx[index], dy[index]) || 1;
|
|
119
88
|
const limited = Math.min(length, temperature);
|
|
120
89
|
xs[index] += dx[index] / length * limited;
|
|
121
90
|
ys[index] += dy[index] / length * limited;
|
|
122
|
-
xs[index] = Math.min(
|
|
123
|
-
|
|
91
|
+
xs[index] = Math.min(
|
|
92
|
+
rect.x + rect.width - margin,
|
|
93
|
+
Math.max(rect.x + margin, xs[index])
|
|
94
|
+
);
|
|
95
|
+
ys[index] = Math.min(
|
|
96
|
+
rect.y + rect.height - margin,
|
|
97
|
+
Math.max(rect.y + margin, ys[index])
|
|
98
|
+
);
|
|
124
99
|
}
|
|
125
100
|
temperature = Math.max(0.5, temperature * 0.975);
|
|
126
101
|
}
|
|
@@ -130,4 +105,7 @@ export function layoutForce(nodes, links, rect, options = {}) {
|
|
|
130
105
|
y: ys[index],
|
|
131
106
|
degree: degrees[index]
|
|
132
107
|
}));
|
|
133
|
-
}
|
|
108
|
+
}
|
|
109
|
+
export {
|
|
110
|
+
layoutForce
|
|
111
|
+
};
|
package/esm/core/format.js
CHANGED
|
@@ -1,80 +1,77 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* 图表里最容易出「廉价感」的地方就是刻度:`12000` 不如 `1.2万`,
|
|
5
|
-
* `0.30000000000000004` 更要命。这里统一收口。
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/** 去掉小数末尾多余的 0 */
|
|
1
|
+
'use client';
|
|
2
|
+
// src/core/format.ts
|
|
9
3
|
function trimZeros(text) {
|
|
10
|
-
return text.includes(
|
|
4
|
+
return text.includes(".") ? text.replace(/\.?0+$/, "") : text;
|
|
11
5
|
}
|
|
12
|
-
|
|
13
|
-
/** 根据数值量级推断合适的小数位 */
|
|
14
|
-
export function inferDecimals(values) {
|
|
6
|
+
function inferDecimals(values) {
|
|
15
7
|
let decimals = 0;
|
|
16
8
|
for (const value of values) {
|
|
17
|
-
if (!Number.isFinite(value))
|
|
9
|
+
if (!Number.isFinite(value))
|
|
10
|
+
continue;
|
|
18
11
|
const text = String(value);
|
|
19
|
-
const dot = text.indexOf(
|
|
20
|
-
if (dot >= 0)
|
|
12
|
+
const dot = text.indexOf(".");
|
|
13
|
+
if (dot >= 0)
|
|
14
|
+
decimals = Math.max(decimals, text.length - dot - 1);
|
|
21
15
|
}
|
|
22
16
|
return Math.min(decimals, 4);
|
|
23
17
|
}
|
|
24
|
-
|
|
25
|
-
/** 紧凑单位:中文场景用「万 / 亿」,符合国内看板习惯 */
|
|
26
|
-
export function formatCompact(value, decimals = 1) {
|
|
18
|
+
function formatCompact(value, decimals = 1) {
|
|
27
19
|
const abs = Math.abs(value);
|
|
28
|
-
const sign = value < 0 ?
|
|
29
|
-
if (abs >= 1e8)
|
|
30
|
-
|
|
31
|
-
if (abs >=
|
|
20
|
+
const sign = value < 0 ? "-" : "";
|
|
21
|
+
if (abs >= 1e8)
|
|
22
|
+
return `${sign}${trimZeros((abs / 1e8).toFixed(decimals))}亿`;
|
|
23
|
+
if (abs >= 1e4)
|
|
24
|
+
return `${sign}${trimZeros((abs / 1e4).toFixed(decimals))}万`;
|
|
25
|
+
if (abs >= 1e3)
|
|
26
|
+
return `${sign}${trimZeros((abs / 1e3).toFixed(decimals))}k`;
|
|
32
27
|
return `${sign}${trimZeros(String(abs))}`;
|
|
33
28
|
}
|
|
34
|
-
|
|
35
|
-
if (!Number.isFinite(value))
|
|
29
|
+
function formatNumber(value, options = {}) {
|
|
30
|
+
if (!Number.isFinite(value))
|
|
31
|
+
return "—";
|
|
36
32
|
const {
|
|
37
33
|
decimals,
|
|
38
34
|
compact = false,
|
|
39
|
-
prefix =
|
|
40
|
-
suffix =
|
|
35
|
+
prefix = "",
|
|
36
|
+
suffix = "",
|
|
41
37
|
thousands = true
|
|
42
38
|
} = options;
|
|
43
|
-
if (compact)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
39
|
+
if (compact)
|
|
40
|
+
return `${prefix}${formatCompact(value, decimals ?? 1)}${suffix}`;
|
|
41
|
+
const fixed = decimals === void 0 ? trimZeros(String(value)) : value.toFixed(decimals);
|
|
42
|
+
if (!thousands)
|
|
43
|
+
return `${prefix}${fixed}${suffix}`;
|
|
44
|
+
const [intPart, decimalPart] = fixed.split(".");
|
|
45
|
+
const withSeparator = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
48
46
|
return `${prefix}${decimalPart ? `${withSeparator}.${decimalPart}` : withSeparator}${suffix}`;
|
|
49
47
|
}
|
|
50
|
-
|
|
51
|
-
if (!total)
|
|
48
|
+
function formatPercent(value, total, decimals = 1) {
|
|
49
|
+
if (!total)
|
|
50
|
+
return "0%";
|
|
52
51
|
return `${trimZeros((value / total * 100).toFixed(decimals))}%`;
|
|
53
52
|
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* 粗略估算文本像素宽度。
|
|
57
|
-
*
|
|
58
|
-
* 为什么要估算而不是真实测量:布局阶段(计算 margin、判断标签是否重叠)
|
|
59
|
-
* 发生在渲染之前,此时没有 DOM 可测量。用「CJK 记 1em、其余记 0.58em」
|
|
60
|
-
* 的经验值足够可靠,误差被 8px 的留白吸收。
|
|
61
|
-
*/
|
|
62
|
-
export function estimateTextWidth(text, fontSize) {
|
|
53
|
+
function estimateTextWidth(text, fontSize) {
|
|
63
54
|
let width = 0;
|
|
64
55
|
for (const char of text) {
|
|
65
56
|
const code = char.codePointAt(0) ?? 0;
|
|
66
|
-
|
|
67
|
-
const isWide = code >= 0x2e80 && code <= 0x9fff || code >= 0xac00 && code <= 0xd7af || code >= 0xf900 && code <= 0xfaff || code >= 0xff00 && code <= 0xff60 || code >= 0xffe0 && code <= 0xffe6;
|
|
57
|
+
const isWide = code >= 11904 && code <= 40959 || code >= 44032 && code <= 55215 || code >= 63744 && code <= 64255 || code >= 65280 && code <= 65376 || code >= 65504 && code <= 65510;
|
|
68
58
|
width += isWide ? fontSize : fontSize * 0.58;
|
|
69
59
|
}
|
|
70
60
|
return width;
|
|
71
61
|
}
|
|
72
|
-
|
|
73
|
-
/** 数值补零宽度,用于让刻度标签右对齐时视觉整齐 */
|
|
74
|
-
export function padNumberLabels(labels) {
|
|
62
|
+
function padNumberLabels(labels) {
|
|
75
63
|
const width = labels.reduce((max, item) => Math.max(max, item.length), 0);
|
|
76
|
-
if (width === 0)
|
|
77
|
-
|
|
78
|
-
if (!labels.every(item => /^-?[\d,.]+$/.test(item)))
|
|
79
|
-
|
|
80
|
-
|
|
64
|
+
if (width === 0)
|
|
65
|
+
return labels;
|
|
66
|
+
if (!labels.every((item) => /^-?[\d,.]+$/.test(item)))
|
|
67
|
+
return labels;
|
|
68
|
+
return labels.map((item) => item.padStart(width, " "));
|
|
69
|
+
}
|
|
70
|
+
export {
|
|
71
|
+
estimateTextWidth,
|
|
72
|
+
formatCompact,
|
|
73
|
+
formatNumber,
|
|
74
|
+
formatPercent,
|
|
75
|
+
inferDecimals,
|
|
76
|
+
padNumberLabels
|
|
77
|
+
};
|
package/esm/core/hierarchy.d.ts
CHANGED