@easy-editor/materials-dashboard-gauge-chart 0.0.3 → 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.
@@ -0,0 +1,61 @@
1
+ // 自定义插件:处理 echarts 模块的命名导出
2
+ export const echartsExternalPlugin = () => {
3
+ const ECHARTS_MODULES = ['echarts', 'echarts/core', 'echarts/charts', 'echarts/components', 'echarts/renderers']
4
+ const VIRTUAL_PREFIX = '\0virtual:echarts-external:'
5
+
6
+ return {
7
+ name: 'vite-plugin-echarts-external',
8
+ enforce: 'pre',
9
+ resolveId(id) {
10
+ if (ECHARTS_MODULES.includes(id)) {
11
+ return VIRTUAL_PREFIX + id
12
+ }
13
+ return null
14
+ },
15
+ load(id) {
16
+ if (!id.startsWith(VIRTUAL_PREFIX)) {
17
+ return null
18
+ }
19
+ const moduleName = id.slice(VIRTUAL_PREFIX.length)
20
+ const globalKey = moduleName.includes('/') ? `["${moduleName}"]` : `.${moduleName}`
21
+
22
+ return `
23
+ // External module: ${moduleName} -> window${globalKey}
24
+ const mod = window${globalKey} || window.echarts;
25
+ if (!mod) {
26
+ throw new Error('External dependency "${moduleName}" is not available on window.');
27
+ }
28
+ export default mod;
29
+ export const {
30
+ ${getExportsForModule(moduleName)}
31
+ } = mod;
32
+ `
33
+ },
34
+ }
35
+ }
36
+
37
+ // 根据模块名返回需要导出的属性
38
+ function getExportsForModule(moduleName) {
39
+ switch (moduleName) {
40
+ case 'echarts/core':
41
+ return 'use, init, graphic, registerMap, getMap'
42
+ case 'echarts/charts':
43
+ return 'MapChart, LinesChart, EffectScatterChart, BarChart, LineChart, PieChart, RadarChart, ScatterChart, GaugeChart'
44
+ case 'echarts/components':
45
+ return 'GeoComponent, TooltipComponent, LegendComponent, GridComponent, VisualMapComponent, TitleComponent'
46
+ case 'echarts/renderers':
47
+ return 'CanvasRenderer, SVGRenderer'
48
+ default:
49
+ return 'use, init, graphic, registerMap'
50
+ }
51
+ }
52
+
53
+ // echarts 模块列表
54
+ export const ECHARTS_EXTERNALS = ['echarts', 'echarts/core', 'echarts/charts', 'echarts/components', 'echarts/renderers']
55
+ export const ECHARTS_GLOBALS = {
56
+ echarts: 'echarts',
57
+ 'echarts/core': 'echarts',
58
+ 'echarts/charts': 'echarts',
59
+ 'echarts/components': 'echarts',
60
+ 'echarts/renderers': 'echarts',
61
+ }
package/CHANGELOG.md CHANGED
@@ -1,15 +1,21 @@
1
- # @easy-editor/materials-dashboard-gauge-chart
2
-
3
- ## 0.0.3
4
-
5
- ### Patch Changes
6
-
7
- - perf: configure & datasource
8
- - Updated dependencies
9
- - @easy-editor/materials-shared@0.0.1
10
-
11
- ## 0.0.2
12
-
13
- ### Patch Changes
14
-
15
- - feat: new setters
1
+ # @easy-editor/materials-dashboard-gauge-chart
2
+
3
+ ## 0.0.4
4
+
5
+ ### Patch Changes
6
+
7
+ - fix: component export error
8
+
9
+ ## 0.0.3
10
+
11
+ ### Patch Changes
12
+
13
+ - perf: configure & datasource
14
+ - Updated dependencies
15
+ - @easy-editor/materials-shared@0.0.1
16
+
17
+ ## 0.0.2
18
+
19
+ ### Patch Changes
20
+
21
+ - feat: new setters
@@ -2,9 +2,18 @@
2
2
  * @easy-editor/easypack configuration
3
3
  * @type {import('@easy-editor/easypack').EasypackConfig}
4
4
  */
