@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.
Files changed (68) hide show
  1. package/esm/charts/area-chart/index.js +27 -23
  2. package/esm/charts/bar-chart/index.js +17 -21
  3. package/esm/charts/candlestick-chart/index.js +242 -180
  4. package/esm/charts/combo-chart/index.js +8 -20
  5. package/esm/charts/funnel-chart/index.js +133 -108
  6. package/esm/charts/gauge-chart/index.js +102 -105
  7. package/esm/charts/graph-chart/index.js +154 -114
  8. package/esm/charts/heatmap-chart/index.js +179 -135
  9. package/esm/charts/line-chart/index.js +28 -24
  10. package/esm/charts/pie-chart/index.d.ts.map +1 -1
  11. package/esm/charts/pie-chart/index.js +198 -139
  12. package/esm/charts/radar-chart/index.d.ts.map +1 -1
  13. package/esm/charts/radar-chart/index.js +154 -128
  14. package/esm/charts/ring-progress/index.js +81 -82
  15. package/esm/charts/sankey-chart/index.js +171 -141
  16. package/esm/charts/scatter-chart/index.d.ts.map +1 -1
  17. package/esm/charts/scatter-chart/index.js +225 -159
  18. package/esm/charts/sparkline/index.js +81 -66
  19. package/esm/charts/tree-chart/index.js +128 -102
  20. package/esm/charts/treemap-chart/index.js +162 -141
  21. package/esm/charts/waterfall-chart/index.js +165 -121
  22. package/esm/components/Legend.d.ts +3 -2
  23. package/esm/components/Legend.d.ts.map +1 -1
  24. package/esm/components/Legend.js +79 -48
  25. package/esm/components/Tooltip.js +62 -71
  26. package/esm/components/chart/index.d.ts +4 -1
  27. package/esm/components/chart/index.d.ts.map +1 -1
  28. package/esm/components/chart/index.js +80 -102
  29. package/esm/components/chart.less +13 -0
  30. package/esm/core/animation.js +31 -43
  31. package/esm/core/export.d.ts +34 -0
  32. package/esm/core/export.d.ts.map +1 -0
  33. package/esm/core/export.js +390 -0
  34. package/esm/core/force.js +22 -45
  35. package/esm/core/format.js +48 -52
  36. package/esm/core/hierarchy.js +60 -92
  37. package/esm/core/layout.js +32 -50
  38. package/esm/core/sampling.js +30 -53
  39. package/esm/core/sankey.js +35 -55
  40. package/esm/core/scale.js +72 -86
  41. package/esm/core/shape.js +101 -133
  42. package/esm/core/sync.d.ts +41 -0
  43. package/esm/core/sync.d.ts.map +1 -0
  44. package/esm/core/sync.js +120 -0
  45. package/esm/hooks/useChartExport.d.ts +19 -0
  46. package/esm/hooks/useChartExport.d.ts.map +1 -0
  47. package/esm/hooks/useChartExport.js +35 -0
  48. package/esm/hooks/useChartTheme.js +17 -19
  49. package/esm/hooks/useChartTransition.js +58 -73
  50. package/esm/hooks/useElementSize.js +19 -28
  51. package/esm/hooks/usePlotPointer.d.ts +23 -0
  52. package/esm/hooks/usePlotPointer.d.ts.map +1 -0
  53. package/esm/hooks/usePlotPointer.js +94 -0
  54. package/esm/index.d.ts +6 -0
  55. package/esm/index.d.ts.map +1 -1
  56. package/esm/index.js +136 -47
  57. package/esm/internal/CartesianChart.d.ts.map +1 -1
  58. package/esm/internal/CartesianChart.js +666 -485
  59. package/esm/internal/model.js +44 -60
  60. package/esm/scene/canvas.js +100 -107
  61. package/esm/scene/svg.js +106 -99
  62. package/esm/scene/types.js +8 -15
  63. package/esm/style.css +10 -0
  64. package/esm/theme/index.js +143 -171
  65. package/esm/types.d.ts +4 -0
  66. package/esm/types.d.ts.map +1 -1
  67. package/esm/types.js +0 -1
  68. package/package.json +1 -1
