@aura-react-comp/charts 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +90 -2
  3. package/esm/charts/area-chart/index.d.ts +19 -0
  4. package/esm/charts/area-chart/index.d.ts.map +1 -0
  5. package/esm/charts/area-chart/index.js +30 -0
  6. package/esm/charts/bar-chart/index.d.ts +16 -0
  7. package/esm/charts/bar-chart/index.d.ts.map +1 -0
  8. package/esm/charts/bar-chart/index.js +27 -0
  9. package/esm/charts/candlestick-chart/index.d.ts +47 -0
  10. package/esm/charts/candlestick-chart/index.d.ts.map +1 -0
  11. package/esm/charts/candlestick-chart/index.js +346 -0
  12. package/esm/charts/combo-chart/index.d.ts +17 -0
  13. package/esm/charts/combo-chart/index.d.ts.map +1 -0
  14. package/esm/charts/combo-chart/index.js +25 -0
  15. package/esm/charts/funnel-chart/index.d.ts +35 -0
  16. package/esm/charts/funnel-chart/index.d.ts.map +1 -0
  17. package/esm/charts/funnel-chart/index.js +199 -0
  18. package/esm/charts/gauge-chart/index.d.ts +41 -0
  19. package/esm/charts/gauge-chart/index.d.ts.map +1 -0
  20. package/esm/charts/gauge-chart/index.js +216 -0
  21. package/esm/charts/graph-chart/index.d.ts +35 -0
  22. package/esm/charts/graph-chart/index.d.ts.map +1 -0
  23. package/esm/charts/graph-chart/index.js +223 -0
  24. package/esm/charts/heatmap-chart/index.d.ts +47 -0
  25. package/esm/charts/heatmap-chart/index.d.ts.map +1 -0
  26. package/esm/charts/heatmap-chart/index.js +280 -0
  27. package/esm/charts/line-chart/index.d.ts +23 -0
  28. package/esm/charts/line-chart/index.d.ts.map +1 -0
  29. package/esm/charts/line-chart/index.js +32 -0
  30. package/esm/charts/pie-chart/index.d.ts +38 -0
  31. package/esm/charts/pie-chart/index.d.ts.map +1 -0
  32. package/esm/charts/pie-chart/index.js +271 -0
  33. package/esm/charts/radar-chart/index.d.ts +44 -0
  34. package/esm/charts/radar-chart/index.d.ts.map +1 -0
  35. package/esm/charts/radar-chart/index.js +262 -0
  36. package/esm/charts/ring-progress/index.d.ts +38 -0
  37. package/esm/charts/ring-progress/index.d.ts.map +1 -0
  38. package/esm/charts/ring-progress/index.js +136 -0
  39. package/esm/charts/sankey-chart/index.d.ts +34 -0
  40. package/esm/charts/sankey-chart/index.d.ts.map +1 -0
  41. package/esm/charts/sankey-chart/index.js +247 -0
  42. package/esm/charts/scatter-chart/index.d.ts +34 -0
  43. package/esm/charts/scatter-chart/index.d.ts.map +1 -0
  44. package/esm/charts/scatter-chart/index.js +304 -0
  45. package/esm/charts/sparkline/index.d.ts +31 -0
  46. package/esm/charts/sparkline/index.d.ts.map +1 -0
  47. package/esm/charts/sparkline/index.js +151 -0
  48. package/esm/charts/tree-chart/index.d.ts +34 -0
  49. package/esm/charts/tree-chart/index.d.ts.map +1 -0
  50. package/esm/charts/tree-chart/index.js +199 -0
  51. package/esm/charts/treemap-chart/index.d.ts +35 -0
  52. package/esm/charts/treemap-chart/index.d.ts.map +1 -0
  53. package/esm/charts/treemap-chart/index.js +238 -0
  54. package/esm/charts/waterfall-chart/index.d.ts +41 -0
  55. package/esm/charts/waterfall-chart/index.d.ts.map +1 -0
  56. package/esm/charts/waterfall-chart/index.js +280 -0
  57. package/esm/components/Legend.d.ts +24 -0
  58. package/esm/components/Legend.d.ts.map +1 -0
  59. package/esm/components/Legend.js +49 -0
  60. package/esm/components/Tooltip.d.ts +24 -0
  61. package/esm/components/Tooltip.d.ts.map +1 -0
  62. package/esm/components/Tooltip.js +84 -0
  63. package/esm/components/chart/index.d.ts +71 -0
  64. package/esm/components/chart/index.d.ts.map +1 -0
  65. package/esm/components/chart/index.js +141 -0
  66. package/esm/components/chart.less +223 -0
  67. package/esm/core/animation.d.ts +49 -0
  68. package/esm/core/animation.d.ts.map +1 -0
  69. package/esm/core/animation.js +92 -0
  70. package/esm/core/force.d.ts +43 -0
  71. package/esm/core/force.d.ts.map +1 -0
  72. package/esm/core/force.js +133 -0
  73. package/esm/core/format.d.ts +34 -0
  74. package/esm/core/format.d.ts.map +1 -0
  75. package/esm/core/format.js +80 -0
  76. package/esm/core/hierarchy.d.ts +77 -0
  77. package/esm/core/hierarchy.d.ts.map +1 -0
  78. package/esm/core/hierarchy.js +232 -0
  79. package/esm/core/layout.d.ts +67 -0
  80. package/esm/core/layout.d.ts.map +1 -0
  81. package/esm/core/layout.js +120 -0
  82. package/esm/core/sampling.d.ts +32 -0
  83. package/esm/core/sampling.d.ts.map +1 -0
  84. package/esm/core/sampling.js +123 -0
  85. package/esm/core/sankey.d.ts +67 -0
  86. package/esm/core/sankey.d.ts.map +1 -0
  87. package/esm/core/sankey.js +135 -0
  88. package/esm/core/scale.d.ts +98 -0
  89. package/esm/core/scale.d.ts.map +1 -0
  90. package/esm/core/scale.js +206 -0
  91. package/esm/core/shape.d.ts +60 -0
  92. package/esm/core/shape.d.ts.map +1 -0
  93. package/esm/core/shape.js +245 -0
  94. package/esm/global.d.ts +6 -0
  95. package/esm/hooks/useChartTheme.d.ts +10 -0
  96. package/esm/hooks/useChartTheme.d.ts.map +1 -0
  97. package/esm/hooks/useChartTheme.js +33 -0
  98. package/esm/hooks/useChartTransition.d.ts +20 -0
  99. package/esm/hooks/useChartTransition.d.ts.map +1 -0
  100. package/esm/hooks/useChartTransition.js +119 -0
  101. package/esm/hooks/useElementSize.d.ts +17 -0
  102. package/esm/hooks/useElementSize.d.ts.map +1 -0
  103. package/esm/hooks/useElementSize.js +46 -0
  104. package/esm/index.d.ts +62 -0
  105. package/esm/index.d.ts.map +1 -0
  106. package/esm/index.js +47 -0
  107. package/esm/internal/CartesianChart.d.ts +16 -0
  108. package/esm/internal/CartesianChart.d.ts.map +1 -0
  109. package/esm/internal/CartesianChart.js +810 -0
  110. package/esm/internal/model.d.ts +67 -0
  111. package/esm/internal/model.d.ts.map +1 -0
  112. package/esm/internal/model.js +145 -0
  113. package/esm/scene/canvas.d.ts +18 -0
  114. package/esm/scene/canvas.d.ts.map +1 -0
  115. package/esm/scene/canvas.js +137 -0
  116. package/esm/scene/svg.d.ts +12 -0
  117. package/esm/scene/svg.d.ts.map +1 -0
  118. package/esm/scene/svg.js +115 -0
  119. package/esm/scene/types.d.ts +89 -0
  120. package/esm/scene/types.d.ts.map +1 -0
  121. package/esm/scene/types.js +20 -0
  122. package/esm/style.css +196 -0
  123. package/esm/theme/index.d.ts +127 -0
  124. package/esm/theme/index.d.ts.map +1 -0
  125. package/esm/theme/index.js +297 -0
  126. package/esm/types.d.ts +168 -0
  127. package/esm/types.d.ts.map +1 -0
  128. package/esm/types.js +1 -0
  129. package/package.json +64 -3
