@aura-react-comp/charts 0.3.1 → 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.js +27 -23
- package/esm/charts/bar-chart/index.js +17 -21
- package/esm/charts/candlestick-chart/index.js +242 -180
- package/esm/charts/combo-chart/index.js +8 -20
- package/esm/charts/funnel-chart/index.js +133 -108
- package/esm/charts/gauge-chart/index.js +102 -105
- package/esm/charts/graph-chart/index.js +154 -114
- package/esm/charts/heatmap-chart/index.js +179 -135
- package/esm/charts/line-chart/index.js +28 -24
- package/esm/charts/pie-chart/index.d.ts.map +1 -1
- package/esm/charts/pie-chart/index.js +198 -139
- package/esm/charts/radar-chart/index.d.ts.map +1 -1
- package/esm/charts/radar-chart/index.js +154 -128
- package/esm/charts/ring-progress/index.js +81 -82
- package/esm/charts/sankey-chart/index.js +171 -141
- package/esm/charts/scatter-chart/index.d.ts.map +1 -1
- package/esm/charts/scatter-chart/index.js +225 -159
- package/esm/charts/sparkline/index.js +81 -66
- package/esm/charts/tree-chart/index.js +128 -102
- package/esm/charts/treemap-chart/index.js +162 -141
- package/esm/charts/waterfall-chart/index.js +165 -121
- package/esm/components/Legend.d.ts +3 -2
- package/esm/components/Legend.d.ts.map +1 -1
- package/esm/components/Legend.js +79 -48
- package/esm/components/Tooltip.js +62 -71
- package/esm/components/chart/index.d.ts +4 -1
- package/esm/components/chart/index.d.ts.map +1 -1
- package/esm/components/chart/index.js +80 -102
- package/esm/components/chart.less +13 -0
- package/esm/core/animation.js +31 -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 +22 -45
- package/esm/core/format.js +48 -52
- package/esm/core/hierarchy.js +60 -92
- package/esm/core/layout.js +32 -50
- package/esm/core/sampling.js +30 -53
- package/esm/core/sankey.js +35 -55
- package/esm/core/scale.js +72 -86
- package/esm/core/shape.js +101 -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.js +17 -19
- package/esm/hooks/useChartTransition.js +58 -73
- package/esm/hooks/useElementSize.js +19 -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 +6 -0
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +136 -47
- package/esm/internal/CartesianChart.d.ts.map +1 -1
- package/esm/internal/CartesianChart.js +666 -485
- package/esm/internal/model.js +44 -60
- package/esm/scene/canvas.js +100 -107
- package/esm/scene/svg.js +106 -99
- package/esm/scene/types.js +8 -15
- package/esm/style.css +10 -0
- package/esm/theme/index.js +143 -171
- package/esm/types.d.ts +4 -0
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js +0 -1
- package/package.json +1 -1
package/esm/core/scale.js
CHANGED
|
@@ -1,79 +1,66 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* 1. 纯函数、零 DOM 依赖 —— 可在 SSR / 测试环境中直接跑;
|
|
7
|
-
* 2. 只描述「数据域 ↔ 像素域」的映射关系,不关心渲染方式,
|
|
8
|
-
* 因此同一套 scale 可同时供 SVG 与 Canvas 渲染器消费;
|
|
9
|
-
* 3. 刻度算法不引入 d3-scale 依赖(包体积 + 版本风险),
|
|
10
|
-
* 但采用与之等价的「1 / 2 / 2.5 / 5 / 10 × 10^n」取整策略,
|
|
11
|
-
* 保证出图结果符合工程直觉。
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/** 数据域 */
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 把任意步长收敛到「人类友好」的值:
|
|
18
|
-
* 1.0 / 2.0 / 2.5 / 5.0 × 10^n。
|
|
19
|
-
*/
|
|
20
|
-
export function niceStep(rawStep) {
|
|
21
|
-
if (!Number.isFinite(rawStep) || rawStep <= 0) return 1;
|
|
2
|
+
// src/core/scale.ts
|
|
3
|
+
function niceStep(rawStep) {
|
|
4
|
+
if (!Number.isFinite(rawStep) || rawStep <= 0)
|
|
5
|
+
return 1;
|
|
22
6
|
const exponent = Math.floor(Math.log10(rawStep));
|
|
23
7
|
const base = 10 ** exponent;
|
|
24
8
|
const fraction = rawStep / base;
|
|
25
9
|
let niceFraction;
|
|
26
|
-
if (fraction <= 1)
|
|
10
|
+
if (fraction <= 1)
|
|
11
|
+
niceFraction = 1;
|
|
12
|
+
else if (fraction <= 2)
|
|
13
|
+
niceFraction = 2;
|
|
14
|
+
else if (fraction <= 2.5)
|
|
15
|
+
niceFraction = 2.5;
|
|
16
|
+
else if (fraction <= 5)
|
|
17
|
+
niceFraction = 5;
|
|
18
|
+
else
|
|
19
|
+
niceFraction = 10;
|
|
27
20
|
return niceFraction * base;
|
|
28
21
|
}
|
|
29
|
-
|
|
30
|
-
/** 浮点累加会产生 0.30000000000000004,统一按步长精度截断 */
|
|
31
22
|
function roundToStep(value, step) {
|
|
32
23
|
const decimals = Math.max(0, -Math.floor(Math.log10(step)) + 1);
|
|
33
24
|
return Number(value.toFixed(Math.min(12, decimals)));
|
|
34
25
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
*/
|
|
41
|
-
export function linearTicks(min, max, count = 5) {
|
|
42
|
-
if (!Number.isFinite(min) || !Number.isFinite(max)) return [];
|
|
43
|
-
if (min === max) return [min];
|
|
26
|
+
function linearTicks(min, max, count = 5) {
|
|
27
|
+
if (!Number.isFinite(min) || !Number.isFinite(max))
|
|
28
|
+
return [];
|
|
29
|
+
if (min === max)
|
|
30
|
+
return [min];
|
|
44
31
|
const step = niceStep((max - min) / Math.max(1, count));
|
|
45
32
|
const start = Math.ceil(min / step) * step;
|
|
46
33
|
const out = [];
|
|
47
|
-
// 容忍浮点误差,避免边界刻度被漏掉
|
|
48
34
|
const epsilon = step * 1e-9;
|
|
49
35
|
for (let value = start; value <= max + epsilon; value += step) {
|
|
50
36
|
out.push(roundToStep(value, step));
|
|
51
37
|
}
|
|
52
38
|
return out;
|
|
53
39
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (!Number.isFinite(min) || !Number.isFinite(max)) return [0, 1];
|
|
40
|
+
function niceExtent(min, max, count = 5) {
|
|
41
|
+
if (!Number.isFinite(min) || !Number.isFinite(max))
|
|
42
|
+
return [0, 1];
|
|
58
43
|
if (min === max) {
|
|
59
|
-
// 退化情况:给一个对称的呼吸空间,避免 0 高度图
|
|
60
44
|
const pad = Math.abs(min) || 1;
|
|
61
45
|
return [min - pad * 0.5, max + pad * 0.5];
|
|
62
46
|
}
|
|
63
47
|
const step = niceStep((max - min) / Math.max(1, count));
|
|
64
|
-
return [
|
|
48
|
+
return [
|
|
49
|
+
roundToStep(Math.floor(min / step) * step, step),
|
|
50
|
+
roundToStep(Math.ceil(max / step) * step, step)
|
|
51
|
+
];
|
|
65
52
|
}
|
|
66
|
-
|
|
53
|
+
function createLinearScale(options) {
|
|
67
54
|
let [min, max] = options.domain;
|
|
68
|
-
|
|
69
|
-
|
|
55
|
+
if (min === max)
|
|
56
|
+
max = min + 1;
|
|
70
57
|
const [r0, r1] = options.range;
|
|
71
58
|
const build = (domain, range, tickStep) => {
|
|
72
59
|
const [d0, d1] = domain;
|
|
73
60
|
const [p0, p1] = range;
|
|
74
61
|
const span = d1 - d0 || 1;
|
|
75
|
-
const scaleFn = value => p0 + (value - d0) / span * (p1 - p0);
|
|
76
|
-
const invertFn = px => d0 + (px - p0) / (p1 - p0 || 1) * span;
|
|
62
|
+
const scaleFn = (value) => p0 + (value - d0) / span * (p1 - p0);
|
|
63
|
+
const invertFn = (px) => d0 + (px - p0) / (p1 - p0 || 1) * span;
|
|
77
64
|
const ticksFn = (count = 5) => {
|
|
78
65
|
if (tickStep) {
|
|
79
66
|
const out = [];
|
|
@@ -86,7 +73,7 @@ export function createLinearScale(options) {
|
|
|
86
73
|
return linearTicks(d0, d1, count);
|
|
87
74
|
};
|
|
88
75
|
const self = {
|
|
89
|
-
type:
|
|
76
|
+
type: "linear",
|
|
90
77
|
domain,
|
|
91
78
|
range,
|
|
92
79
|
scale: scaleFn,
|
|
@@ -98,10 +85,10 @@ export function createLinearScale(options) {
|
|
|
98
85
|
},
|
|
99
86
|
copy(overrides) {
|
|
100
87
|
return createLinearScale({
|
|
101
|
-
domain: overrides
|
|
102
|
-
range: overrides
|
|
103
|
-
nice: overrides
|
|
104
|
-
tickStep: overrides
|
|
88
|
+
domain: (overrides == null ? void 0 : overrides.domain) ?? domain,
|
|
89
|
+
range: (overrides == null ? void 0 : overrides.range) ?? range,
|
|
90
|
+
nice: overrides == null ? void 0 : overrides.nice,
|
|
91
|
+
tickStep: (overrides == null ? void 0 : overrides.tickStep) ?? tickStep
|
|
105
92
|
});
|
|
106
93
|
}
|
|
107
94
|
};
|
|
@@ -109,19 +96,11 @@ export function createLinearScale(options) {
|
|
|
109
96
|
};
|
|
110
97
|
const base = build([min, max], [r0, r1], options.tickStep);
|
|
111
98
|
if (options.nice) {
|
|
112
|
-
return base.nice(typeof options.nice ===
|
|
99
|
+
return base.nice(typeof options.nice === "number" ? options.nice : 5);
|
|
113
100
|
}
|
|
114
101
|
return base;
|
|
115
102
|
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* 分类坐标(band):每个分类占一个等宽区间,柱子/分组柱靠它定位。
|
|
119
|
-
*
|
|
120
|
-
* 公式与 d3.scaleBand 一致(align 固定 0.5,即整体居中):
|
|
121
|
-
* step = range / (n - paddingInner + paddingOuter * 2)
|
|
122
|
-
* bandWidth = step * (1 - paddingInner)
|
|
123
|
-
*/
|
|
124
|
-
export function createBandScale(options) {
|
|
103
|
+
function createBandScale(options) {
|
|
125
104
|
const domain = options.domain;
|
|
126
105
|
const [r0, r1] = options.range;
|
|
127
106
|
const paddingInner = clamp(options.paddingInner ?? 0.2);
|
|
@@ -131,32 +110,29 @@ export function createBandScale(options) {
|
|
|
131
110
|
const step = n > 0 ? span / Math.max(1, n - paddingInner + paddingOuter * 2) : 0;
|
|
132
111
|
const bandWidth = Math.max(0, step * (1 - paddingInner));
|
|
133
112
|
const start = r0 + (span - step * (n - paddingInner)) / 2;
|
|
134
|
-
const index = new Map();
|
|
113
|
+
const index = /* @__PURE__ */ new Map();
|
|
135
114
|
domain.forEach((key, i) => {
|
|
136
|
-
if (!index.has(key))
|
|
115
|
+
if (!index.has(key))
|
|
116
|
+
index.set(key, i);
|
|
137
117
|
});
|
|
138
118
|
return {
|
|
139
|
-
type:
|
|
119
|
+
type: "band",
|
|
140
120
|
domain,
|
|
141
121
|
range: options.range,
|
|
142
122
|
bandWidth,
|
|
143
123
|
step,
|
|
144
124
|
scale(value) {
|
|
145
125
|
const i = index.get(value);
|
|
146
|
-
return i ===
|
|
126
|
+
return i === void 0 ? void 0 : start + step * i;
|
|
147
127
|
},
|
|
148
128
|
center(value) {
|
|
149
129
|
const i = index.get(value);
|
|
150
|
-
return i ===
|
|
130
|
+
return i === void 0 ? void 0 : start + step * i + bandWidth / 2;
|
|
151
131
|
},
|
|
152
132
|
ticks: () => [...domain]
|
|
153
133
|
};
|
|
154
134
|
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* 点坐标(point):分类落在刻度线上而非区间内,折线图/散点图用它。
|
|
158
|
-
*/
|
|
159
|
-
export function createPointScale(options) {
|
|
135
|
+
function createPointScale(options) {
|
|
160
136
|
const domain = options.domain;
|
|
161
137
|
const [r0, r1] = options.range;
|
|
162
138
|
const padding = clamp(options.paddingOuter ?? 0.5);
|
|
@@ -165,17 +141,19 @@ export function createPointScale(options) {
|
|
|
165
141
|
const denom = Math.max(1, n - 1 + padding * 2);
|
|
166
142
|
const step = n > 1 ? span / denom : 0;
|
|
167
143
|
const start = n > 1 ? r0 + step * padding : (r0 + r1) / 2;
|
|
168
|
-
const index = new Map();
|
|
144
|
+
const index = /* @__PURE__ */ new Map();
|
|
169
145
|
domain.forEach((key, i) => {
|
|
170
|
-
if (!index.has(key))
|
|
146
|
+
if (!index.has(key))
|
|
147
|
+
index.set(key, i);
|
|
171
148
|
});
|
|
172
149
|
return {
|
|
173
|
-
type:
|
|
150
|
+
type: "point",
|
|
174
151
|
domain,
|
|
175
152
|
range: options.range,
|
|
176
153
|
scale(value) {
|
|
177
154
|
const i = index.get(value);
|
|
178
|
-
if (i ===
|
|
155
|
+
if (i === void 0)
|
|
156
|
+
return void 0;
|
|
179
157
|
return n > 1 ? start + step * i : (r0 + r1) / 2;
|
|
180
158
|
},
|
|
181
159
|
ticks: () => [...domain]
|
|
@@ -184,24 +162,32 @@ export function createPointScale(options) {
|
|
|
184
162
|
function clamp(value, min = 0, max = 1) {
|
|
185
163
|
return Math.min(max, Math.max(min, value));
|
|
186
164
|
}
|
|
187
|
-
|
|
188
|
-
/** 根据一组数值求 [min, max],忽略 null / NaN */
|
|
189
|
-
export function extentOf(values) {
|
|
165
|
+
function extentOf(values) {
|
|
190
166
|
let min = Infinity;
|
|
191
167
|
let max = -Infinity;
|
|
192
168
|
for (const value of values) {
|
|
193
|
-
if (value === null || value ===
|
|
194
|
-
|
|
195
|
-
if (value
|
|
169
|
+
if (value === null || value === void 0 || Number.isNaN(value))
|
|
170
|
+
continue;
|
|
171
|
+
if (value < min)
|
|
172
|
+
min = value;
|
|
173
|
+
if (value > max)
|
|
174
|
+
max = value;
|
|
196
175
|
}
|
|
197
|
-
if (min === Infinity)
|
|
176
|
+
if (min === Infinity)
|
|
177
|
+
return [0, 1];
|
|
198
178
|
return [min, max];
|
|
199
179
|
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* 数值轴默认要包含 0 基线(柱状图必须如此,否则柱子长度失真)。
|
|
203
|
-
*/
|
|
204
|
-
export function extentWithZero(values) {
|
|
180
|
+
function extentWithZero(values) {
|
|
205
181
|
const [min, max] = extentOf(values);
|
|
206
182
|
return [Math.min(0, min), Math.max(0, max)];
|
|
207
|
-
}
|
|
183
|
+
}
|
|
184
|
+
export {
|
|
185
|
+
createBandScale,
|
|
186
|
+
createLinearScale,
|
|
187
|
+
createPointScale,
|
|
188
|
+
extentOf,
|
|
189
|
+
extentWithZero,
|
|
190
|
+
linearTicks,
|
|
191
|
+
niceExtent,
|
|
192
|
+
niceStep
|
|
193
|
+
};
|
package/esm/core/shape.js
CHANGED
|
@@ -1,41 +1,25 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
* 所有图表最终都落到「一段 SVG path 字符串」这一层:
|
|
6
|
-
* · SVG 渲染器把它直接写进 `<path d>`;
|
|
7
|
-
* · Canvas 渲染器把它交给 `new Path2D(d)`。
|
|
8
|
-
* 两条渲染路径共用同一份几何计算,因此不存在「SVG 与 Canvas 画得不一样」的问题。
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
/** 曲线上的点允许为 null,表示此处数据缺失、路径应断开 */
|
|
12
|
-
|
|
13
|
-
const f = value => Number.isFinite(value) ? String(Math.round(value * 100) / 100) : '0';
|
|
14
|
-
|
|
15
|
-
/** 把点序列拆成若干连续段,段与段之间是 null 造成的断点 */
|
|
16
|
-
export function splitSegments(points) {
|
|
2
|
+
// src/core/shape.ts
|
|
3
|
+
var f = (value) => Number.isFinite(value) ? String(Math.round(value * 100) / 100) : "0";
|
|
4
|
+
function splitSegments(points) {
|
|
17
5
|
const segments = [];
|
|
18
6
|
let current = [];
|
|
19
7
|
for (const point of points) {
|
|
20
|
-
if (point)
|
|
8
|
+
if (point)
|
|
9
|
+
current.push(point);
|
|
10
|
+
else if (current.length) {
|
|
21
11
|
segments.push(current);
|
|
22
12
|
current = [];
|
|
23
13
|
}
|
|
24
14
|
}
|
|
25
|
-
if (current.length)
|
|
15
|
+
if (current.length)
|
|
16
|
+
segments.push(current);
|
|
26
17
|
return segments;
|
|
27
18
|
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Fritsch–Carlson 单调切线。
|
|
31
|
-
*
|
|
32
|
-
* 目的:让平滑折线在数据极值处不「鼓包」。
|
|
33
|
-
* 普通 Catmull-Rom 会让曲线冲出数据范围(视觉上像在说谎),
|
|
34
|
-
* 折线图必须避免这种失真,因此默认曲线类型用的是 monotone。
|
|
35
|
-
*/
|
|
36
19
|
function monotoneTangents(xs, ys) {
|
|
37
20
|
const n = xs.length;
|
|
38
|
-
if (n < 2)
|
|
21
|
+
if (n < 2)
|
|
22
|
+
return [0];
|
|
39
23
|
const dx = [];
|
|
40
24
|
const slopes = [];
|
|
41
25
|
for (let i = 0; i < n - 1; i += 1) {
|
|
@@ -50,7 +34,7 @@ function monotoneTangents(xs, ys) {
|
|
|
50
34
|
const s0 = slopes[i - 1];
|
|
51
35
|
const s1 = slopes[i];
|
|
52
36
|
if (s0 * s1 <= 0) {
|
|
53
|
-
m[i] = 0;
|
|
37
|
+
m[i] = 0;
|
|
54
38
|
} else {
|
|
55
39
|
const w1 = 2 * dx[i] + dx[i - 1];
|
|
56
40
|
const w2 = dx[i] + 2 * dx[i - 1];
|
|
@@ -59,43 +43,33 @@ function monotoneTangents(xs, ys) {
|
|
|
59
43
|
}
|
|
60
44
|
return m;
|
|
61
45
|
}
|
|
62
|
-
|
|
63
|
-
/** 单个点序列 → path 命令(不含 M 之外的前缀处理) */
|
|
64
46
|
function segmentToPath(points, curve) {
|
|
65
47
|
if (points.length === 1) {
|
|
66
|
-
|
|
67
|
-
const {
|
|
68
|
-
x,
|
|
69
|
-
y
|
|
70
|
-
} = points[0];
|
|
48
|
+
const { x, y } = points[0];
|
|
71
49
|
return `M${f(x)},${f(y)}L${f(x)},${f(y)}`;
|
|
72
50
|
}
|
|
73
|
-
if (curve ===
|
|
74
|
-
return points.map((p, i) => `${i === 0 ?
|
|
51
|
+
if (curve === "linear") {
|
|
52
|
+
return points.map((p, i) => `${i === 0 ? "M" : "L"}${f(p.x)},${f(p.y)}`).join("");
|
|
75
53
|
}
|
|
76
|
-
if (curve ===
|
|
77
|
-
let
|
|
54
|
+
if (curve === "step") {
|
|
55
|
+
let d2 = `M${f(points[0].x)},${f(points[0].y)}`;
|
|
78
56
|
for (let i = 1; i < points.length; i += 1) {
|
|
79
57
|
const prev = points[i - 1];
|
|
80
58
|
const curr = points[i];
|
|
81
59
|
const midX = (prev.x + curr.x) / 2;
|
|
82
|
-
|
|
60
|
+
d2 += `H${f(midX)}V${f(curr.y)}H${f(curr.x)}`;
|
|
83
61
|
}
|
|
84
|
-
return
|
|
62
|
+
return d2;
|
|
85
63
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const xs = points.map(p => p.x);
|
|
89
|
-
const ys = points.map(p => p.y);
|
|
64
|
+
const xs = points.map((p) => p.x);
|
|
65
|
+
const ys = points.map((p) => p.y);
|
|
90
66
|
const tangents = monotoneTangents(xs, ys);
|
|
91
67
|
let d = `M${f(points[0].x)},${f(points[0].y)}`;
|
|
92
68
|
for (let i = 0; i < points.length - 1; i += 1) {
|
|
93
69
|
const p0 = points[i];
|
|
94
70
|
const p1 = points[i + 1];
|
|
95
71
|
const dx = (p1.x - p0.x) / 3;
|
|
96
|
-
|
|
97
|
-
// smooth 在单调切线基础上放大 20%,视觉更「圆润」但仍不越界
|
|
98
|
-
const grow = curve === 'smooth' ? 1.2 : 1;
|
|
72
|
+
const grow = curve === "smooth" ? 1.2 : 1;
|
|
99
73
|
const c1x = p0.x + dx;
|
|
100
74
|
const c1y = p0.y + tangents[i] * dx * grow;
|
|
101
75
|
const c2x = p1.x - dx;
|
|
@@ -104,43 +78,29 @@ function segmentToPath(points, curve) {
|
|
|
104
78
|
}
|
|
105
79
|
return d;
|
|
106
80
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
export function linePath(points, curve = 'monotone') {
|
|
110
|
-
return splitSegments(points).map(segment => segmentToPath(segment, curve)).filter(Boolean).join('');
|
|
81
|
+
function linePath(points, curve = "monotone") {
|
|
82
|
+
return splitSegments(points).map((segment) => segmentToPath(segment, curve)).filter(Boolean).join("");
|
|
111
83
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
* @param points 上边界点集
|
|
117
|
-
* @param baseline 基线像素 y(通常是 0 值所在位置)
|
|
118
|
-
*/
|
|
119
|
-
export function areaPath(points, baseline, curve = 'monotone') {
|
|
120
|
-
return splitSegments(points).map(segment => {
|
|
121
|
-
if (segment.length === 0) return '';
|
|
84
|
+
function areaPath(points, baseline, curve = "monotone") {
|
|
85
|
+
return splitSegments(points).map((segment) => {
|
|
86
|
+
if (segment.length === 0)
|
|
87
|
+
return "";
|
|
122
88
|
const top = segmentToPath(segment, curve);
|
|
123
89
|
const last = segment[segment.length - 1];
|
|
124
90
|
const first = segment[0];
|
|
125
91
|
return `${top}L${f(last.x)},${f(baseline)}L${f(first.x)},${f(baseline)}Z`;
|
|
126
|
-
}).filter(Boolean).join(
|
|
92
|
+
}).filter(Boolean).join("");
|
|
127
93
|
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* 扇形路径(饼图 / 环图 / 玫瑰图)。
|
|
131
|
-
*
|
|
132
|
-
* @param cx 圆心 x
|
|
133
|
-
* @param cy 圆心 y
|
|
134
|
-
* @param innerRadius 内半径,大于 0 即为环形
|
|
135
|
-
* @param outerRadius 外半径
|
|
136
|
-
* @param startAngle 起始角(弧度,0 指向 3 点钟方向,顺时针为正)
|
|
137
|
-
* @param endAngle 结束角
|
|
138
|
-
*/
|
|
139
|
-
export function arcPath(cx, cy, innerRadius, outerRadius, startAngle, endAngle) {
|
|
94
|
+
function arcPath(cx, cy, innerRadius, outerRadius, startAngle, endAngle) {
|
|
140
95
|
const sweep = endAngle - startAngle;
|
|
141
|
-
// 整圆无法用单段 A 命令表达(起终点重合),拆成两个半圆
|
|
142
96
|
if (Math.abs(sweep) >= Math.PI * 2 - 1e-6) {
|
|
143
|
-
return [
|
|
97
|
+
return [
|
|
98
|
+
`M${f(cx - outerRadius)},${f(cy)}`,
|
|
99
|
+
`A${f(outerRadius)},${f(outerRadius)} 0 1 1 ${f(cx + outerRadius)},${f(cy)}`,
|
|
100
|
+
`A${f(outerRadius)},${f(outerRadius)} 0 1 1 ${f(cx - outerRadius)},${f(cy)}`,
|
|
101
|
+
innerRadius > 0 ? `M${f(cx - innerRadius)},${f(cy)}A${f(innerRadius)},${f(innerRadius)} 0 1 0 ${f(cx + innerRadius)},${f(cy)}A${f(innerRadius)},${f(innerRadius)} 0 1 0 ${f(cx - innerRadius)},${f(cy)}` : "",
|
|
102
|
+
"Z"
|
|
103
|
+
].join("");
|
|
144
104
|
}
|
|
145
105
|
const largeArc = Math.abs(sweep) > Math.PI ? 1 : 0;
|
|
146
106
|
const direction = sweep > 0 ? 1 : 0;
|
|
@@ -155,82 +115,81 @@ export function arcPath(cx, cy, innerRadius, outerRadius, startAngle, endAngle)
|
|
|
155
115
|
const iy1 = cy + innerRadius * Math.sin(endAngle);
|
|
156
116
|
const ix0 = cx + innerRadius * Math.cos(startAngle);
|
|
157
117
|
const iy0 = cy + innerRadius * Math.sin(startAngle);
|
|
158
|
-
return [
|
|
118
|
+
return [
|
|
119
|
+
`M${f(x0)},${f(y0)}`,
|
|
120
|
+
`A${f(outerRadius)},${f(outerRadius)} 0 ${largeArc} ${direction} ${f(x1)},${f(y1)}`,
|
|
121
|
+
`L${f(ix1)},${f(iy1)}`,
|
|
122
|
+
`A${f(innerRadius)},${f(innerRadius)} 0 ${largeArc} ${direction === 1 ? 0 : 1} ${f(ix0)},${f(iy0)}`,
|
|
123
|
+
"Z"
|
|
124
|
+
].join("");
|
|
159
125
|
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* 圆角矩形路径。
|
|
163
|
-
*
|
|
164
|
-
* 柱状图的圆角通常只在上方两个角(负值柱则在下两个角),
|
|
165
|
-
* 因此用 corners 精确控制,而不是简单给 rect 加 rx(那会四角全圆)。
|
|
166
|
-
*/
|
|
167
|
-
export function roundedRectPath(x, y, width, height, radius, corners = {
|
|
126
|
+
function roundedRectPath(x, y, width, height, radius, corners = {
|
|
168
127
|
tl: true,
|
|
169
128
|
tr: true
|
|
170
129
|
}) {
|
|
171
|
-
const r = Math.max(
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
} = corners;
|
|
130
|
+
const r = Math.max(
|
|
131
|
+
0,
|
|
132
|
+
Math.min(radius, Math.abs(width) / 2, Math.abs(height) / 2)
|
|
133
|
+
);
|
|
134
|
+
if (r === 0)
|
|
135
|
+
return `M${f(x)},${f(y)}H${f(x + width)}V${f(y + height)}H${f(x)}Z`;
|
|
136
|
+
const { tl = true, tr = true, br = false, bl = false } = corners;
|
|
179
137
|
const rtl = tl ? r : 0;
|
|
180
138
|
const rtr = tr ? r : 0;
|
|
181
139
|
const rbr = br ? r : 0;
|
|
182
140
|
const rbl = bl ? r : 0;
|
|
183
141
|
const x1 = x + width;
|
|
184
142
|
const y1 = y + height;
|
|
185
|
-
return [
|
|
143
|
+
return [
|
|
144
|
+
`M${f(x + rtl)},${f(y)}`,
|
|
145
|
+
`H${f(x1 - rtr)}`,
|
|
146
|
+
rtr ? `A${f(rtr)},${f(rtr)} 0 0 1 ${f(x1)},${f(y + rtr)}` : "",
|
|
147
|
+
`V${f(y1 - rbr)}`,
|
|
148
|
+
rbr ? `A${f(rbr)},${f(rbr)} 0 0 1 ${f(x1 - rbr)},${f(y1)}` : "",
|
|
149
|
+
`H${f(x + rbl)}`,
|
|
150
|
+
rbl ? `A${f(rbl)},${f(rbl)} 0 0 1 ${f(x)},${f(y1 - rbl)}` : "",
|
|
151
|
+
`V${f(y + rtl)}`,
|
|
152
|
+
rtl ? `A${f(rtl)},${f(rtl)} 0 0 1 ${f(x + rtl)},${f(y)}` : "",
|
|
153
|
+
"Z"
|
|
154
|
+
].join("");
|
|
186
155
|
}
|
|
187
|
-
|
|
188
|
-
/** 散点图形符号 */
|
|
189
|
-
|
|
190
|
-
export function symbolPath(type, cx, cy, size) {
|
|
156
|
+
function symbolPath(type, cx, cy, size) {
|
|
191
157
|
const s = size / 2;
|
|
192
158
|
switch (type) {
|
|
193
|
-
case
|
|
159
|
+
case "square":
|
|
194
160
|
return `M${f(cx - s)},${f(cy - s)}H${f(cx + s)}V${f(cy + s)}H${f(cx - s)}Z`;
|
|
195
|
-
case
|
|
161
|
+
case "diamond":
|
|
196
162
|
return `M${f(cx)},${f(cy - s)}L${f(cx + s)},${f(cy)}L${f(cx)},${f(cy + s)}L${f(cx - s)},${f(cy)}Z`;
|
|
197
|
-
case
|
|
163
|
+
case "triangle":
|
|
198
164
|
return `M${f(cx)},${f(cy - s)}L${f(cx + s)},${f(cy + s)}L${f(cx - s)},${f(cy + s)}Z`;
|
|
199
|
-
case
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
case 'circle':
|
|
215
|
-
default:
|
|
216
|
-
{
|
|
217
|
-
// 用两段圆弧表达圆形,保证与 rect/path 渲染器行为一致
|
|
218
|
-
return `M${f(cx - s)},${f(cy)}A${f(s)},${f(s)} 0 1 0 ${f(cx + s)},${f(cy)}A${f(s)},${f(s)} 0 1 0 ${f(cx - s)},${f(cy)}Z`;
|
|
165
|
+
case "cross": {
|
|
166
|
+
const t = s / 3;
|
|
167
|
+
return [
|
|
168
|
+
`M${f(cx - s)},${f(cy - t)}H${f(cx - t)}V${f(cy - s)}H${f(cx + t)}V${f(cy - t)}`,
|
|
169
|
+
`H${f(cx + s)}V${f(cy + t)}H${f(cx + t)}V${f(cy + s)}H${f(cx - t)}V${f(cy + t)}H${f(cx - s)}Z`
|
|
170
|
+
].join("");
|
|
171
|
+
}
|
|
172
|
+
case "star": {
|
|
173
|
+
const points = [];
|
|
174
|
+
for (let i = 0; i < 10; i += 1) {
|
|
175
|
+
const radius = i % 2 === 0 ? s : s * 0.45;
|
|
176
|
+
const angle = Math.PI / 5 * i - Math.PI / 2;
|
|
177
|
+
points.push(
|
|
178
|
+
`${f(cx + radius * Math.cos(angle))},${f(cy + radius * Math.sin(angle))}`
|
|
179
|
+
);
|
|
219
180
|
}
|
|
181
|
+
return `M${points.join("L")}Z`;
|
|
182
|
+
}
|
|
183
|
+
case "circle":
|
|
184
|
+
default: {
|
|
185
|
+
return `M${f(cx - s)},${f(cy)}A${f(s)},${f(s)} 0 1 0 ${f(cx + s)},${f(cy)}A${f(s)},${f(s)} 0 1 0 ${f(cx - s)},${f(cy)}Z`;
|
|
186
|
+
}
|
|
220
187
|
}
|
|
221
188
|
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* 圆弧中心线路径。
|
|
225
|
-
*
|
|
226
|
-
* 与 arcPath 的区别:arcPath 生成的是「环带 + 填充」的闭合区域,
|
|
227
|
-
* 而这里只生成一条中心线圆弧,供 stroke 使用(仪表盘、进度环都用它)。
|
|
228
|
-
*/
|
|
229
|
-
export function arcLinePath(cx, cy, radius, startAngle, endAngle) {
|
|
189
|
+
function arcLinePath(cx, cy, radius, startAngle, endAngle) {
|
|
230
190
|
const sweep = endAngle - startAngle;
|
|
231
|
-
if (Math.abs(sweep) < 1e-6 || radius <= 0)
|
|
232
|
-
|
|
233
|
-
// 整圆无法用单段弧表达,拆成两段
|
|
191
|
+
if (Math.abs(sweep) < 1e-6 || radius <= 0)
|
|
192
|
+
return "";
|
|
234
193
|
if (Math.abs(sweep) >= Math.PI * 2 - 1e-6) {
|
|
235
194
|
const x = cx + radius * Math.cos(startAngle);
|
|
236
195
|
const y = cy + radius * Math.sin(startAngle);
|
|
@@ -243,4 +202,13 @@ export function arcLinePath(cx, cy, radius, startAngle, endAngle) {
|
|
|
243
202
|
const x1 = cx + radius * Math.cos(endAngle);
|
|
244
203
|
const y1 = cy + radius * Math.sin(endAngle);
|
|
245
204
|
return `M${f(x0)},${f(y0)}A${f(radius)},${f(radius)} 0 ${largeArc} ${direction} ${f(x1)},${f(y1)}`;
|
|
246
|
-
}
|
|
205
|
+
}
|
|
206
|
+
export {
|
|
207
|
+
arcLinePath,
|
|
208
|
+
arcPath,
|
|
209
|
+
areaPath,
|
|
210
|
+
linePath,
|
|
211
|
+
roundedRectPath,
|
|
212
|
+
splitSegments,
|
|
213
|
+
symbolPath
|
|
214
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
/** 跨图表联动广播载荷 */
|
|
3
|
+
export interface ChartSyncPayload {
|
|
4
|
+
/** 触发本次联动的图表实例 ID */
|
|
5
|
+
sourceId: string;
|
|
6
|
+
/** 高亮类目索引;null 表示离开 / 清除高亮 */
|
|
7
|
+
index: number | null;
|
|
8
|
+
/** 对应的 x 轴类目标签(用于多图表类目对齐) */
|
|
9
|
+
label?: string | number;
|
|
10
|
+
}
|
|
11
|
+
/** ChartSyncProvider 属性 */
|
|
12
|
+
export interface ChartSyncProviderProps {
|
|
13
|
+
children: React.ReactNode;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 跨图表联动作用域容器。
|
|
17
|
+
*
|
|
18
|
+
* 内部通过恒定引用的订阅 store 分发联动事件,Context 值永不变更,
|
|
19
|
+
* 因此不会引发无关子组件的整树重渲染;同时为多面板或单测提供完全隔离的作用域。
|
|
20
|
+
*/
|
|
21
|
+
export declare function ChartSyncProvider({ children }: ChartSyncProviderProps): React.JSX.Element;
|
|
22
|
+
export declare namespace ChartSyncProvider {
|
|
23
|
+
var displayName: string;
|
|
24
|
+
}
|
|
25
|
+
/** useChartSync 返回值 */
|
|
26
|
+
export interface UseChartSyncResult {
|
|
27
|
+
/** 当前图表唯一源标识 */
|
|
28
|
+
sourceId: string;
|
|
29
|
+
/** 来自同组其他图表的联动载荷;无联动或已清除时为 null */
|
|
30
|
+
synced: ChartSyncPayload | null;
|
|
31
|
+
/** 向同组广播高亮索引与标签 */
|
|
32
|
+
emit: (index: number | null, label?: string | number) => void;
|
|
33
|
+
/** 清除本图表发出的联动高亮 */
|
|
34
|
+
clear: () => void;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 订阅并参与跨图表联动。
|
|
38
|
+
* 未传入 `group` 时为空操作,不建立订阅、不产生额外渲染。
|
|
39
|
+
*/
|
|
40
|
+
export declare function useChartSync(group?: string): UseChartSyncResult;
|
|
41
|
+
//# sourceMappingURL=sync.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync.d.ts","sourceRoot":"","sources":["sync.tsx"],"names":[],"mappings":"AAAA,OAAO,KAQN,MAAM,OAAO,CAAC;AAEf,gBAAgB;AAChB,MAAM,WAAW,gBAAgB;IAC/B,qBAAqB;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,8BAA8B;IAC9B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,6BAA6B;IAC7B,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACzB;AAoED,2BAA2B;AAC3B,MAAM,WAAW,sBAAsB;IACrC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,EAAE,QAAQ,EAAE,EAAE,sBAAsB,qBAUrE;yBAVe,iBAAiB;;;AAcjC,uBAAuB;AACvB,MAAM,WAAW,kBAAkB;IACjC,gBAAgB;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,mCAAmC;IACnC,MAAM,EAAE,gBAAgB,GAAG,IAAI,CAAC;IAChC,mBAAmB;IACnB,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,KAAK,IAAI,CAAC;IAC9D,mBAAmB;IACnB,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,kBAAkB,CAqD/D"}
|