@@ -1,8 +1,13 @@
1
1
  'use client';
2
- import React, { useMemo, useState } from 'react';
3
- import { classNames, prefixCls } from '@aura-react-comp/shared';
2
+ // src/charts/waterfall-chart/index.tsx
3
+ import { useMemo, useState } from "react";
4
+ import { classNames, prefixCls } from "@aura-react-comp/shared";
4
5
  import { SvgScene } from "../../scene/svg";
5
- import { createBandScale, createLinearScale, linearTicks } from "../../core/scale";
6
+ import {
7
+ createBandScale,
8
+ createLinearScale,
9
+ linearTicks
10
+ } from "../../core/scale";
6
11
  import { computeLayout, suggestTickCount } from "../../core/layout";
7
12
  import { formatNumber } from "../../core/format";
8
13
  import { roundedRectPath } from "../../core/shape";
@@ -12,21 +17,13 @@ import { useElementSize } from "../../hooks/useElementSize";
12
17
  import { useChartTransition } from "../../hooks/useChartTransition";
13
18
  import { resolveAnimation, staggerProgress } from "../../core/animation";
14
19
  import { Tooltip } from "../../components/Tooltip";
15
- import { jsx as _jsx } from "react/jsx-runtime";
16
- import { jsxs as _jsxs } from "react/jsx-runtime";
17
- /**
18
- * 瀑布图。
19
- *
20
- * 用柱子的「起点 + 终点」表达增减过程,最后落到总计。
21
- * 与堆叠柱的区别:堆叠柱的每段都从 0 起算的相对量,瀑布图的每一段都是**增量**,
22
- * 位置取决于此前所有项的累计。
23
- */
24
- export function WaterfallChart({
20
+ import { jsx, jsxs } from "react/jsx-runtime";
21
+ function WaterfallChart({
25
22
  data = [],
26
- width = '100%',
23
+ width = "100%",
27
24
  height = 320,
28
25
  positiveColor,
29
- negativeColor = '#ef4444',
26
+ negativeColor = "#ef4444",
30
27
  totalColor,
31
28
  connector = true,
32
29
  theme: themeInput,
@@ -36,19 +33,16 @@ export function WaterfallChart({
36
33
  className,
37
34
  style,
38
35
  ariaLabel,
39
- emptyText = '暂无数据'
36
+ emptyText = "暂无数据"
40
37
  }) {
41
38
  const theme = useChartTheme(themeInput, palette);
42
- const {
43
- ref,
44
- size
45
- } = useElementSize();
39
+ const { ref, size } = useElementSize();
46
40
  const [activeIndex, setActiveIndex] = useState(null);
47
41
  const rows = useMemo(() => {
48
42
  const up = positiveColor ?? colorAt(theme, 0);
49
43
  const total = totalColor ?? colorAt(theme, 1);
50
44
  let running = 0;
51
- return data.map(item => {
45
+ return data.map((item) => {
52
46
  if (item.isTotal) {
53
47
  const row = {
54
48
  name: item.name,
@@ -73,7 +67,8 @@ export function WaterfallChart({
73
67
  });
74
68
  }, [data, positiveColor, negativeColor, totalColor, theme]);
75
69
  const domain = useMemo(() => {
76
- if (!rows.length) return [0, 1];
70
+ if (!rows.length)
71
+ return [0, 1];
77
72
  let min = 0;
78
73
  let max = 0;
79
74
  for (const row of rows) {
@@ -82,43 +77,61 @@ export function WaterfallChart({
82
77
  }
83
78
  return min === max ? [0, max + 1] : [min, max];
84
79
  }, [rows]);
85
- const yTicks = useMemo(() => linearTicks(domain[0], domain[1], suggestTickCount(Math.max(60, height - 48))), [domain, height]);
86
- const yLabels = useMemo(() => yTicks.map(tick => formatNumber(tick)), [yTicks]);
87
- const layout = useMemo(() => computeLayout({
88
- width: Math.max(size.width, 0),
89
- height: Math.max(size.height, 0),
90
- yLabels,
91
- xLabels: rows.map(row => row.name),
92
- theme
93
- }), [size.width, size.height, yLabels, rows, theme]);
94
- const {
95
- plot
96
- } = layout;
97
- const xScale = useMemo(() => createBandScale({
98
- domain: rows.map(row => row.name),
99
- range: [plot.x, plot.x + plot.width],
100
- paddingInner: 0.36,
101
- paddingOuter: 0.12
102
- }), [rows, plot.x, plot.width]);
103
- const yScale = useMemo(() => createLinearScale({
104
- domain,
105
- range: [plot.y + plot.height, plot.y]
106
- }), [domain, plot.y, plot.height]);
107
- const animKey = typeof animation === 'object' ? JSON.stringify(animation) : String(animation);
108
- const animConfig = useMemo(() => resolveAnimation(animation),
109
- // eslint-disable-next-line react-hooks/exhaustive-deps
110
- [animKey]);
111
-
112
- // 每根柱子用 [起点, 终点] 两个数描述,插值时两端一起平滑移动
113
- const valueMatrix = useMemo(() => rows.map(row => [row.start, row.end]), [rows]);
80
+ const yTicks = useMemo(
81
+ () => linearTicks(
82
+ domain[0],
83
+ domain[1],
84
+ suggestTickCount(Math.max(60, height - 48))
85
+ ),
86
+ [domain, height]
87
+ );
88
+ const yLabels = useMemo(
89
+ () => yTicks.map((tick) => formatNumber(tick)),
90
+ [yTicks]
91
+ );
92
+ const layout = useMemo(
93
+ () => computeLayout({
94
+ width: Math.max(size.width, 0),
95
+ height: Math.max(size.height, 0),
96
+ yLabels,
97
+ xLabels: rows.map((row) => row.name),
98
+ theme
99
+ }),
100
+ [size.width, size.height, yLabels, rows, theme]
101
+ );
102
+ const { plot } = layout;
103
+ const xScale = useMemo(
104
+ () => createBandScale({
105
+ domain: rows.map((row) => row.name),
106
+ range: [plot.x, plot.x + plot.width],
107
+ paddingInner: 0.36,
108
+ paddingOuter: 0.12
109
+ }),
110
+ [rows, plot.x, plot.width]
111
+ );
112
+ const yScale = useMemo(
113
+ () => createLinearScale({ domain, range: [plot.y + plot.height, plot.y] }),
114
+ [domain, plot.y, plot.height]
115
+ );
116
+ const animKey = typeof animation === "object" ? JSON.stringify(animation) : String(animation);
117
+ const animConfig = useMemo(
118
+ () => resolveAnimation(animation),
119
+ // eslint-disable-next-line react-hooks/exhaustive-deps
120
+ [animKey]
121
+ );
122
+ const valueMatrix = useMemo(
123
+ () => rows.map((row) => [row.start, row.end]),
124
+ [rows]
125
+ );
114
126
  const transition = useChartTransition(valueMatrix, animConfig);
115
127
  const progress = transition.progress;
116
128
  const scene = useMemo(() => {
117
- if (!rows.length || plot.width <= 0) return [];
129
+ if (!rows.length || plot.width <= 0)
130
+ return [];
118
131
  const nodes = [];
119
132
  yTicks.forEach((tick, index) => {
120
133
  nodes.push({
121
- kind: 'line',
134
+ kind: "line",
122
135
  key: `grid-${index}`,
123
136
  x1: plot.x,
124
137
  y1: yScale.scale(tick),
@@ -126,27 +139,25 @@ export function WaterfallChart({
126
139
  y2: yScale.scale(tick),
127
140
  stroke: theme.grid.line,
128
141
  strokeWidth: theme.grid.lineWidth,
129
- strokeDasharray: theme.grid.dash.join(' ') || undefined
142
+ strokeDasharray: theme.grid.dash.join(" ") || void 0
130
143
  });
131
144
  nodes.push({
132
- kind: 'text',
145
+ kind: "text",
133
146
  key: `yl-${index}`,
134
147
  x: plot.x - theme.axis.tickSize - 4,
135
148
  y: yScale.scale(tick),
136
149
  text: yLabels[index],
137
150
  fill: theme.axis.label,
138
151
  fontSize: theme.font.size,
139
- anchor: 'end',
140
- baseline: 'middle',
152
+ anchor: "end",
153
+ baseline: "middle",
141
154
  fontFamily: theme.font.family
142
155
  });
143
156
  });
144
-
145
- // 零基线(负值场景下必须显式画出)
146
157
  if (domain[0] < 0) {
147
158
  nodes.push({
148
- kind: 'line',
149
- key: 'zero',
159
+ kind: "line",
160
+ key: "zero",
150
161
  x1: plot.x,
151
162
  y1: yScale.scale(0),
152
163
  x2: plot.x + plot.width,
@@ -157,12 +168,18 @@ export function WaterfallChart({
157
168
  }
158
169
  const barWidth = Math.min(xScale.bandWidth, 56);
159
170
  rows.forEach((row, index) => {
171
+ var _a, _b;
160
172
  const cx = xScale.center(row.name);
161
- if (cx === undefined) return;
162
- // 逐柱错峰 + 从各自起点生长(不是整排从底部弹起)
163
- const local = staggerProgress(progress, index, rows.length, animConfig.stagger);
164
- const animatedStart = transition.values[index]?.[0] ?? row.start;
165
- const animatedEnd = transition.values[index]?.[1] ?? row.end;
173
+ if (cx === void 0)
174
+ return;
175
+ const local = staggerProgress(
176
+ progress,
177
+ index,
178
+ rows.length,
179
+ animConfig.stagger
180
+ );
181
+ const animatedStart = ((_a = transition.values[index]) == null ? void 0 : _a[0]) ?? row.start;
182
+ const animatedEnd = ((_b = transition.values[index]) == null ? void 0 : _b[1]) ?? row.end;
166
183
  const drawnEnd = animatedStart + (animatedEnd - animatedStart) * local;
167
184
  const top = yScale.scale(drawnEnd);
168
185
  const bottom = yScale.scale(animatedStart);
@@ -170,7 +187,7 @@ export function WaterfallChart({
170
187
  const h = Math.max(2, Math.abs(bottom - top));
171
188
  const isActive = activeIndex === index;
172
189
  nodes.push({
173
- kind: 'path',
190
+ kind: "path",
174
191
  key: `bar-${index}`,
175
192
  d: roundedRectPath(cx - barWidth / 2, y, barWidth, h, 3, {
176
193
  tl: true,
@@ -178,17 +195,15 @@ export function WaterfallChart({
178
195
  }),
179
196
  fill: row.color,
180
197
  opacity: activeIndex === null || isActive ? 1 : 0.55,
181
- pointerEvents: 'none'
198
+ pointerEvents: "none"
182
199
  });
183
-
184
- // 连接线:从前一根柱的终点水平引到本柱起点
185
200
  if (connector && index > 0) {
186
201
  const prev = rows[index - 1];
187
202
  const prevCx = xScale.center(prev.name);
188
- if (prevCx !== undefined) {
203
+ if (prevCx !== void 0) {
189
204
  const linkY = yScale.scale(prev.end);
190
205
  nodes.push({
191
- kind: 'line',
206
+ kind: "line",
192
207
  key: `link-${index}`,
193
208
  x1: prevCx + barWidth / 2,
194
209
  y1: linkY,
@@ -196,35 +211,51 @@ export function WaterfallChart({
196
211
  y2: linkY,
197
212
  stroke: theme.axis.line,
198
213
  strokeWidth: 1,
199
- strokeDasharray: '3 3',
200
- pointerEvents: 'none'
214
+ strokeDasharray: "3 3",
215
+ pointerEvents: "none"
201
216
  });
202
217
  }
203
218
  }
204
219
  nodes.push({
205
- kind: 'text',
220
+ kind: "text",
206
221
  key: `xlabel-${index}`,
207
222
  x: cx,
208
223
  y: plot.y + plot.height + theme.axis.tickSize + theme.font.size,
209
224
  text: row.name,
210
225
  fill: theme.axis.label,
211
226
  fontSize: theme.font.size,
212
- anchor: 'middle',
227
+ anchor: "middle",
213
228
  fontFamily: theme.font.family
214
229
  });
215
230
  });
216
231
  return nodes;
217
- }, [rows, plot, yTicks, yLabels, yScale, xScale, theme, progress, activeIndex, domain, connector, transition.values, animConfig.stagger]);
218
- const indexFromPointer = clientX => {
232
+ }, [
233
+ rows,
234
+ plot,
235
+ yTicks,
236
+ yLabels,
237
+ yScale,
238
+ xScale,
239
+ theme,
240
+ progress,
241
+ activeIndex,
242
+ domain,
243
+ connector,
244
+ transition.values,
245
+ animConfig.stagger
246
+ ]);
247
+ const indexFromPointer = (clientX) => {
219
248
  const element = ref.current;
220
- if (!element || !rows.length) return null;
249
+ if (!element || !rows.length)
250
+ return null;
221
251
  const rect = element.getBoundingClientRect();
222
252
  const localX = clientX - rect.left;
223
253
  let nearest = null;
224
254
  let min = Infinity;
225
255
  rows.forEach((row, index) => {
226
256
  const cx = xScale.center(row.name);
227
- if (cx === undefined) return;
257
+ if (cx === void 0)
258
+ return;
228
259
  const distance = Math.abs(cx - localX);
229
260
  if (distance < min) {
230
261
  min = distance;
@@ -237,45 +268,58 @@ export function WaterfallChart({
237
268
  const tooltipPayload = active ? {
238
269
  label: active.name,
239
270
  row: null,
240
- series: [{
241
- key: active.name,
242
- name: active.isTotal ? '累计' : '增减',
243
- color: active.color,
244
- value: active.isTotal ? active.end : active.value,
245
- formatted: formatNumber(active.isTotal ? active.end : active.value)
246
- }]
271
+ series: [
272
+ {
273
+ key: active.name,
274
+ name: active.isTotal ? "累计" : "增减",
275
+ color: active.color,
276
+ value: active.isTotal ? active.end : active.value,
277
+ formatted: formatNumber(active.isTotal ? active.end : active.value)
278
+ }
279
+ ]
247
280
  } : null;
248
- const autoAriaLabel = ariaLabel ?? `瀑布图:${rows.map(row => row.name).join('、')}`;
249
- return /*#__PURE__*/_jsx("div", {
250
- className: classNames(prefixCls('chart'), className),
251
- style: {
252
- width,
253
- height,
254
- ...style
255
- },
256
- "data-aura-chart": "waterfall",
257
- children: /*#__PURE__*/_jsxs("div", {
258
- ref: ref,
259
- className: prefixCls('chart__plot'),
260
- onPointerMove: event => setActiveIndex(indexFromPointer(event.clientX)),
261
- onPointerLeave: () => setActiveIndex(null),
262
- children: [rows.length === 0 || size.width === 0 ? /*#__PURE__*/_jsx("div", {
263
- className: prefixCls('chart__empty'),
264
- children: emptyText
265
- }) : /*#__PURE__*/_jsx(SvgScene, {
266
- nodes: scene,
267
- width: size.width,
268
- height: size.height,
269
- ariaLabel: autoAriaLabel
270
- }), /*#__PURE__*/_jsx(Tooltip, {
271
- payload: tooltipPayload,
272
- x: active ? xScale.center(active.name) ?? 0 : 0,
273
- y: active ? yScale.scale(Math.max(active.start, active.end)) - 10 : 0,
274
- containerWidth: size.width,
275
- containerHeight: size.height,
276
- theme: theme,
277
- config: tooltip
278
- })]
279
- })
280
- });
281
- }
281
+ const autoAriaLabel = ariaLabel ?? `瀑布图:${rows.map((row) => row.name).join("、")}`;
282
+ return /* @__PURE__ */ jsx(
283
+ "div",
284
+ {
285
+ className: classNames(prefixCls("chart"), className),
286
+ style: { width, height, ...style },
287
+ "data-aura-chart": "waterfall",
288
+ children: /* @__PURE__ */ jsxs(
289
+ "div",
290
+ {
291
+ ref,
292
+ className: prefixCls("chart__plot"),
293
+ onPointerMove: (event) => setActiveIndex(indexFromPointer(event.clientX)),
294
+ onPointerLeave: () => setActiveIndex(null),
295
+ children: [
296
+ rows.length === 0 || size.width === 0 ? /* @__PURE__ */ jsx("div", { className: prefixCls("chart__empty"), children: emptyText }) : /* @__PURE__ */ jsx(
297
+ SvgScene,
298
+ {
299
+ nodes: scene,
300
+ width: size.width,
301
+ height: size.height,
302
+ ariaLabel: autoAriaLabel
303
+ }
304
+ ),
305
+ /* @__PURE__ */ jsx(
306
+ Tooltip,
307
+ {
308
+ payload: tooltipPayload,
309
+ x: active ? xScale.center(active.name) ?? 0 : 0,
310
+ y: active ? yScale.scale(Math.max(active.start, active.end)) - 10 : 0,
311
+ containerWidth: size.width,
312
+ containerHeight: size.height,
313
+ theme,
314
+ config: tooltip
315
+ }
316
+ )
317
+ ]
318
+ }
319
+ )
320
+ }
321
+ );
322
+ }
323
+ export {
324
+ WaterfallChart
325
+ };
@@ -11,13 +11,14 @@ export interface LegendProps {
11
11
  items: LegendItem[];
12
12
  theme: ChartTheme;
13
13
  config?: boolean | LegendConfig;
14
- onToggle: (key: string) => void;
14
+ /** 点击切换系列显隐回调;未传或 selectable=false 时仅作静态展示 */
15
+ onToggle?: (key: string) => void;
15
16
  }
16
17
  /**
17
18
  * 图例。
18
19
  *
19
20
  * 刻意用 DOM 而不是画进 SVG:
20
- * · 可以用原生 button,键盘可达、可聚焦,天然符合无障碍要求;
21
+ * · 可筛选时用原生 button,键盘可达、可聚焦,天然符合无障碍要求;
21
22
  * · 换行、省略号交给 CSS,省去手写文本换行计算。
22
23
  */
23
24
  export declare function Legend({ items, theme, config, onToggle }: LegendProps): React.JSX.Element | null;
@@ -1 +1 @@
1
- {"version":3,"file":"Legend.d.ts","sourceRoot":"","sources":["Legend.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C,MAAM,WAAW,UAAU;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,UAAU,EAAE,CAAC;IACpB,KAAK,EAAE,UAAU,CAAC;IAClB,MAAM,CAAC,EAAE,OAAO,GAAG,YAAY,CAAC;IAChC,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CACjC;AAED;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,WAAW,4BA8CrE"}
1
+ {"version":3,"file":"Legend.d.ts","sourceRoot":"","sources":["Legend.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C,MAAM,WAAW,UAAU;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,UAAU,EAAE,CAAC;IACpB,KAAK,EAAE,UAAU,CAAC;IAClB,MAAM,CAAC,EAAE,OAAO,GAAG,YAAY,CAAC;IAChC,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CAClC;AAED;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,WAAW,4BA2ErE"}
@@ -1,50 +1,81 @@
1
1
  'use client';
2
- import React from 'react';
3
- import { classNames, prefixCls } from '@aura-react-comp/shared';
4
- import { jsx as _jsx } from "react/jsx-runtime";
5
- import { jsxs as _jsxs } from "react/jsx-runtime";
6
- /**
7
- * 图例。
8
- *
9
- * 刻意用 DOM 而不是画进 SVG:
10
- * · 可以用原生 button,键盘可达、可聚焦,天然符合无障碍要求;
11
- * · 换行、省略号交给 CSS,省去手写文本换行计算。
12
- */
13
- export function Legend({
14
- items,
15
- theme,
16
- config,
17
- onToggle
18
- }) {
19
- if (config === false || items.length === 0) return null;
20
- const options = typeof config === 'object' ? config : {};
21
- const {
22
- position = 'top',
23
- align = 'left'
24
- } = options;
25
- if (options.show === false) return null;
26
- return /*#__PURE__*/_jsx("div", {
27
- className: classNames(prefixCls('chart__legend'), position === 'bottom' && prefixCls('chart__legend--bottom'), align !== 'left' && prefixCls(`chart__legend--${align}`)),
28
- role: "group",
29
- "aria-label": "\u56FE\u4F8B",
30
- children: items.map(item => /*#__PURE__*/_jsxs("button", {
31
- type: "button",
32
- className: classNames(prefixCls('chart__legend-item'), !item.visible && prefixCls('chart__legend-item--inactive')),
33
- "aria-pressed": item.visible,
34
- title: `${item.visible ? '隐藏' : '显示'} ${item.name}`,
35
- onClick: () => onToggle(item.key),
36
- children: [/*#__PURE__*/_jsx("span", {
37
- className: prefixCls('chart__legend-dot'),
38
- style: {
39
- background: item.color,
40
- opacity: item.visible ? 1 : 0.5
2
+ // src/components/Legend.tsx
3
+ import { classNames, prefixCls } from "@aura-react-comp/shared";
4
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
5
+ function Legend({ items, theme, config, onToggle }) {
6
+ if (config === false || items.length === 0)
7
+ return null;
8
+ const options = typeof config === "object" ? config : {};
9
+ const { position = "top", align = "left", selectable = true } = options;
10
+ if (options.show === false)
11
+ return null;
12
+ const interactive = selectable !== false && Boolean(onToggle);
13
+ return /* @__PURE__ */ jsx(
14
+ "div",
15
+ {
16
+ className: classNames(
17
+ prefixCls("chart__legend"),
18
+ position === "bottom" && prefixCls("chart__legend--bottom"),
19
+ align !== "left" && prefixCls(`chart__legend--${align}`)
20
+ ),
21
+ role: "group",
22
+ "aria-label": "图例",
23
+ children: items.map((item) => {
24
+ const content = /* @__PURE__ */ jsxs(Fragment, { children: [
25
+ /* @__PURE__ */ jsx(
26
+ "span",
27
+ {
28
+ className: prefixCls("chart__legend-dot"),
29
+ style: {
30
+ background: item.color,
31
+ opacity: item.visible ? 1 : 0.45
32
+ }
33
+ }
34
+ ),
35
+ /* @__PURE__ */ jsx(
36
+ "span",
37
+ {
38
+ className: prefixCls("chart__legend-text"),
39
+ style: {
40
+ color: item.visible ? theme.legend.text : theme.legend.textInactive
41
+ },
42
+ children: item.name
43
+ }
44
+ )
45
+ ] });
46
+ if (!interactive) {
47
+ return /* @__PURE__ */ jsx(
48
+ "span",
49
+ {
50
+ className: classNames(
51
+ prefixCls("chart__legend-item"),
52
+ prefixCls("chart__legend-item--static"),
53
+ !item.visible && prefixCls("chart__legend-item--inactive")
54
+ ),
55
+ children: content
56
+ },
57
+ item.key
58
+ );
41
59
  }
42
- }), /*#__PURE__*/_jsx("span", {
43
- style: {
44
- color: item.visible ? theme.legend.text : theme.legend.textInactive
45
- },
46
- children: item.name
47
- })]
48
- }, item.key))
49
- });
50
- }
60
+ return /* @__PURE__ */ jsx(
61
+ "button",
62
+ {
63
+ type: "button",
64
+ className: classNames(
65
+ prefixCls("chart__legend-item"),
66
+ !item.visible && prefixCls("chart__legend-item--inactive")
67
+ ),
68
+ "aria-pressed": item.visible,
69
+ title: `${item.visible ? "隐藏" : "显示"} ${item.name}`,
70
+ onClick: () => onToggle == null ? void 0 : onToggle(item.key),
71
+ children: content
72
+ },
73
+ item.key
74
+ );
75
+ })
76
+ }
77
+ );
78
+ }
79
+ export {
80
+ Legend
81
+ };