@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.
Files changed (98) hide show
  1. package/esm/charts/area-chart/index.d.ts +2 -2
  2. package/esm/charts/area-chart/index.js +28 -23
  3. package/esm/charts/bar-chart/index.d.ts +1 -1
  4. package/esm/charts/bar-chart/index.js +18 -21
  5. package/esm/charts/candlestick-chart/index.d.ts +2 -2
  6. package/esm/charts/candlestick-chart/index.js +243 -180
  7. package/esm/charts/combo-chart/index.d.ts +1 -1
  8. package/esm/charts/combo-chart/index.js +9 -20
  9. package/esm/charts/funnel-chart/index.d.ts +2 -2
  10. package/esm/charts/funnel-chart/index.js +134 -108
  11. package/esm/charts/gauge-chart/index.d.ts +1 -1
  12. package/esm/charts/gauge-chart/index.js +103 -105
  13. package/esm/charts/graph-chart/index.d.ts +3 -3
  14. package/esm/charts/graph-chart/index.js +155 -114
  15. package/esm/charts/heatmap-chart/index.d.ts +2 -2
  16. package/esm/charts/heatmap-chart/index.js +180 -135
  17. package/esm/charts/line-chart/index.d.ts +2 -2
  18. package/esm/charts/line-chart/index.js +29 -24
  19. package/esm/charts/pie-chart/index.d.ts +2 -2
  20. package/esm/charts/pie-chart/index.d.ts.map +1 -1
  21. package/esm/charts/pie-chart/index.js +199 -139
  22. package/esm/charts/radar-chart/index.d.ts +2 -2
  23. package/esm/charts/radar-chart/index.d.ts.map +1 -1
  24. package/esm/charts/radar-chart/index.js +155 -128
  25. package/esm/charts/ring-progress/index.d.ts +1 -1
  26. package/esm/charts/ring-progress/index.js +82 -82
  27. package/esm/charts/sankey-chart/index.d.ts +3 -3
  28. package/esm/charts/sankey-chart/index.js +172 -141
  29. package/esm/charts/scatter-chart/index.d.ts +3 -3
  30. package/esm/charts/scatter-chart/index.d.ts.map +1 -1
  31. package/esm/charts/scatter-chart/index.js +226 -159
  32. package/esm/charts/sparkline/index.d.ts +1 -1
  33. package/esm/charts/sparkline/index.js +82 -66
  34. package/esm/charts/tree-chart/index.d.ts +3 -3
  35. package/esm/charts/tree-chart/index.js +129 -102
  36. package/esm/charts/treemap-chart/index.d.ts +3 -3
  37. package/esm/charts/treemap-chart/index.js +163 -141
  38. package/esm/charts/waterfall-chart/index.d.ts +2 -2
  39. package/esm/charts/waterfall-chart/index.js +166 -121
  40. package/esm/components/Legend.d.ts +5 -4
  41. package/esm/components/Legend.d.ts.map +1 -1
  42. package/esm/components/Legend.js +80 -48
  43. package/esm/components/Tooltip.d.ts +2 -2
  44. package/esm/components/Tooltip.js +63 -71
  45. package/esm/components/chart/index.d.ts +5 -2
  46. package/esm/components/chart/index.d.ts.map +1 -1
  47. package/esm/components/chart/index.js +81 -102
  48. package/esm/components/chart.less +13 -0
  49. package/esm/core/animation.js +32 -43
  50. package/esm/core/export.d.ts +34 -0
  51. package/esm/core/export.d.ts.map +1 -0
  52. package/esm/core/export.js +390 -0
  53. package/esm/core/force.js +23 -45
  54. package/esm/core/format.js +49 -52
  55. package/esm/core/hierarchy.d.ts +1 -1
  56. package/esm/core/hierarchy.js +61 -92
  57. package/esm/core/layout.d.ts +1 -1
  58. package/esm/core/layout.js +33 -50
  59. package/esm/core/sampling.js +31 -53
  60. package/esm/core/sankey.js +36 -55
  61. package/esm/core/scale.js +73 -86
  62. package/esm/core/shape.js +102 -133
  63. package/esm/core/sync.d.ts +41 -0
  64. package/esm/core/sync.d.ts.map +1 -0
  65. package/esm/core/sync.js +120 -0
  66. package/esm/hooks/useChartExport.d.ts +19 -0
  67. package/esm/hooks/useChartExport.d.ts.map +1 -0
  68. package/esm/hooks/useChartExport.js +35 -0
  69. package/esm/hooks/useChartTheme.d.ts +6 -5
  70. package/esm/hooks/useChartTheme.d.ts.map +1 -1
  71. package/esm/hooks/useChartTheme.js +18 -18
  72. package/esm/hooks/useChartTransition.d.ts +1 -1
  73. package/esm/hooks/useChartTransition.js +59 -73
  74. package/esm/hooks/useElementSize.js +20 -28
  75. package/esm/hooks/usePlotPointer.d.ts +23 -0
  76. package/esm/hooks/usePlotPointer.d.ts.map +1 -0
  77. package/esm/hooks/usePlotPointer.js +94 -0
  78. package/esm/index.d.ts +56 -50
  79. package/esm/index.d.ts.map +1 -1
  80. package/esm/index.js +137 -47
  81. package/esm/internal/CartesianChart.d.ts +1 -1
  82. package/esm/internal/CartesianChart.d.ts.map +1 -1
  83. package/esm/internal/CartesianChart.js +667 -485
  84. package/esm/internal/model.d.ts +2 -2
  85. package/esm/internal/model.js +45 -60
  86. package/esm/scene/canvas.d.ts +1 -1
  87. package/esm/scene/canvas.js +101 -107
  88. package/esm/scene/svg.d.ts +1 -1
  89. package/esm/scene/svg.js +107 -99
  90. package/esm/scene/types.js +9 -15
  91. package/esm/style.css +10 -0
  92. package/esm/theme/index.d.ts +2 -2
  93. package/esm/theme/index.d.ts.map +1 -1
  94. package/esm/theme/index.js +145 -170
  95. package/esm/types.d.ts +7 -3
  96. package/esm/types.d.ts.map +1 -1
  97. package/esm/types.js +1 -1
  98. package/package.json +2 -2
