st-comp 0.0.21 → 0.0.23

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,182 @@
1
+ <template>
2
+ <div
3
+ ref="chartRef"
4
+ class="chart"
5
+ ></div>
6
+ </template>
7
+
8
+ <script setup lang="ts">
9
+ import { ref, onMounted, onUnmounted, computed, watch } from "vue"
10
+ import * as echarts from 'echarts'
11
+ import type { EChartsType } from 'echarts'
12
+
13
+ const defaultConfig = {
14
+ tooltipFormatter: null
15
+ }
16
+ let chart: EChartsType
17
+ let resizeRo: any
18
+
19
+ const props = defineProps({
20
+ data: {
21
+ type: Array,
22
+ default: () => ([]),
23
+ }, // 数据
24
+ config: {
25
+ type: Object,
26
+ default: () => ({})
27
+ }
28
+ })
29
+
30
+ const chartRef = ref<HTMLElement>()
31
+
32
+ const mergeConfig = computed(() => ({ ...defaultConfig, ...props.config }))
33
+
34
+ watch(
35
+ () => [props.data],
36
+ () => {
37
+ draw()
38
+ },
39
+ { deep: true }
40
+ )
41
+
42
+ onMounted(() => {
43
+ chart = echarts.init(chartRef.value)
44
+ draw()
45
+ // 绑定resize事件
46
+ let isFirst: boolean | null = true
47
+ resizeRo = new ResizeObserver(() => {
48
+ if (isFirst) {
49
+ isFirst = null
50
+ return
51
+ }
52
+ chart.resize()
53
+ })
54
+ resizeRo.observe(chartRef.value)
55
+ })
56
+
57
+ onUnmounted(() => {
58
+ chart.dispose()
59
+ resizeRo.disconnect()
60
+ resizeRo = null
61
+ })
62
+
63
+ const draw = () => {
64
+ chart.setOption(
65
+ {
66
+ tooltip: {
67
+ confine: true,
68
+ formatter: mergeConfig.value.tooltipFormatter
69
+ },
70
+ series: [
71
+ {
72
+ type: 'treemap',
73
+ width: '100%',
74
+ height: '100%',
75
+ visibleMin: 300,
76
+ breadcrumb: {
77
+ show: false,
78
+ },
79
+ label: {
80
+ show: true,
81
+ formatter: '{b}'
82
+ },
83
+ upperLabel: {
84
+ show: true,
85
+ height: 30
86
+ },
87
+ itemStyle: {
88
+ borderColor: '#fff'
89
+ },
90
+ levels: [
91
+ {
92
+ itemStyle: {
93
+ borderColor: '#777',
94
+ borderWidth: 0,
95
+ gapWidth: 0
96
+ },
97
+ upperLabel: {
98
+ show: false
99
+ }
100
+ },
101
+ {
102
+ itemStyle: {
103
+ borderColor: '#000',
104
+ borderWidth: 2,
105
+ gapWidth: 1
106
+ },
107
+ upperLabel: {
108
+ padding: [8, 0, 0, 0],
109
+ formatter: (params: any) => {
110
+ return `{title|${params.data.name} ${params.data.labelValue}} {${params.data.percent >= 0 ? 'redPercent' : 'greenPercent'}|${params.data.percent}%}`
111
+ },
112
+ rich: {
113
+ title: {
114
+ color: '#fff',
115
+ fontSize: 14,
116
+ },
117
+ redPercent: {
118
+ color: 'red',
119
+ fontSize: 14,
120
+ },
121
+ greenPercent: {
122
+ color: 'green',
123
+ fontSize: 14,
124
+ }
125
+ }
126
+ },
127
+ },
128
+ {
129
+ colorSaturation: [0.35, 0.5],
130
+ itemStyle: {
131
+ borderWidth: 0,
132
+ gapWidth: 1,
133
+ borderColorSaturation: 0.6
134
+ },
135
+ label: {
136
+ align: 'center',
137
+ verticalAlign: 'middle',
138
+ fontSize: 20,
139
+ formatter: (params: any) => {
140
+ console.log(params)
141
+ return `${params.data.name}
142
+ ${params.data.percent > 0 ? '+' : ''}${params.data.percent}%`
143
+ },
144
+ },
145
+ }
146
+ ],
147
+ data: props.data.map((item: any) => {
148
+ return {
149
+ ...item,
150
+ itemStyle: {
151
+ color: item.color,
152
+ },
153
+ children: item.children.map((child: any) => {
154
+ return {
155
+ ...child,
156
+ itemStyle: {
157
+ color: child.color,
158
+ }
159
+ }
160
+ })
161
+ }
162
+ })
163
+ }
164
+ ]
165
+ }
166
+ )
167
+ }
168
+
169
+ defineExpose({
170
+ reset: () => {
171
+ draw()
172
+ }, // 重置
173
+ })
174
+ </script>
175
+
176
+ <style lang="scss" scoped>
177
+ .chart {
178
+ width: 100%;
179
+ height: 100%;
180
+ overflow: hidden;
181
+ }
182
+ </style>