@easy-editor/materials-dashboard-bar-chart 0.0.2 → 0.0.4

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/src/component.tsx CHANGED
@@ -1,260 +1,311 @@
1
- import { useEffect, useRef, type CSSProperties, type Ref } from 'react'
2
- import * as echarts from 'echarts/core'
3
- import { BarChart } from 'echarts/charts'
4
- import { GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
5
- import { CanvasRenderer } from 'echarts/renderers'
6
- import type { SeriesOption } from 'echarts'
7
- import { DEFAULT_COLORS, DEFAULT_DATA, type DataPoint } from './constants'
8
- import styles from './component.module.css'
9
-
10
- // 按需注册 ECharts 组件
11
- echarts.use([BarChart, GridComponent, TooltipComponent, LegendComponent, CanvasRenderer])
12
-
13
- interface BarChartProps {
14
- ref?: Ref<HTMLDivElement>
15
- data?: DataPoint[]
16
- xField?: string
17
- yFields?: string[]
18
- colors?: string[]
19
- layout?: 'vertical' | 'horizontal'
20
- stacked?: boolean
21
- gradient?: boolean
22
- borderRadius?: number
23
- barGap?: string
24
- showGrid?: boolean
25
- showLegend?: boolean
26
- showTooltip?: boolean
27
- glowEffect?: boolean
28
- style?: CSSProperties
29
- }
30
-
31
- interface SeriesOptions {
32
- stacked: boolean
33
- gradient: boolean
34
- borderRadius: number
35
- glowEffect: boolean
36
- layout: string
37
- barGap?: string
38
- }
39
-
40
- // 获取渐变色
41
- const getGradientColor = (color: string, isVertical: boolean) =>
42
- new echarts.graphic.LinearGradient(isVertical ? 0 : 1, isVertical ? 1 : 0, isVertical ? 0 : 0, isVertical ? 0 : 1, [
43
- { offset: 0, color: `${color}99` },
44
- { offset: 1, color },
45
- ])
46
-
47
- // 构建单个 series 配置
48
- const createBarSeries = (field: string, data: DataPoint[], color: string, options: SeriesOptions): SeriesOption => {
49
- const { stacked, gradient, borderRadius, glowEffect, layout } = options
50
- const isVertical = layout === 'vertical'
51
-
52
- return {
53
- name: field,
54
- type: 'bar' as const,
55
- data: data.map(item => item[field] as number),
56
- stack: stacked ? 'total' : undefined,
57
- barGap: options.barGap,
58
- itemStyle: {
59
- color: gradient ? getGradientColor(color, isVertical) : color,
60
- borderRadius,
61
- shadowColor: glowEffect ? color : 'transparent',
62
- shadowBlur: glowEffect ? 10 : 0,
63
- },
64
- }
65
- }
66
-
67
- // 构建 series 配置
68
- const buildSeries = (yFields: string[], data: DataPoint[], colors: string[], options: SeriesOptions): SeriesOption[] =>
69
- yFields.map((field, index) => {
70
- const color = colors[index % colors.length]
71
- return createBarSeries(field, data, color, options)
72
- })
73
-
74
- // 构建图表配置
75
- const buildOption = (
76
- data: DataPoint[],
77
- xField: string,
78
- series: SeriesOption[],
79
- options: {
80
- layout: string
81
- showGrid: boolean
82
- showLegend: boolean
83
- showTooltip: boolean
84
- glowEffect: boolean
85
- },
86
- ) => {
87
- const { layout, showGrid, showLegend, showTooltip } = options
88
- const isHorizontal = layout === 'horizontal'
89
-
90
- return {
91
- backgroundColor: 'transparent',
92
- grid: {
93
- top: showLegend ? 40 : 20,
94
- right: 20,
95
- bottom: 30,
96
- left: isHorizontal ? 80 : 50,
97
- containLabel: false,
98
- },
99
- xAxis: {
100
- type: isHorizontal ? 'value' : 'category',
101
- data: isHorizontal ? undefined : data.map(item => item[xField]),
102
- axisLine: {
103
- lineStyle: {
104
- color: '#8899aa',
105
- opacity: 0.3,
106
- },
107
- },
108
- axisTick: { show: false },
109
- axisLabel: {
110
- color: '#8899aa',
111
- fontSize: isHorizontal ? 11 : 12,
112
- },
113
- splitLine: {
114
- show: showGrid,
115
- lineStyle: {
116
- color: '#00d4ff',
117
- opacity: 0.1,
118
- type: 'dashed',
119
- },
120
- },
121
- },
122
- yAxis: {
123
- type: isHorizontal ? 'category' : 'value',
124
- data: isHorizontal ? data.map(item => item[xField]) : undefined,
125
- axisLine: {
126
- lineStyle: {
127
- color: '#8899aa',
128
- opacity: 0.3,
129
- },
130
- },
131
- axisTick: { show: false },
132
- axisLabel: {
133
- color: '#8899aa',
134
- fontSize: isHorizontal ? 12 : 11,
135
- },
136
- splitLine: {
137
- show: showGrid && !isHorizontal,
138
- lineStyle: {
139
- color: '#00d4ff',
140
- opacity: 0.1,
141
- type: 'dashed',
142
- },
143
- },
144
- },
145
- tooltip: showTooltip
146
- ? {
147
- trigger: 'axis',
148
- axisPointer: {
149
- type: 'shadow',
150
- },
151
- backgroundColor: 'rgba(0, 20, 40, 0.9)',
152
- borderColor: '#00d4ff',
153
- borderWidth: 1,
154
- textStyle: {
155
- color: '#fff',
156
- },
157
- }
158
- : undefined,
159
- legend: showLegend
160
- ? {
161
- show: true,
162
- top: 10,
163
- textStyle: {
164
- color: '#8899aa',
165
- fontSize: 11,
166
- },
167
- }
168
- : undefined,
169
- series,
170
- }
171
- }
172
-
173
- const BarChartComponent = (props: BarChartProps) => {
174
- const {
175
- ref,
176
- data = DEFAULT_DATA,
177
- xField = 'name',
178
- yFields = ['value1', 'value2'],
179
- colors = DEFAULT_COLORS,
180
- layout = 'vertical',
181
- stacked = false,
182
- gradient = true,
183
- borderRadius = 4,
184
- barGap = '20%',
185
- showGrid = true,
186
- showLegend = true,
187
- showTooltip = true,
188
- glowEffect = true,
189
- style: externalStyle,
190
- } = props
191
-
192
- const chartRef = useRef<HTMLDivElement>(null)
193
- const chartInstance = useRef<echarts.ECharts | null>(null)
194
-
195
- useEffect(() => {
196
- if (!chartRef.current) {
197
- return
198
- }
199
-
200
- chartInstance.current = echarts.init(chartRef.current)
201
-
202
- // 构建 series
203
- const series = buildSeries(yFields, data, colors, {
204
- stacked,
205
- gradient,
206
- borderRadius,
207
- glowEffect,
208
- layout,
209
- barGap,
210
- })
211
-
212
- const option = buildOption(data, xField, series, {
213
- layout,
214
- showGrid,
215
- showLegend,
216
- showTooltip,
217
- glowEffect,
218
- })
219
-
220
- chartInstance.current.setOption(option)
221
-
222
- const resizeObserver = new ResizeObserver(() => {
223
- chartInstance.current?.resize()
224
- })
225
- resizeObserver.observe(chartRef.current)
226
-
227
- return () => {
228
- resizeObserver.disconnect()
229
- chartInstance.current?.dispose()
230
- }
231
- }, [
232
- data,
233
- xField,
234
- yFields,
235
- colors,
236
- layout,
237
- stacked,
238
- gradient,
239
- borderRadius,
240
- barGap,
241
- showGrid,
242
- showLegend,
243
- showTooltip,
244
- glowEffect,
245
- ])
246
-
247
- const containerStyle: CSSProperties = {
248
- width: '100%',
249
- height: '100%',
250
- ...externalStyle,
251
- }
252
-
253
- return (
254
- <div className={styles.container} ref={ref} style={containerStyle}>
255
- <div className={styles.chart} ref={chartRef} />
256
- </div>
257
- )
258
- }
259
-
260
- export default BarChartComponent
1
+ /**
2
+ * Bar Chart Component
3
+ * 柱状图组件 - 支持数据源绑定和事件交互
4
+ */
5
+
6
+ import { useEffect, useMemo, useRef, type CSSProperties } from 'react'
7
+ import * as echarts from 'echarts/core'
8
+ import { BarChart as EChartsBarChart } from 'echarts/charts'
9
+ import { GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
10
+ import { CanvasRenderer } from 'echarts/renderers'
11
+ import type { SeriesOption } from 'echarts'
12
+ import { type MaterialComponet, useDataSource } from '@easy-editor/materials-shared'
13
+ import { DEFAULT_COLORS, type DataPoint } from './constants'
14
+ import styles from './component.module.css'
15
+
16
+ // 按需注册 ECharts 组件
17
+ echarts.use([EChartsBarChart, GridComponent, TooltipComponent, LegendComponent, CanvasRenderer])
18
+
19
+ export interface BarChartProps extends MaterialComponet {
20
+ /** X轴字段 */
21
+ xField?: string
22
+ /** Y轴字段列表 */
23
+ yFields?: string[]
24
+ /** 颜色列表 */
25
+ colors?: string[]
26
+ /** 布局方向 */
27
+ layout?: 'vertical' | 'horizontal'
28
+ /** 堆叠模式 */
29
+ stacked?: boolean
30
+ /** 渐变填充 */
31
+ gradient?: boolean
32
+ /** 圆角 */
33
+ borderRadius?: number
34
+ /** 柱间距 */
35
+ barGap?: string
36
+ /** 显示网格 */
37
+ showGrid?: boolean
38
+ /** 显示图例 */
39
+ showLegend?: boolean
40
+ /** 显示提示 */
41
+ showTooltip?: boolean
42
+ /** 发光效果 */
43
+ glowEffect?: boolean
44
+ /** 点击事件 */
45
+ onClick?: (e: React.MouseEvent) => void
46
+ /** 双击事件 */
47
+ onDoubleClick?: (e: React.MouseEvent) => void
48
+ /** 鼠标进入 */
49
+ onMouseEnter?: (e: React.MouseEvent) => void
50
+ /** 鼠标离开 */
51
+ onMouseLeave?: (e: React.MouseEvent) => void
52
+ }
53
+
54
+ interface SeriesOptions {
55
+ stacked: boolean
56
+ gradient: boolean
57
+ borderRadius: number
58
+ glowEffect: boolean
59
+ layout: string
60
+ barGap?: string
61
+ }
62
+
63
+ // 获取渐变色
64
+ const getGradientColor = (color: string, isVertical: boolean) =>
65
+ new echarts.graphic.LinearGradient(isVertical ? 0 : 1, isVertical ? 1 : 0, isVertical ? 0 : 0, isVertical ? 0 : 1, [
66
+ { offset: 0, color: `${color}99` },
67
+ { offset: 1, color },
68
+ ])
69
+
70
+ // 构建单个 series 配置
71
+ const createBarSeries = (field: string, data: DataPoint[], color: string, options: SeriesOptions): SeriesOption => {
72
+ const { stacked, gradient, borderRadius, glowEffect, layout } = options
73
+ const isVertical = layout === 'vertical'
74
+
75
+ return {
76
+ name: field,
77
+ type: 'bar' as const,
78
+ data: data.map(item => item[field] as number),
79
+ stack: stacked ? 'total' : undefined,
80
+ barGap: options.barGap,
81
+ itemStyle: {
82
+ color: gradient ? getGradientColor(color, isVertical) : color,
83
+ borderRadius,
84
+ shadowColor: glowEffect ? color : 'transparent',
85
+ shadowBlur: glowEffect ? 10 : 0,
86
+ },
87
+ }
88
+ }
89
+
90
+ // 构建 series 配置
91
+ const buildSeries = (yFields: string[], data: DataPoint[], colors: string[], options: SeriesOptions): SeriesOption[] =>
92
+ yFields.map((field, index) => {
93
+ const color = colors[index % colors.length]
94
+ return createBarSeries(field, data, color, options)
95
+ })
96
+
97
+ // 构建图表配置
98
+ const buildOption = (
99
+ data: DataPoint[],
100
+ xField: string,
101
+ series: SeriesOption[],
102
+ options: {
103
+ layout: string
104
+ showGrid: boolean
105
+ showLegend: boolean
106
+ showTooltip: boolean
107
+ glowEffect: boolean
108
+ },
109
+ ) => {
110
+ const { layout, showGrid, showLegend, showTooltip } = options
111
+ const isHorizontal = layout === 'horizontal'
112
+
113
+ return {
114
+ backgroundColor: 'transparent',
115
+ grid: {
116
+ top: showLegend ? 40 : 20,
117
+ right: 20,
118
+ bottom: 30,
119
+ left: isHorizontal ? 80 : 50,
120
+ containLabel: false,
121
+ },
122
+ xAxis: {
123
+ type: isHorizontal ? 'value' : 'category',
124
+ data: isHorizontal ? undefined : data.map(item => item[xField]),
125
+ axisLine: {
126
+ lineStyle: {
127
+ color: '#8899aa',
128
+ opacity: 0.3,
129
+ },
130
+ },
131
+ axisTick: { show: false },
132
+ axisLabel: {
133
+ color: '#8899aa',
134
+ fontSize: isHorizontal ? 11 : 12,
135
+ },
136
+ splitLine: {
137
+ show: showGrid,
138
+ lineStyle: {
139
+ color: '#00d4ff',
140
+ opacity: 0.1,
141
+ type: 'dashed',
142
+ },
143
+ },
144
+ },
145
+ yAxis: {
146
+ type: isHorizontal ? 'category' : 'value',
147
+ data: isHorizontal ? data.map(item => item[xField]) : undefined,
148
+ axisLine: {
149
+ lineStyle: {
150
+ color: '#8899aa',
151
+ opacity: 0.3,
152
+ },
153
+ },
154
+ axisTick: { show: false },
155
+ axisLabel: {
156
+ color: '#8899aa',
157
+ fontSize: isHorizontal ? 12 : 11,
158
+ },
159
+ splitLine: {
160
+ show: showGrid && !isHorizontal,
161
+ lineStyle: {
162
+ color: '#00d4ff',
163
+ opacity: 0.1,
164
+ type: 'dashed',
165
+ },
166
+ },
167
+ },
168
+ tooltip: showTooltip
169
+ ? {
170
+ trigger: 'axis',
171
+ axisPointer: {
172
+ type: 'shadow',
173
+ },
174
+ backgroundColor: 'rgba(0, 20, 40, 0.9)',
175
+ borderColor: '#00d4ff',
176
+ borderWidth: 1,
177
+ textStyle: {
178
+ color: '#fff',
179
+ },
180
+ }
181
+ : undefined,
182
+ legend: showLegend
183
+ ? {
184
+ show: true,
185
+ top: 10,
186
+ textStyle: {
187
+ color: '#8899aa',
188
+ fontSize: 11,
189
+ },
190
+ }
191
+ : undefined,
192
+ series,
193
+ }
194
+ }
195
+
196
+ export const BarChart: React.FC<BarChartProps> = ({
197
+ ref,
198
+ $data,
199
+ __dataSource,
200
+ xField = 'name',
201
+ yFields = ['value1', 'value2'],
202
+ colors = DEFAULT_COLORS,
203
+ layout = 'vertical',
204
+ stacked = false,
205
+ gradient = true,
206
+ borderRadius = 4,
207
+ barGap = '20%',
208
+ showGrid = true,
209
+ showLegend = true,
210
+ showTooltip = true,
211
+ glowEffect = true,
212
+ rotation = 0,
213
+ opacity = 100,
214
+ background = 'transparent',
215
+ style: externalStyle,
216
+ onClick,
217
+ onDoubleClick,
218
+ onMouseEnter,
219
+ onMouseLeave,
220
+ }) => {
221
+ const chartRef = useRef<HTMLDivElement>(null)
222
+ const chartInstance = useRef<echarts.ECharts | null>(null)
223
+
224
+ // 解析数据源
225
+ const dataSource = useDataSource($data, __dataSource)
226
+ const data = useMemo<DataPoint[]>(() => {
227
+ if (dataSource.length > 0 && dataSource[0]?.name && dataSource[0]?.value1 && dataSource[0]?.value2) {
228
+ return dataSource.map(item => ({
229
+ name: String(item.name),
230
+ value1: Number(item.value1),
231
+ value2: Number(item.value2),
232
+ }))
233
+ }
234
+ return []
235
+ }, [dataSource])
236
+
237
+ useEffect(() => {
238
+ if (!chartRef.current) {
239
+ return
240
+ }
241
+
242
+ chartInstance.current = echarts.init(chartRef.current)
243
+
244
+ // 构建 series
245
+ const series = buildSeries(yFields, data, colors, {
246
+ stacked,
247
+ gradient,
248
+ borderRadius,
249
+ glowEffect,
250
+ layout,
251
+ barGap,
252
+ })
253
+
254
+ const option = buildOption(data, xField, series, {
255
+ layout,
256
+ showGrid,
257
+ showLegend,
258
+ showTooltip,
259
+ glowEffect,
260
+ })
261
+
262
+ chartInstance.current.setOption(option)
263
+
264
+ const resizeObserver = new ResizeObserver(() => {
265
+ chartInstance.current?.resize()
266
+ })
267
+ resizeObserver.observe(chartRef.current)
268
+
269
+ return () => {
270
+ resizeObserver.disconnect()
271
+ chartInstance.current?.dispose()
272
+ }
273
+ }, [
274
+ data,
275
+ xField,
276
+ yFields,
277
+ colors,
278
+ layout,
279
+ stacked,
280
+ gradient,
281
+ borderRadius,
282
+ barGap,
283
+ showGrid,
284
+ showLegend,
285
+ showTooltip,
286
+ glowEffect,
287
+ ])
288
+
289
+ const containerStyle: CSSProperties = {
290
+ width: '100%',
291
+ height: '100%',
292
+ transform: rotation !== 0 ? `rotate(${rotation}deg)` : undefined,
293
+ opacity: opacity / 100,
294
+ backgroundColor: background,
295
+ ...externalStyle,
296
+ }
297
+
298
+ return (
299
+ <div
300
+ className={styles.container}
301
+ onClick={onClick}
302
+ onDoubleClick={onDoubleClick}
303
+ onMouseEnter={onMouseEnter}
304
+ onMouseLeave={onMouseLeave}
305
+ ref={ref}
306
+ style={containerStyle}
307
+ >
308
+ <div className={styles.chart} ref={chartRef} />
309
+ </div>
310
+ )
311
+ }