@@ -1,39 +1,18 @@
1
- /**
2
- * 桑基图布局。
3
- *
4
- * 三步:
5
- * 1. 拓扑分层 —— 用最长路径(每层取所有前驱的最大层 + 1),
6
- * 保证一条链路从左到右不回头;
7
- * 2. 层内堆叠 —— 每层总流量归一化到可用高度,节点按流量占比占高;
8
- * 3. 端口分配 —— 同一节点上的多条链路按顺序切分它的一条边,
9
- * 这样带宽之和恰好等于节点高度。
10
- *
11
- * 含环的数据无法拓扑排序,这里用「松弛 n 轮后停止」兜底,
12
- * 而不是抛错 —— 展示型图表遇到脏数据应该画出来,而不是白屏。
13
- */
14
- export function layoutSankey(nodes, links, rect, options) {
15
- const {
16
- nodeWidth = 12,
17
- nodeGap = 10,
18
- colorOf
19
- } = options;
20
- if (!nodes.length || !links.length) return {
21
- nodes: [],
22
- links: [],
23
- layers: 0
24
- };
25
- const names = nodes.map(node => node.name);
1
+ 'use client';
2
+ // src/core/sankey.ts
3
+ function layoutSankey(nodes, links, rect, options) {
4
+ const { nodeWidth = 12, nodeGap = 10, colorOf } = options;
5
+ if (!nodes.length || !links.length)
6
+ return { nodes: [], links: [], layers: 0 };
7
+ const names = nodes.map((node) => node.name);
26
8
  const exists = new Set(names);
27
- const validLinks = links.filter(link => exists.has(link.source) && exists.has(link.target) && link.source !== link.target && Number.isFinite(link.value) && link.value > 0);
28
- if (!validLinks.length) return {
29
- nodes: [],
30
- links: [],
31
- layers: 0
32
- };
33
-
34
- // ---- 1. 拓扑分层 ----
35
- const layer = new Map();
36
- names.forEach(name => layer.set(name, 0));
9
+ const validLinks = links.filter(
10
+ (link) => exists.has(link.source) && exists.has(link.target) && link.source !== link.target && Number.isFinite(link.value) && link.value > 0
11
+ );
12
+ if (!validLinks.length)
13
+ return { nodes: [], links: [], layers: 0 };
14
+ const layer = /* @__PURE__ */ new Map();
15
+ names.forEach((name) => layer.set(name, 0));
37
16
  for (let round = 0; round < names.length; round += 1) {
38
17
  let changed = false;
39
18
  for (const link of validLinks) {
@@ -43,24 +22,24 @@ export function layoutSankey(nodes, links, rect, options) {
43
22
  changed = true;
44
23
  }
45
24
  }
46
- if (!changed) break;
25
+ if (!changed)
26
+ break;
47
27
  }
48
28
  const maxLayer = Math.max(...Array.from(layer.values()));
49
29
  const layers = maxLayer + 1;
50
-
51
- // ---- 2. 节点流量与层内堆叠 ----
52
- const inflow = new Map();
53
- const outflow = new Map();
30
+ const inflow = /* @__PURE__ */ new Map();
31
+ const outflow = /* @__PURE__ */ new Map();
54
32
  for (const link of validLinks) {
55
33
  outflow.set(link.source, (outflow.get(link.source) ?? 0) + link.value);
56
34
  inflow.set(link.target, (inflow.get(link.target) ?? 0) + link.value);
57
35
  }
58
- const valueOf = name => Math.max(inflow.get(name) ?? 0, outflow.get(name) ?? 0);
36
+ const valueOf = (name) => Math.max(inflow.get(name) ?? 0, outflow.get(name) ?? 0);
59
37
  const columnGap = (rect.width - nodeWidth * layers) / Math.max(1, layers - 1);
60
- const boxes = new Map();
38
+ const boxes = /* @__PURE__ */ new Map();
61
39
  for (let current = 0; current < layers; current += 1) {
62
- const column = names.filter(name => (layer.get(name) ?? 0) === current);
63
- if (!column.length) continue;
40
+ const column = names.filter((name) => (layer.get(name) ?? 0) === current);
41
+ if (!column.length)
42
+ continue;
64
43
  const totalFlow = column.reduce((sum, name) => sum + valueOf(name), 0) || 1;
65
44
  const available = rect.height - nodeGap * (column.length - 1);
66
45
  const scale = available / totalFlow;
@@ -68,7 +47,7 @@ export function layoutSankey(nodes, links, rect, options) {
68
47
  column.forEach((name, index) => {
69
48
  const value = valueOf(name);
70
49
  const height = Math.max(2, value * scale);
71
- const node = nodes.find(item => item.name === name);
50
+ const node = nodes.find((item) => item.name === name);
72
51
  boxes.set(name, {
73
52
  name,
74
53
  layer: current,
@@ -77,17 +56,14 @@ export function layoutSankey(nodes, links, rect, options) {
77
56
  width: nodeWidth,
78
57
  height,
79
58
  value,
80
- color: node?.color ?? colorOf(current, index)
59
+ color: (node == null ? void 0 : node.color) ?? colorOf(current, index)
81
60
  });
82
61
  cursor += height + nodeGap;
83
62
  });
84
63
  }
85
-
86
- // ---- 3. 端口分配 ----
87
- // 源节点按目标层序排列、目标节点按源层序排列,避免带状交叉成一团
88
- const sourceCursor = new Map();
89
- const targetCursor = new Map();
90
- names.forEach(name => {
64
+ const sourceCursor = /* @__PURE__ */ new Map();
65
+ const targetCursor = /* @__PURE__ */ new Map();
66
+ names.forEach((name) => {
91
67
  const box = boxes.get(name);
92
68
  if (box) {
93
69
  sourceCursor.set(name, box.y);
@@ -96,14 +72,16 @@ export function layoutSankey(nodes, links, rect, options) {
96
72
  });
97
73
  const sortedLinks = [...validLinks].sort((a, b) => {
98
74
  const layerDiff = (layer.get(a.target) ?? 0) - (layer.get(b.target) ?? 0);
99
- if (layerDiff !== 0) return layerDiff;
75
+ if (layerDiff !== 0)
76
+ return layerDiff;
100
77
  return (layer.get(a.source) ?? 0) - (layer.get(b.source) ?? 0);
101
78
  });
102
79
  const paths = [];
103
80
  for (const link of sortedLinks) {
104
81
  const source = boxes.get(link.source);
105
82
  const target = boxes.get(link.target);
106
- if (!source || !target) continue;
83
+ if (!source || !target)
84
+ continue;
107
85
  const sourceTotal = Math.max(1, valueOf(source.name));
108
86
  const targetTotal = Math.max(1, valueOf(target.name));
109
87
  const sourceThickness = link.value / sourceTotal * source.height;
@@ -132,4 +110,7 @@ export function layoutSankey(nodes, links, rect, options) {
132
110
  links: paths,
133
111
  layers
134
112
  };
135
- }
113
+ }
114
+ export {
115
+ layoutSankey
116
+ };
package/esm/core/scale.js CHANGED
@@ -1,78 +1,66 @@
1
- /**
2
- * 刻度与坐标映射内核。
3
- *
4
- * 设计原则:
5
- * 1. 纯函数、零 DOM 依赖 —— 可在 SSR / 测试环境中直接跑;
6
- * 2. 只描述「数据域 ↔ 像素域」的映射关系,不关心渲染方式,
7
- * 因此同一套 scale 可同时供 SVG 与 Canvas 渲染器消费;
8
- * 3. 刻度算法不引入 d3-scale 依赖(包体积 + 版本风险),
9
- * 但采用与之等价的「1 / 2 / 2.5 / 5 / 10 × 10^n」取整策略,
10
- * 保证出图结果符合工程直觉。
11
- */
12
-
13
- /** 数据域 */
14
-
15
- /**
16
- * 把任意步长收敛到「人类友好」的值:
17
- * 1.0 / 2.0 / 2.5 / 5.0 × 10^n。
18
- */
19
- export function niceStep(rawStep) {
20
- if (!Number.isFinite(rawStep) || rawStep <= 0) return 1;
1
+ 'use client';
2
+ // src/core/scale.ts
3
+ function niceStep(rawStep) {
4
+ if (!Number.isFinite(rawStep) || rawStep <= 0)
5
+ return 1;
21
6
  const exponent = Math.floor(Math.log10(rawStep));
22
7
  const base = 10 ** exponent;
23
8
  const fraction = rawStep / base;
24
9
  let niceFraction;
25
- if (fraction <= 1) niceFraction = 1;else if (fraction <= 2) niceFraction = 2;else if (fraction <= 2.5) niceFraction = 2.5;else if (fraction <= 5) niceFraction = 5;else niceFraction = 10;
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;
26
20
  return niceFraction * base;
27
21
  }
28
-
29
- /** 浮点累加会产生 0.30000000000000004,统一按步长精度截断 */
30
22
  function roundToStep(value, step) {
31
23
  const decimals = Math.max(0, -Math.floor(Math.log10(step)) + 1);
32
24
  return Number(value.toFixed(Math.min(12, decimals)));
33
25
  }
34
-
35
- /**
36
- * 在 [min, max] 内生成刻度。
37
- *
38
- * @param count 期望的刻度数量(实际数量会因取整而围绕它浮动)
39
- */
40
- export function linearTicks(min, max, count = 5) {
41
- if (!Number.isFinite(min) || !Number.isFinite(max)) return [];
42
- 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];
43
31
  const step = niceStep((max - min) / Math.max(1, count));
44
32
  const start = Math.ceil(min / step) * step;
45
33
  const out = [];
46
- // 容忍浮点误差,避免边界刻度被漏掉
47
34
  const epsilon = step * 1e-9;
48
35
  for (let value = start; value <= max + epsilon; value += step) {
49
36
  out.push(roundToStep(value, step));
50
37
  }
51
38
  return out;
52
39
  }
53
-
54
- /** 把 domain 扩展到刻度边界 */
55
- export function niceExtent(min, max, count = 5) {
56
- 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];
57
43
  if (min === max) {
58
- // 退化情况:给一个对称的呼吸空间,避免 0 高度图
59
44
  const pad = Math.abs(min) || 1;
60
45
  return [min - pad * 0.5, max + pad * 0.5];
61
46
  }
62
47
  const step = niceStep((max - min) / Math.max(1, count));
63
- return [roundToStep(Math.floor(min / step) * step, step), roundToStep(Math.ceil(max / step) * step, step)];
48
+ return [
49
+ roundToStep(Math.floor(min / step) * step, step),
50
+ roundToStep(Math.ceil(max / step) * step, step)
51
+ ];
64
52
  }
65
- export function createLinearScale(options) {
53
+ function createLinearScale(options) {
66
54
  let [min, max] = options.domain;
67
- // 防御:反向域(如 0 到 -100)同样要能正确映射
68
- if (min === max) max = min + 1;
55
+ if (min === max)
56
+ max = min + 1;
69
57
  const [r0, r1] = options.range;
70
58
  const build = (domain, range, tickStep) => {
71
59
  const [d0, d1] = domain;
72
60
  const [p0, p1] = range;
73
61
  const span = d1 - d0 || 1;
74
- const scaleFn = value => p0 + (value - d0) / span * (p1 - p0);
75
- 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;
76
64
  const ticksFn = (count = 5) => {
77
65
  if (tickStep) {
78
66
  const out = [];
@@ -85,7 +73,7 @@ export function createLinearScale(options) {
85
73
  return linearTicks(d0, d1, count);
86
74
  };
87
75
  const self = {
88
- type: 'linear',
76
+ type: "linear",
89
77
  domain,
90
78
  range,
91
79
  scale: scaleFn,
@@ -97,10 +85,10 @@ export function createLinearScale(options) {
97
85
  },
98
86
  copy(overrides) {
99
87
  return createLinearScale({
100
- domain: overrides?.domain ?? domain,
101
- range: overrides?.range ?? range,
102
- nice: overrides?.nice,
103
- tickStep: overrides?.tickStep ?? tickStep
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
104
92
  });
105
93
  }
106
94
  };
@@ -108,19 +96,11 @@ export function createLinearScale(options) {
108
96
  };
109
97
  const base = build([min, max], [r0, r1], options.tickStep);
110
98
  if (options.nice) {
111
- return base.nice(typeof options.nice === 'number' ? options.nice : 5);
99
+ return base.nice(typeof options.nice === "number" ? options.nice : 5);
112
100
  }
113
101
  return base;
114
102
  }
115
-
116
- /**
117
- * 分类坐标(band):每个分类占一个等宽区间,柱子/分组柱靠它定位。
118
- *
119
- * 公式与 d3.scaleBand 一致(align 固定 0.5,即整体居中):
120
- * step = range / (n - paddingInner + paddingOuter * 2)
121
- * bandWidth = step * (1 - paddingInner)
122
- */
123
- export function createBandScale(options) {
103
+ function createBandScale(options) {
124
104
  const domain = options.domain;
125
105
  const [r0, r1] = options.range;
126
106
  const paddingInner = clamp(options.paddingInner ?? 0.2);
@@ -130,32 +110,29 @@ export function createBandScale(options) {
130
110
  const step = n > 0 ? span / Math.max(1, n - paddingInner + paddingOuter * 2) : 0;
131
111
  const bandWidth = Math.max(0, step * (1 - paddingInner));
132
112
  const start = r0 + (span - step * (n - paddingInner)) / 2;
133
- const index = new Map();
113
+ const index = /* @__PURE__ */ new Map();
134
114
  domain.forEach((key, i) => {
135
- if (!index.has(key)) index.set(key, i);
115
+ if (!index.has(key))
116
+ index.set(key, i);
136
117
  });
137
118
  return {
138
- type: 'band',
119
+ type: "band",
139
120
  domain,
140
121
  range: options.range,
141
122
  bandWidth,
142
123
  step,
143
124
  scale(value) {
144
125
  const i = index.get(value);
145
- return i === undefined ? undefined : start + step * i;
126
+ return i === void 0 ? void 0 : start + step * i;
146
127
  },
147
128
  center(value) {
148
129
  const i = index.get(value);
149
- return i === undefined ? undefined : start + step * i + bandWidth / 2;
130
+ return i === void 0 ? void 0 : start + step * i + bandWidth / 2;
150
131
  },
151
132
  ticks: () => [...domain]
152
133
  };
153
134
  }
154
-
155
- /**
156
- * 点坐标(point):分类落在刻度线上而非区间内,折线图/散点图用它。
157
- */
158
- export function createPointScale(options) {
135
+ function createPointScale(options) {
159
136
  const domain = options.domain;
160
137
  const [r0, r1] = options.range;
161
138
  const padding = clamp(options.paddingOuter ?? 0.5);
@@ -164,17 +141,19 @@ export function createPointScale(options) {
164
141
  const denom = Math.max(1, n - 1 + padding * 2);
165
142
  const step = n > 1 ? span / denom : 0;
166
143
  const start = n > 1 ? r0 + step * padding : (r0 + r1) / 2;
167
- const index = new Map();
144
+ const index = /* @__PURE__ */ new Map();
168
145
  domain.forEach((key, i) => {
169
- if (!index.has(key)) index.set(key, i);
146
+ if (!index.has(key))
147
+ index.set(key, i);
170
148
  });
171
149
  return {
172
- type: 'point',
150
+ type: "point",
173
151
  domain,
174
152
  range: options.range,
175
153
  scale(value) {
176
154
  const i = index.get(value);
177
- if (i === undefined) return undefined;
155
+ if (i === void 0)
156
+ return void 0;
178
157
  return n > 1 ? start + step * i : (r0 + r1) / 2;
179
158
  },
180
159
  ticks: () => [...domain]
@@ -183,24 +162,32 @@ export function createPointScale(options) {
183
162
  function clamp(value, min = 0, max = 1) {
184
163
  return Math.min(max, Math.max(min, value));
185
164
  }
186
-
187
- /** 根据一组数值求 [min, max],忽略 null / NaN */
188
- export function extentOf(values) {
165
+ function extentOf(values) {
189
166
  let min = Infinity;
190
167
  let max = -Infinity;
191
168
  for (const value of values) {
192
- if (value === null || value === undefined || Number.isNaN(value)) continue;
193
- if (value < min) min = value;
194
- if (value > max) max = 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;
195
175
  }
196
- if (min === Infinity) return [0, 1];
176
+ if (min === Infinity)
177
+ return [0, 1];
197
178
  return [min, max];
198
179
  }
199
-
200
- /**
201
- * 数值轴默认要包含 0 基线(柱状图必须如此,否则柱子长度失真)。
202
- */
203
- export function extentWithZero(values) {
180
+ function extentWithZero(values) {
204
181
  const [min, max] = extentOf(values);
205
182
  return [Math.min(0, min), Math.max(0, max)];
206
- }
183
+ }
184
+ export {
185
+ createBandScale,
186
+ createLinearScale,
187
+ createPointScale,
188
+ extentOf,
189
+ extentWithZero,
190
+ linearTicks,
191
+ niceExtent,
192
+ niceStep
193
+ };