5
+ import { ECHARTS_EXTERNALS, ECHARTS_GLOBALS, echartsExternalPlugin } from './.vite/echarts-external-plugin'
6
+
5
7
  export default {
6
8
  preset: 'material',
7
9
  dev: {
8
10
  port: 5001,
9
11
  },
12
+ rollup: {
13
+ external: ECHARTS_EXTERNALS,
14
+ output: {
15
+ globals: ECHARTS_GLOBALS,
16
+ },
17
+ },
18
+ vitePlugins: [echartsExternalPlugin()],
10
19
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-editor/materials-dashboard-gauge-chart",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "Gauge Chart component for EasyEditor dashboard",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/src/component.tsx CHANGED
@@ -1,225 +1,227 @@
1
- /**
2
- * Gauge Chart Component
3
- * 仪表盘组件 - 支持数据源绑定和事件交互
4
- */
5
-
6
- import { useEffect, useMemo, useRef, type CSSProperties } from 'react'
7
- import * as echarts from 'echarts/core'
8
- import { GaugeChart as EChartsGaugeChart } from 'echarts/charts'
9
- import { TooltipComponent } from 'echarts/components'
10
- import { CanvasRenderer } from 'echarts/renderers'
11
- import type { EChartsOption } from 'echarts'
12
- import { type MaterialComponet, useDataSource } from '@easy-editor/materials-shared'
13
- import { DEFAULT_RANGES, type GaugeRange } from './constants'
14
- import styles from './component.module.css'
15
-
16
- // 按需注册 ECharts 组件
17
- echarts.use([EChartsGaugeChart, TooltipComponent, CanvasRenderer])
18
-
19
- export interface GaugeChartProps extends MaterialComponet {
20
- /** 最小值 */
21
- min?: number
22
- /** 最大值 */
23
- max?: number
24
- /** 单位 */
25
- unit?: string
26
- /** 显示刻度 */
27
- showScale?: boolean
28
- /** 刻度数量 */
29
- divisions?: number
30
- /** 显示刻度值 */
31
- showLabels?: boolean
32
- /** 指针类型 */
33
- pointerType?: 'needle' | 'triangle' | 'rect'
34
- /** 指针颜色 */
35
- pointerColor?: string
36
- /** 颜色区间 */
37
- ranges?: GaugeRange[]
38
- /** 发光效果 */
39
- glowEffect?: boolean
40
- /** 点击事件 */
41
- onClick?: (e: React.MouseEvent) => void
42
- /** 双击事件 */
43
- onDoubleClick?: (e: React.MouseEvent) => void
44
- /** 鼠标进入 */
45
- onMouseEnter?: (e: React.MouseEvent) => void
46
- /** 鼠标离开 */
47
- onMouseLeave?: (e: React.MouseEvent) => void
48
- }
49
-
50
- export const GaugeChart: React.FC<GaugeChartProps> = ({
51
- ref,
52
- $data,
53
- __dataSource,
54
- min = 0,
55
- max = 100,
56
- unit = '',
57
- showScale = true,
58
- divisions = 10,
59
- showLabels = true,
60
- pointerType = 'needle',
61
- pointerColor = '#00d4ff',
62
- ranges = DEFAULT_RANGES,
63
- glowEffect = true,
64
- rotation = 0,
65
- opacity = 100,
66
- background = 'transparent',
67
- style: externalStyle,
68
- onClick,
69
- onDoubleClick,
70
- onMouseEnter,
71
- onMouseLeave,
72
- }) => {
73
- const chartRef = useRef<HTMLDivElement>(null)
74
- const chartInstance = useRef<echarts.ECharts | null>(null)
75
-
76
- // 解析数据源(单值)
77
- const dataSource = useDataSource($data, __dataSource)
78
- const value = useMemo<number>(() => {
79
- if (dataSource.length > 0 && dataSource[0]?.value !== undefined) {
80
- return Number(dataSource[0].value)
81
- }
82
- return 0
83
- }, [dataSource])
84
-
85
- useEffect(() => {
86
- if (!chartRef.current) {
87
- return
88
- }
89
-
90
- chartInstance.current = echarts.init(chartRef.current)
91
-
92
- // 构建颜色区间
93
- const axisLineColors: [number, string][] = ranges.map(range => [(range.to - min) / (max - min), range.color])
94
-
95
- // 指针宽度根据类型调整
96
- const pointerWidthMap: Record<string, number> = {
97
- needle: 4,
98
- triangle: 8,
99
- rect: 6,
100
- }
101
- const pointerWidth = pointerWidthMap[pointerType] ?? 6
102
-
103
- const option: EChartsOption = {
104
- backgroundColor: 'transparent',
105
- series: [
106
- {
107
- type: 'gauge',
108
- min,
109
- max,
110
- startAngle: 180,
111
- endAngle: 0,
112
- center: ['50%', '80%'],
113
- radius: '72%',
114
- progress: {
115
- show: false,
116
- },
117
- axisLine: {
118
- lineStyle: {
119
- width: 16,
120
- color: axisLineColors,
121
- shadowColor: glowEffect ? 'rgba(0, 212, 255, 0.3)' : 'transparent',
122
- shadowBlur: glowEffect ? 10 : 0,
123
- },
124
- },
125
- axisTick: {
126
- show: showScale,
127
- distance: -20,
128
- length: 4,
129
- lineStyle: {
130
- color: '#8899aa',
131
- width: 1,
132
- },
133
- splitNumber: divisions / 5,
134
- },
135
- splitLine: {
136
- show: showScale,
137
- distance: -24,
138
- length: 8,
139
- lineStyle: {
140
- color: '#8899aa',
141
- width: 2,
142
- },
143
- },
144
- axisLabel: {
145
- show: showLabels,
146
- distance: -32,
147
- color: '#8899aa',
148
- fontSize: 9,
149
- },
150
- pointer: {
151
- show: true,
152
- length: '55%',
153
- width: pointerWidth,
154
- itemStyle: {
155
- color: pointerColor,
156
- shadowColor: glowEffect ? pointerColor : 'transparent',
157
- shadowBlur: glowEffect ? 10 : 0,
158
- },
159
- },
160
- anchor: {
161
- show: true,
162
- size: 10,
163
- itemStyle: {
164
- color: pointerColor,
165
- shadowColor: glowEffect ? pointerColor : 'transparent',
166
- shadowBlur: glowEffect ? 5 : 0,
167
- },
168
- },
169
- title: {
170
- show: true,
171
- offsetCenter: [0, '20%'],
172
- color: '#8899aa',
173
- fontSize: 11,
174
- },
175
- detail: {
176
- valueAnimation: true,
177
- offsetCenter: [0, '40%'],
178
- fontSize: 22,
179
- fontWeight: 'bold',
180
- color: '#fff',
181
- formatter: (val: number) => `${val}${unit}`,
182
- textShadowColor: glowEffect ? 'rgba(0, 212, 255, 0.5)' : 'transparent',
183
- textShadowBlur: glowEffect ? 10 : 0,
184
- },
185
- data: [{ value }],
186
- },
187
- ],
188
- }
189
-
190
- chartInstance.current.setOption(option)
191
-
192
- const resizeObserver = new ResizeObserver(() => {
193
- chartInstance.current?.resize()
194
- })
195
- resizeObserver.observe(chartRef.current)
196
-
197
- return () => {
198
- resizeObserver.disconnect()
199
- chartInstance.current?.dispose()
200
- }
201
- }, [value, min, max, unit, showScale, divisions, showLabels, pointerType, pointerColor, ranges, glowEffect])
202
-
203
- const containerStyle: CSSProperties = {
204
- width: '100%',
205
- height: '100%',
206
- transform: rotation !== 0 ? `rotate(${rotation}deg)` : undefined,
207
- opacity: opacity / 100,
208
- backgroundColor: background,
209
- ...externalStyle,
210
- }
211
-
212
- return (
213
- <div
214
- className={styles.container}
215
- onClick={onClick}
216
- onDoubleClick={onDoubleClick}
217
- onMouseEnter={onMouseEnter}
218
- onMouseLeave={onMouseLeave}
219
- ref={ref}
220
- style={containerStyle}
221
- >
222
- <div className={styles.chart} ref={chartRef} />
223
- </div>
224
- )
225
- }
1
+ /**
2
+ * Gauge Chart Component
3
+ * 仪表盘组件 - 支持数据源绑定和事件交互
4
+ */
5
+
6
+ import { useEffect, useMemo, useRef, type CSSProperties } from 'react'
7
+ import * as echarts from 'echarts/core'
8
+ import { GaugeChart as EChartsGaugeChart } from 'echarts/charts'
9
+ import { TooltipComponent } from 'echarts/components'
10
+ import { CanvasRenderer } from 'echarts/renderers'
11
+ import type { EChartsOption } from 'echarts'
12
+ import { type MaterialComponet, useDataSource } from '@easy-editor/materials-shared'
13
+ import { DEFAULT_RANGES, type GaugeRange } from './constants'
14
+ import styles from './component.module.css'
15
+
16
+ // 按需注册 ECharts 组件
17
+ echarts.use([EChartsGaugeChart, TooltipComponent, CanvasRenderer])
18
+
19
+ export interface GaugeChartProps extends MaterialComponet {
20
+ /** 最小值 */
21
+ min?: number
22
+ /** 最大值 */
23
+ max?: number
24
+ /** 单位 */
25
+ unit?: string
26
+ /** 显示刻度 */
27
+ showScale?: boolean
28
+ /** 刻度数量 */
29
+ divisions?: number
30
+ /** 显示刻度值 */
31
+ showLabels?: boolean
32
+ /** 指针类型 */
33
+ pointerType?: 'needle' | 'triangle' | 'rect'
34
+ /** 指针颜色 */
35
+ pointerColor?: string
36
+ /** 颜色区间 */
37
+ ranges?: GaugeRange[]
38
+ /** 发光效果 */
39
+ glowEffect?: boolean
40
+ /** 点击事件 */
41
+ onClick?: (e: React.MouseEvent) => void
42
+ /** 双击事件 */
43
+ onDoubleClick?: (e: React.MouseEvent) => void
44
+ /** 鼠标进入 */
45
+ onMouseEnter?: (e: React.MouseEvent) => void
46
+ /** 鼠标离开 */
47
+ onMouseLeave?: (e: React.MouseEvent) => void
48
+ }
49
+
50
+ export const GaugeChart: React.FC<GaugeChartProps> = ({
51
+ ref,
52
+ $data,
53
+ __dataSource,
54
+ min = 0,
55
+ max = 100,
56
+ unit = '',
57
+ showScale = true,
58
+ divisions = 10,
59
+ showLabels = true,
60
+ pointerType = 'needle',
61
+ pointerColor = '#00d4ff',
62
+ ranges = DEFAULT_RANGES,
63
+ glowEffect = true,
64
+ rotation = 0,
65
+ opacity = 100,
66
+ background = 'transparent',
67
+ style: externalStyle,
68
+ onClick,
69
+ onDoubleClick,
70
+ onMouseEnter,
71
+ onMouseLeave,
72
+ }) => {
73
+ const chartRef = useRef<HTMLDivElement>(null)
74
+ const chartInstance = useRef<echarts.ECharts | null>(null)
75
+
76
+ // 解析数据源(单值)
77
+ const dataSource = useDataSource($data, __dataSource)
78
+ const value = useMemo<number>(() => {
79
+ if (dataSource.length > 0 && dataSource[0]?.value !== undefined) {
80
+ return Number(dataSource[0].value)
81
+ }
82
+ return 0
83
+ }, [dataSource])
84
+
85
+ useEffect(() => {
86
+ if (!chartRef.current) {
87
+ return
88
+ }
89
+
90
+ chartInstance.current = echarts.init(chartRef.current)
91
+
92
+ // 构建颜色区间
93
+ const axisLineColors: [number, string][] = ranges.map(range => [(range.to - min) / (max - min), range.color])
94
+
95
+ // 指针宽度根据类型调整
96
+ const pointerWidthMap: Record<string, number> = {
97
+ needle: 4,
98
+ triangle: 8,
99
+ rect: 6,
100
+ }
101
+ const pointerWidth = pointerWidthMap[pointerType] ?? 6
102
+
103
+ const option: EChartsOption = {
104
+ backgroundColor: 'transparent',
105
+ series: [
106
+ {
107
+ type: 'gauge',
108
+ min,
109
+ max,
110
+ startAngle: 180,
111
+ endAngle: 0,
112
+ center: ['50%', '80%'],
113
+ radius: '72%',
114
+ progress: {
115
+ show: false,
116
+ },
117
+ axisLine: {
118
+ lineStyle: {
119
+ width: 16,
120
+ color: axisLineColors,
121
+ shadowColor: glowEffect ? 'rgba(0, 212, 255, 0.3)' : 'transparent',
122
+ shadowBlur: glowEffect ? 10 : 0,
123
+ },
124
+ },
125
+ axisTick: {
126
+ show: showScale,
127
+ distance: -20,
128
+ length: 4,
129
+ lineStyle: {
130
+ color: '#8899aa',
131
+ width: 1,
132
+ },
133
+ splitNumber: divisions / 5,
134
+ },
135
+ splitLine: {
136
+ show: showScale,
137
+ distance: -24,
138
+ length: 8,
139
+ lineStyle: {
140
+ color: '#8899aa',
141
+ width: 2,
142
+ },
143
+ },
144
+ axisLabel: {
145
+ show: showLabels,
146
+ distance: -32,
147
+ color: '#8899aa',
148
+ fontSize: 9,
149
+ },
150
+ pointer: {
151
+ show: true,
152
+ length: '55%',
153
+ width: pointerWidth,
154
+ itemStyle: {
155
+ color: pointerColor,
156
+ shadowColor: glowEffect ? pointerColor : 'transparent',
157
+ shadowBlur: glowEffect ? 10 : 0,
158
+ },
159
+ },
160
+ anchor: {
161
+ show: true,
162
+ size: 10,
163
+ itemStyle: {
164
+ color: pointerColor,
165
+ shadowColor: glowEffect ? pointerColor : 'transparent',
166
+ shadowBlur: glowEffect ? 5 : 0,
167
+ },
168
+ },
169
+ title: {
170
+ show: true,
171
+ offsetCenter: [0, '20%'],
172
+ color: '#8899aa',
173
+ fontSize: 11,
174
+ },
175
+ detail: {
176
+ valueAnimation: true,
177
+ offsetCenter: [0, '40%'],
178
+ fontSize: 22,
179
+ fontWeight: 'bold',
180
+ color: '#fff',
181
+ formatter: (val: number) => `${val}${unit}`,
182
+ textShadowColor: glowEffect ? 'rgba(0, 212, 255, 0.5)' : 'transparent',
183
+ textShadowBlur: glowEffect ? 10 : 0,
184
+ },
185
+ data: [{ value }],
186
+ },
187
+ ],
188
+ }
189
+
190
+ chartInstance.current.setOption(option)
191
+
192
+ const resizeObserver = new ResizeObserver(() => {
193
+ chartInstance.current?.resize()
194
+ })
195
+ resizeObserver.observe(chartRef.current)
196
+
197
+ return () => {
198
+ resizeObserver.disconnect()
199
+ chartInstance.current?.dispose()
200
+ }
201
+ }, [value, min, max, unit, showScale, divisions, showLabels, pointerType, pointerColor, ranges, glowEffect])
202
+
203
+ const containerStyle: CSSProperties = {
204
+ width: '100%',
205
+ height: '100%',
206
+ transform: rotation !== 0 ? `rotate(${rotation}deg)` : undefined,
207
+ opacity: opacity / 100,
208
+ backgroundColor: background,
209
+ ...externalStyle,
210
+ }
211
+
212
+ return (
213
+ <div
214
+ className={styles.container}
215
+ onClick={onClick}
216
+ onDoubleClick={onDoubleClick}
217
+ onMouseEnter={onMouseEnter}
218
+ onMouseLeave={onMouseLeave}
219
+ ref={ref}
220
+ style={containerStyle}
221
+ >
222
+ <div className={styles.chart} ref={chartRef} />
223
+ </div>
224
+ )
225
+ }
226
+
227
+ export default GaugeChart
package/src/meta.ts CHANGED
@@ -1,23 +1,23 @@
1
- import type { ComponentMetadata } from '@easy-editor/core'
2
- import { MaterialGroup } from '@easy-editor/materials-shared'
3
- import { COMPONENT_NAME, PACKAGE_NAME } from './constants'
4
- import { configure } from './configure'
5
- import { snippets } from './snippets'
6
- import pkg from '../package.json'
7
-
8
- const meta: ComponentMetadata = {
9
- componentName: COMPONENT_NAME,
10
- title: '仪表盘',
11
- group: MaterialGroup.CHART,
12
- devMode: 'proCode',
13
- npm: {
14
- package: PACKAGE_NAME,
15
- version: pkg.version,
16
- globalName: COMPONENT_NAME,
17
- componentName: COMPONENT_NAME,
18
- },
19
- snippets,
20
- configure,
21
- }
22
-
23
- export { meta }
1
+ import type { ComponentMetadata } from '@easy-editor/core'
2
+ import { MaterialGroup } from '@easy-editor/materials-shared'
3
+ import { COMPONENT_NAME, PACKAGE_NAME } from './constants'
4
+ import { configure } from './configure'
5
+ import { snippets } from './snippets'
6
+ import pkg from '../package.json'
7
+
8
+ export const meta: ComponentMetadata = {
9
+ componentName: COMPONENT_NAME,
10
+ title: '仪表盘',
11
+ group: MaterialGroup.CHART,
12
+ devMode: 'proCode',
13
+ npm: {
14
+ package: PACKAGE_NAME,
15
+ version: pkg.version,
16
+ globalName: COMPONENT_NAME,
17
+ componentName: COMPONENT_NAME,
18
+ },
19
+ snippets,
20
+ configure,
21
+ }
22
+
23
+ export default meta