@@ -0,0 +1,223 @@
1
+ .aura-chart {
2
+ position: relative;
3
+ display: flex;
4
+ flex-direction: column;
5
+ width: 100%;
6
+ font-family: var(--aura-font-family, inherit);
7
+ color: var(--aura-text, #111827);
8
+
9
+ &__legend {
10
+ display: flex;
11
+ flex-wrap: wrap;
12
+ gap: 4px 16px;
13
+ padding: 0 4px 8px;
14
+ font-size: 12px;
15
+ line-height: 20px;
16
+ flex: 0 0 auto;
17
+ user-select: none;
18
+
19
+ &--bottom {
20
+ padding: 8px 4px 0;
21
+ }
22
+
23
+ &--center {
24
+ justify-content: center;
25
+ }
26
+
27
+ &--right {
28
+ justify-content: flex-end;
29
+ }
30
+ }
31
+
32
+ &__legend-item {
33
+ display: inline-flex;
34
+ align-items: center;
35
+ gap: 6px;
36
+ padding: 0;
37
+ border: 0;
38
+ background: none;
39
+ font: inherit;
40
+ color: inherit;
41
+ cursor: pointer;
42
+ transition: opacity 0.15s ease;
43
+
44
+ &:hover {
45
+ opacity: 0.75;
46
+ }
47
+
48
+ &:focus-visible {
49
+ outline: 2px solid var(--aura-primary-300, #d8b4fe);
50
+ outline-offset: 2px;
51
+ border-radius: 4px;
52
+ }
53
+
54
+ &--inactive {
55
+ opacity: 0.45;
56
+ }
57
+ }
58
+
59
+ &__legend-dot {
60
+ width: 8px;
61
+ height: 8px;
62
+ border-radius: 2px;
63
+ flex: 0 0 auto;
64
+ }
65
+
66
+ &__plot {
67
+ position: relative;
68
+ flex: 1 1 auto;
69
+ min-height: 0;
70
+ width: 100%;
71
+ overflow: hidden;
72
+ }
73
+
74
+ &__zoom {
75
+ flex: 0 0 auto;
76
+ padding: 6px 12px 2px;
77
+ box-sizing: content-box;
78
+ }
79
+
80
+ &__zoom-track {
81
+ position: relative;
82
+ height: 100%;
83
+ border-radius: 4px;
84
+ background: var(--aura-bg-tertiary, #f3f4f6);
85
+ cursor: pointer;
86
+ }
87
+
88
+ &__zoom-window {
89
+ position: absolute;
90
+ top: 0;
91
+ bottom: 0;
92
+ border-radius: 4px;
93
+ background: rgba(124, 58, 237, 0.12);
94
+ border: 1px solid var(--aura-primary-500, #a855f7);
95
+ cursor: grab;
96
+
97
+ &:active {
98
+ cursor: grabbing;
99
+ }
100
+ }
101
+
102
+ &__zoom-handle {
103
+ position: absolute;
104
+ top: 50%;
105
+ width: 8px;
106
+ height: 16px;
107
+ margin-top: -8px;
108
+ border-radius: 3px;
109
+ background: var(--aura-primary-700, #7c3aed);
110
+ cursor: ew-resize;
111
+
112
+ &--start {
113
+ left: -4px;
114
+ }
115
+
116
+ &--end {
117
+ right: -4px;
118
+ }
119
+ }
120
+
121
+ &__canvas {
122
+ display: block;
123
+ width: 100%;
124
+ height: 100%;
125
+ }
126
+
127
+ &__tooltip {
128
+ position: absolute;
129
+ z-index: 10;
130
+ min-width: 120px;
131
+ max-width: 280px;
132
+ padding: 8px 10px;
133
+ border-radius: 10px;
134
+ border: 1px solid transparent;
135
+ font-size: 12px;
136
+ line-height: 1.6;
137
+ pointer-events: none;
138
+ transition:
139
+ left 0.08s linear,
140
+ top 0.08s linear;
141
+ }
142
+
143
+ &__tooltip-title {
144
+ font-weight: 500;
145
+ margin-bottom: 4px;
146
+ opacity: 0.75;
147
+ }
148
+
149
+ &__tooltip-item {
150
+ display: flex;
151
+ align-items: center;
152
+ gap: 6px;
153
+ justify-content: space-between;
154
+ }
155
+
156
+ &__tooltip-label {
157
+ display: inline-flex;
158
+ align-items: center;
159
+ gap: 6px;
160
+ min-width: 0;
161
+ opacity: 0.85;
162
+ }
163
+
164
+ &__tooltip-value {
165
+ font-weight: 600;
166
+ font-variant-numeric: tabular-nums;
167
+ white-space: nowrap;
168
+ }
169
+
170
+ &__empty {
171
+ display: flex;
172
+ align-items: center;
173
+ justify-content: center;
174
+ width: 100%;
175
+ height: 100%;
176
+ font-size: 13px;
177
+ color: var(--aura-text-tertiary, #9ca3af);
178
+ }
179
+
180
+ &__title {
181
+ padding: 0 4px 8px;
182
+ flex: 0 0 auto;
183
+ }
184
+
185
+ &__title-main {
186
+ font-size: 14px;
187
+ font-weight: 600;
188
+ line-height: 22px;
189
+ color: var(--aura-text, #111827);
190
+ }
191
+
192
+ &__title-sub {
193
+ font-size: 12px;
194
+ line-height: 18px;
195
+ color: var(--aura-text-secondary, #6b7280);
196
+ }
197
+
198
+ /* 无障碍数据表:视觉上隐藏,屏幕阅读器可读 */
199
+ /**
200
+ * 视觉隐藏容器。
201
+ *
202
+ * 注意必须由 div 承担 position/overflow —— table 的 display 会忽略
203
+ * height 与 overflow,把 1px 直接加在 table 上挡不住它撑开文档滚动高度
204
+ * (8000 行数据曾把页面撑到 21 万像素,滚到底全是空白)。
205
+ */
206
+ &__a11y {
207
+ position: absolute;
208
+ width: 1px;
209
+ height: 1px;
210
+ padding: 0;
211
+ margin: -1px;
212
+ overflow: hidden;
213
+ clip: rect(0, 0, 0, 0);
214
+ clip-path: inset(50%);
215
+ white-space: nowrap;
216
+ border: 0;
217
+ }
218
+
219
+ &__a11y-table {
220
+ border-collapse: collapse;
221
+ width: 100%;
222
+ }
223
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * 动画基础设施。
3
+ *
4
+ * 设计目标(针对「生硬」的根因):
5
+ * 1. 缓动不能只有一种 —— 默认改用 easeOutQuint,尾部更慢,收得更柔和;
6
+ * 2. 图元不能同时到齐 —— 提供 stagger(错峰),让动画「扫过」而非「弹起」;
7
+ * 3. 数据更新不能重播入场 —— 交给 useChartTransition 做值插值。
8
+ */
9
+ export type EasingName = 'linear' | 'easeOutCubic' | 'easeOutQuint' | 'easeInOutCubic' | 'easeOutBack';
10
+ export declare function getEasing(name: EasingName): (t: number) => number;
11
+ export interface ChartAnimationOptions {
12
+ /** 时长(毫秒) */
13
+ duration?: number;
14
+ /** 缓动曲线 */
15
+ easing?: EasingName;
16
+ /**
17
+ * 错峰强度 0~1:最后一个图元比第一个晚多少比例的总时长开始。
18
+ * 0 表示全部同步;0.35 表示最后一个晚 35% 开始。
19
+ */
20
+ stagger?: number;
21
+ /** 整体延迟(毫秒) */
22
+ delay?: number;
23
+ }
24
+ export interface ResolvedAnimation {
25
+ enabled: boolean;
26
+ duration: number;
27
+ easing: EasingName;
28
+ stagger: number;
29
+ delay: number;
30
+ ease: (t: number) => number;
31
+ }
32
+ export declare const DEFAULT_ANIMATION: Required<ChartAnimationOptions>;
33
+ /** 统一解析 boolean | 配置对象 */
34
+ export declare function resolveAnimation(input?: boolean | ChartAnimationOptions, defaultEnabled?: boolean): ResolvedAnimation;
35
+ export declare function clamp01(value: number): number;
36
+ /**
37
+ * 计算第 index 个元素的局部进度。
38
+ *
39
+ * 把全局进度 t 切成 count 段:第 i 个元素从 `stagger * i / (count-1)` 开始,
40
+ * 在剩余 `1 - stagger` 的窗口内走完自己的 0→1。
41
+ *
42
+ * 这样整组图元不是「同时弹起」,而是像画笔从左到右扫过去。
43
+ */
44
+ export declare function staggerProgress(t: number, index: number, count: number, stagger: number): number;
45
+ /** 数值插值 */
46
+ export declare function lerp(from: number, to: number, t: number): number;
47
+ /** 矩阵的轻量指纹,用于判断「数据是否真的变了」 */
48
+ export declare function hashMatrix(matrix: readonly (readonly (number | null)[])[]): number;
49
+ //# sourceMappingURL=animation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"animation.d.ts","sourceRoot":"","sources":["animation.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,MAAM,UAAU,GACpB,QAAQ,GAAG,cAAc,GAAG,cAAc,GAAG,gBAAgB,GAAG,aAAa,CAAC;AAgBhF,wBAAgB,SAAS,CAAC,IAAI,EAAE,UAAU,GAAG,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAEjE;AAED,MAAM,WAAW,qBAAqB;IACpC,aAAa;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW;IACX,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,eAAe;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,UAAU,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;CAC7B;AAED,eAAO,MAAM,iBAAiB,EAAE,QAAQ,CAAC,qBAAqB,CAK7D,CAAC;AAEF,0BAA0B;AAC1B,wBAAgB,gBAAgB,CAC9B,KAAK,CAAC,EAAE,OAAO,GAAG,qBAAqB,EACvC,cAAc,UAAO,GACpB,iBAAiB,CAuBnB;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,CAAC,EAAE,MAAM,EACT,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,GACd,MAAM,CAMR;AAED,WAAW;AACX,wBAAgB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED,6BAA6B;AAC7B,wBAAgB,UAAU,CACxB,MAAM,EAAE,SAAS,CAAC,SAAS,CAAC,MAAM,GAAG,IAAI,CAAC,EAAE,CAAC,EAAE,GAC9C,MAAM,CAUR"}
@@ -0,0 +1,92 @@
1
+ /**
2
+ * 动画基础设施。
3
+ *
4
+ * 设计目标(针对「生硬」的根因):
5
+ * 1. 缓动不能只有一种 —— 默认改用 easeOutQuint,尾部更慢,收得更柔和;
6
+ * 2. 图元不能同时到齐 —— 提供 stagger(错峰),让动画「扫过」而非「弹起」;
7
+ * 3. 数据更新不能重播入场 —— 交给 useChartTransition 做值插值。
8
+ */
9
+
10
+ const easingFns = {
11
+ linear: t => t,
12
+ easeOutCubic: t => 1 - (1 - t) ** 3,
13
+ // 比 cubic 尾部更松:数字类、柱状生长用它的减速感最自然
14
+ easeOutQuint: t => 1 - (1 - t) ** 5,
15
+ easeInOutCubic: t => t < 0.5 ? 4 * t ** 3 : 1 - (-2 * t + 2) ** 3 / 2,
16
+ // 轻微回弹(约 10% 过冲),仅用于环形/雷达这类「展开」动作
17
+ easeOutBack: t => {
18
+ const c1 = 1.70158;
19
+ const c3 = c1 + 1;
20
+ return 1 + c3 * (t - 1) ** 3 + c1 * (t - 1) ** 2;
21
+ }
22
+ };
23
+ export function getEasing(name) {
24
+ return easingFns[name] ?? easingFns.easeOutQuint;
25
+ }
26
+ export const DEFAULT_ANIMATION = {
27
+ duration: 620,
28
+ easing: 'easeOutQuint',
29
+ stagger: 0.35,
30
+ delay: 0
31
+ };
32
+
33
+ /** 统一解析 boolean | 配置对象 */
34
+ export function resolveAnimation(input, defaultEnabled = true) {
35
+ if (input === false) {
36
+ return {
37
+ enabled: false,
38
+ duration: 0,
39
+ easing: DEFAULT_ANIMATION.easing,
40
+ stagger: 0,
41
+ delay: 0,
42
+ ease: t => t
43
+ };
44
+ }
45
+ const options = typeof input === 'object' ? input : {};
46
+ const easing = options.easing ?? DEFAULT_ANIMATION.easing;
47
+ return {
48
+ enabled: defaultEnabled,
49
+ duration: Math.max(0, options.duration ?? DEFAULT_ANIMATION.duration),
50
+ easing,
51
+ stagger: clamp01(options.stagger ?? DEFAULT_ANIMATION.stagger),
52
+ delay: Math.max(0, options.delay ?? DEFAULT_ANIMATION.delay),
53
+ ease: getEasing(easing)
54
+ };
55
+ }
56
+ export function clamp01(value) {
57
+ return value < 0 ? 0 : value > 1 ? 1 : value;
58
+ }
59
+
60
+ /**
61
+ * 计算第 index 个元素的局部进度。
62
+ *
63
+ * 把全局进度 t 切成 count 段:第 i 个元素从 `stagger * i / (count-1)` 开始,
64
+ * 在剩余 `1 - stagger` 的窗口内走完自己的 0→1。
65
+ *
66
+ * 这样整组图元不是「同时弹起」,而是像画笔从左到右扫过去。
67
+ */
68
+ export function staggerProgress(t, index, count, stagger) {
69
+ if (count <= 1 || stagger <= 0) return clamp01(t);
70
+ const delay = stagger * index / (count - 1);
71
+ const span = 1 - stagger;
72
+ if (span <= 0) return t >= delay ? 1 : 0;
73
+ return clamp01((t - delay) / span);
74
+ }
75
+
76
+ /** 数值插值 */
77
+ export function lerp(from, to, t) {
78
+ return from + (to - from) * t;
79
+ }
80
+
81
+ /** 矩阵的轻量指纹,用于判断「数据是否真的变了」 */
82
+ export function hashMatrix(matrix) {
83
+ let hash = matrix.length * 2654435761;
84
+ for (const row of matrix) {
85
+ hash = hash * 31 + row.length | 0;
86
+ for (let index = 0; index < row.length; index += 1) {
87
+ const value = row[index];
88
+ hash = hash * 31 + (value === null ? -1 : Math.round(value * 100)) | 0;
89
+ }
90
+ }
91
+ return hash;
92
+ }
@@ -0,0 +1,43 @@
1
+ export interface ForceNode {
2
+ id: string;
3
+ name?: string;
4
+ /** 分组名,用于配色 */
5
+ group?: string;
6
+ /** 节点权重,影响半径(缺省按连接数) */
7
+ size?: number;
8
+ }
9
+ export interface ForceLink {
10
+ source: string;
11
+ target: string;
12
+ value?: number;
13
+ }
14
+ export interface ForceLayoutOptions {
15
+ /** 迭代次数,越多越稳定但越慢 */
16
+ iterations?: number;
17
+ /** 节点半径,用于把节点约束在画布内 */
18
+ radius?: number;
19
+ }
20
+ export interface ForceNodePosition {
21
+ id: string;
22
+ x: number;
23
+ y: number;
24
+ /** 连接数,可用于决定半径 */
25
+ degree: number;
26
+ }
27
+ /**
28
+ * 力导向布局(Fruchterman–Reingold)。
29
+ *
30
+ * 把图当成物理系统:节点两两相斥、有连边的相互吸引,
31
+ * 每轮按「温度」限制最大位移,温度线性冷却到 0 后收敛。
32
+ *
33
+ * 初始位置用「圆环 + 确定性扰动」而不是随机散布:
34
+ * 随机数会让同一份数据在每次刷新时长得不一样,也不利于截图对比;
35
+ * 圆环本身已经足够均匀,能让迭代更快收敛。
36
+ */
37
+ export declare function layoutForce(nodes: ForceNode[], links: ForceLink[], rect: {
38
+ x: number;
39
+ y: number;
40
+ width: number;
41
+ height: number;
42
+ }, options?: ForceLayoutOptions): ForceNodePosition[];
43
+ //# sourceMappingURL=force.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"force.d.ts","sourceRoot":"","sources":["force.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,SAAS;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,eAAe;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,wBAAwB;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,kBAAkB;IACjC,oBAAoB;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,uBAAuB;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,kBAAkB;IAClB,MAAM,EAAE,MAAM,CAAC;CAChB;AAQD;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,SAAS,EAAE,EAClB,KAAK,EAAE,SAAS,EAAE,EAClB,IAAI,EAAE;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAC7D,OAAO,GAAE,kBAAuB,GAC/B,iBAAiB,EAAE,CAsHrB"}
@@ -0,0 +1,133 @@
1
+ /** 确定性伪随机:同一个 seed 恒定,保证 SSR 与截图一致 */
2
+ function pseudo(seed) {
3
+ const value = Math.sin(seed * 12.9898) * 43758.5453;
4
+ return value - Math.floor(value);
5
+ }
6
+
7
+ /**
8
+ * 力导向布局(Fruchterman–Reingold)。
9
+ *
10
+ * 把图当成物理系统:节点两两相斥、有连边的相互吸引,
11
+ * 每轮按「温度」限制最大位移,温度线性冷却到 0 后收敛。
12
+ *
13
+ * 初始位置用「圆环 + 确定性扰动」而不是随机散布:
14
+ * 随机数会让同一份数据在每次刷新时长得不一样,也不利于截图对比;
15
+ * 圆环本身已经足够均匀,能让迭代更快收敛。
16
+ */
17
+ export function layoutForce(nodes, links, rect, options = {}) {
18
+ const count = nodes.length;
19
+ if (count === 0) return [];
20
+ const {
21
+ iterations = 220,
22
+ radius = 8
23
+ } = options;
24
+ const indexOf = new Map(nodes.map((node, index) => [node.id, index]));
25
+ const xs = new Array(count);
26
+ const ys = new Array(count);
27
+ const degrees = new Array(count).fill(0);
28
+ const valid = [];
29
+ for (const link of links) {
30
+ const a = indexOf.get(link.source);
31
+ const b = indexOf.get(link.target);
32
+ if (a === undefined || b === undefined || a === b) continue;
33
+ valid.push({
34
+ a,
35
+ b
36
+ });
37
+ degrees[a] += 1;
38
+ degrees[b] += 1;
39
+ }
40
+
41
+ // 初始:圆环 + 轻微扰动(单节点时居中)
42
+ const cx = rect.x + rect.width / 2;
43
+ const cy = rect.y + rect.height / 2;
44
+ const ringRadius = Math.min(rect.width, rect.height) * 0.34;
45
+ for (let index = 0; index < count; index += 1) {
46
+ if (count === 1) {
47
+ xs[index] = cx;
48
+ ys[index] = cy;
49
+ continue;
50
+ }
51
+ const angle = index / count * Math.PI * 2;
52
+ const jitter = (pseudo(index + 1) - 0.5) * ringRadius * 0.35;
53
+ xs[index] = cx + Math.cos(angle) * (ringRadius + jitter);
54
+ ys[index] = cy + Math.sin(angle) * (ringRadius + jitter);
55
+ }
56
+ if (count === 1) {
57
+ return [{
58
+ id: nodes[0].id,
59
+ x: cx,
60
+ y: cy,
61
+ degree: 0
62
+ }];
63
+ }
64
+ const area = Math.max(1, rect.width * rect.height);
65
+ const k = Math.sqrt(area / count);
66
+ const margin = radius + 2;
67
+ let temperature = Math.min(rect.width, rect.height) / 8;
68
+ for (let step = 0; step < iterations; step += 1) {
69
+ const dx = new Array(count).fill(0);
70
+ const dy = new Array(count).fill(0);
71
+
72
+ // 斥力:所有节点两两排斥
73
+ for (let i = 0; i < count; i += 1) {
74
+ for (let j = i + 1; j < count; j += 1) {
75
+ let deltaX = xs[i] - xs[j];
76
+ let deltaY = ys[i] - ys[j];
77
+ let distance = Math.hypot(deltaX, deltaY);
78
+ if (distance < 0.01) {
79
+ // 完全重合时给一个确定性的偏移方向,避免除零
80
+ deltaX = (pseudo(i + j + 1) - 0.5) * 0.1;
81
+ deltaY = (pseudo(i + j + 2) - 0.5) * 0.1;
82
+ distance = 0.01;
83
+ }
84
+ const force = k * k / distance;
85
+ const ux = deltaX / distance;
86
+ const uy = deltaY / distance;
87
+ dx[i] += ux * force;
88
+ dy[i] += uy * force;
89
+ dx[j] -= ux * force;
90
+ dy[j] -= uy * force;
91
+ }
92
+ }
93
+
94
+ // 引力:有连边的相互吸引
95
+ for (const {
96
+ a,
97
+ b
98
+ } of valid) {
99
+ let deltaX = xs[a] - xs[b];
100
+ let deltaY = ys[a] - ys[b];
101
+ let distance = Math.hypot(deltaX, deltaY);
102
+ if (distance < 0.01) {
103
+ deltaX = 0.01;
104
+ deltaY = 0;
105
+ distance = 0.01;
106
+ }
107
+ const force = distance * distance / k;
108
+ const ux = deltaX / distance;
109
+ const uy = deltaY / distance;
110
+ dx[a] -= ux * force;
111
+ dy[a] -= uy * force;
112
+ dx[b] += ux * force;
113
+ dy[b] += uy * force;
114
+ }
115
+
116
+ // 按温度限制位移,并夹在画布内
117
+ for (let index = 0; index < count; index += 1) {
118
+ const length = Math.hypot(dx[index], dy[index]) || 1;
119
+ const limited = Math.min(length, temperature);
120
+ xs[index] += dx[index] / length * limited;
121
+ ys[index] += dy[index] / length * limited;
122
+ xs[index] = Math.min(rect.x + rect.width - margin, Math.max(rect.x + margin, xs[index]));
123
+ ys[index] = Math.min(rect.y + rect.height - margin, Math.max(rect.y + margin, ys[index]));
124
+ }
125
+ temperature = Math.max(0.5, temperature * 0.975);
126
+ }
127
+ return nodes.map((node, index) => ({
128
+ id: node.id,
129
+ x: xs[index],
130
+ y: ys[index],
131
+ degree: degrees[index]
132
+ }));
133
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * 数值与文本格式化。
3
+ *
4
+ * 图表里最容易出「廉价感」的地方就是刻度:`12000` 不如 `1.2万`,
5
+ * `0.30000000000000004` 更要命。这里统一收口。
6
+ */
7
+ export interface NumberFormatOptions {
8
+ /** 最多保留几位小数,默认按步长自动推断 */
9
+ decimals?: number;
10
+ /** 是否使用紧凑单位(万 / 亿),默认 false */
11
+ compact?: boolean;
12
+ /** 前缀 / 后缀,如 % 或 元 */
13
+ prefix?: string;
14
+ suffix?: string;
15
+ /** 千分位,默认 true */
16
+ thousands?: boolean;
17
+ }
18
+ /** 根据数值量级推断合适的小数位 */
19
+ export declare function inferDecimals(values: readonly number[]): number;
20
+ /** 紧凑单位:中文场景用「万 / 亿」,符合国内看板习惯 */
21
+ export declare function formatCompact(value: number, decimals?: number): string;
22
+ export declare function formatNumber(value: number, options?: NumberFormatOptions): string;
23
+ export declare function formatPercent(value: number, total: number, decimals?: number): string;
24
+ /**
25
+ * 粗略估算文本像素宽度。
26
+ *
27
+ * 为什么要估算而不是真实测量:布局阶段(计算 margin、判断标签是否重叠)
28
+ * 发生在渲染之前,此时没有 DOM 可测量。用「CJK 记 1em、其余记 0.58em」
29
+ * 的经验值足够可靠,误差被 8px 的留白吸收。
30
+ */
31
+ export declare function estimateTextWidth(text: string, fontSize: number): number;
32
+ /** 数值补零宽度,用于让刻度标签右对齐时视觉整齐 */
33
+ export declare function padNumberLabels(labels: string[]): string[];
34
+ //# sourceMappingURL=format.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["format.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,WAAW,mBAAmB;IAClC,yBAAyB;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,+BAA+B;IAC/B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,sBAAsB;IACtB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,kBAAkB;IAClB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAOD,qBAAqB;AACrB,wBAAgB,aAAa,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAS/D;AAED,iCAAiC;AACjC,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,SAAI,GAAG,MAAM,CAOjE;AAED,wBAAgB,YAAY,CAC1B,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,mBAAwB,GAChC,MAAM,CAsBR;AAED,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,QAAQ,SAAI,GACX,MAAM,CAGR;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAcxE;AAED,6BAA6B;AAC7B,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAM1D"}
@@ -0,0 +1,80 @@
1
+ /**
2
+ * 数值与文本格式化。
3
+ *
4
+ * 图表里最容易出「廉价感」的地方就是刻度:`12000` 不如 `1.2万`,
5
+ * `0.30000000000000004` 更要命。这里统一收口。
6
+ */
7
+
8
+ /** 去掉小数末尾多余的 0 */
9
+ function trimZeros(text) {
10
+ return text.includes('.') ? text.replace(/\.?0+$/, '') : text;
11
+ }
12
+
13
+ /** 根据数值量级推断合适的小数位 */
14
+ export function inferDecimals(values) {
15
+ let decimals = 0;
16
+ for (const value of values) {
17
+ if (!Number.isFinite(value)) continue;
18
+ const text = String(value);
19
+ const dot = text.indexOf('.');
20
+ if (dot >= 0) decimals = Math.max(decimals, text.length - dot - 1);
21
+ }
22
+ return Math.min(decimals, 4);
23
+ }
24
+
25
+ /** 紧凑单位:中文场景用「万 / 亿」,符合国内看板习惯 */
26
+ export function formatCompact(value, decimals = 1) {
27
+ const abs = Math.abs(value);
28
+ const sign = value < 0 ? '-' : '';
29
+ if (abs >= 1e8) return `${sign}${trimZeros((abs / 1e8).toFixed(decimals))}亿`;
30
+ if (abs >= 1e4) return `${sign}${trimZeros((abs / 1e4).toFixed(decimals))}万`;
31
+ if (abs >= 1e3) return `${sign}${trimZeros((abs / 1e3).toFixed(decimals))}k`;
32
+ return `${sign}${trimZeros(String(abs))}`;
33
+ }
34
+ export function formatNumber(value, options = {}) {
35
+ if (!Number.isFinite(value)) return '—';
36
+ const {
37
+ decimals,
38
+ compact = false,
39
+ prefix = '',
40
+ suffix = '',
41
+ thousands = true
42
+ } = options;
43
+ if (compact) return `${prefix}${formatCompact(value, decimals ?? 1)}${suffix}`;
44
+ const fixed = decimals === undefined ? trimZeros(String(value)) : value.toFixed(decimals);
45
+ if (!thousands) return `${prefix}${fixed}${suffix}`;
46
+ const [intPart, decimalPart] = fixed.split('.');
47
+ const withSeparator = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
48
+ return `${prefix}${decimalPart ? `${withSeparator}.${decimalPart}` : withSeparator}${suffix}`;
49
+ }
50
+ export function formatPercent(value, total, decimals = 1) {
51
+ if (!total) return '0%';
52
+ return `${trimZeros((value / total * 100).toFixed(decimals))}%`;
53
+ }
54
+
55
+ /**
56
+ * 粗略估算文本像素宽度。
57
+ *
58
+ * 为什么要估算而不是真实测量:布局阶段(计算 margin、判断标签是否重叠)
59
+ * 发生在渲染之前,此时没有 DOM 可测量。用「CJK 记 1em、其余记 0.58em」
60
+ * 的经验值足够可靠,误差被 8px 的留白吸收。
61
+ */
62
+ export function estimateTextWidth(text, fontSize) {
63
+ let width = 0;
64
+ for (const char of text) {
65
+ const code = char.codePointAt(0) ?? 0;
66
+ // CJK 统一表意文字、全角标点、假名、韩文
67
+ const isWide = code >= 0x2e80 && code <= 0x9fff || code >= 0xac00 && code <= 0xd7af || code >= 0xf900 && code <= 0xfaff || code >= 0xff00 && code <= 0xff60 || code >= 0xffe0 && code <= 0xffe6;
68
+ width += isWide ? fontSize : fontSize * 0.58;
69
+ }
70
+ return width;
71
+ }
72
+
73
+ /** 数值补零宽度,用于让刻度标签右对齐时视觉整齐 */
74
+ export function padNumberLabels(labels) {
75
+ const width = labels.reduce((max, item) => Math.max(max, item.length), 0);
76
+ if (width === 0) return labels;
77
+ // 只在全部为纯数字(含符号/千分位)时补零,避免把「1月」也补成「01月」
78
+ if (!labels.every(item => /^-?[\d,.]+$/.test(item))) return labels;
79
+ return labels.map(item => item.padStart(width, ' '));
80
+ }