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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "st-comp",
3
3
  "public": true,
4
- "version": "0.0.21",
4
+ "version": "0.0.23",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",
@@ -1,9 +1,138 @@
1
- <script setup lang="ts"></script>
2
-
3
1
  <template>
4
-
2
+ <div
3
+ ref="chartRef"
4
+ class="chart"
5
+ ></div>
5
6
  </template>
6
7
 
7
- <style lang="scss" scoped>
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
+ dataZoom: null,
16
+ grid: {
17
+ left: '60px',
18
+ bottom: '20px',
19
+ right: '0',
20
+ top: 0
21
+ }
22
+ }
23
+ let chart: EChartsType
24
+ let resizeRo: any
25
+
26
+ const props = defineProps({
27
+ data: {
28
+ type: Object,
29
+ default: () => ({}),
30
+ }, // 数据
31
+ config: {
32
+ type: Object,
33
+ default: () => ({})
34
+ }
35
+ })
36
+
37
+ const chartRef = ref<HTMLElement>()
38
+
39
+ const mergeConfig = computed(() => ({ ...defaultConfig, ...props.config }))
8
40
 
41
+ watch(
42
+ () => [props.data],
43
+ () => {
44
+ draw()
45
+ },
46
+ { deep: true }
47
+ )
48
+
49
+ onMounted(() => {
50
+ chart = echarts.init(chartRef.value)
51
+ draw()
52
+ // 绑定resize事件
53
+ let isFirst: boolean | null = true
54
+ resizeRo = new ResizeObserver(() => {
55
+ if (isFirst) {
56
+ isFirst = null
57
+ return
58
+ }
59
+ chart.resize()
60
+ })
61
+ resizeRo.observe(chartRef.value)
62
+ })
63
+
64
+ onUnmounted(() => {
65
+ chart.dispose()
66
+ resizeRo.disconnect()
67
+ resizeRo = null
68
+ })
69
+
70
+ const draw = () => {
71
+ chart.setOption(
72
+ {
73
+ tooltip: {
74
+ confine: true,
75
+ formatter: mergeConfig.value.tooltipFormatter
76
+ },
77
+ grid: mergeConfig.value.grid,
78
+ xAxis: {
79
+ type: "category",
80
+ data: props.data.date,
81
+ },
82
+ yAxis: {
83
+ type: "category",
84
+ inverse: true,
85
+ data: props.data.data.map((i: any) => i.name),
86
+ },
87
+ visualMap: {
88
+ type: 'piecewise', // 类型: 分段式[piecewise] | 连续形[continuous]
89
+ show: false,
90
+ inRange: [],
91
+ },
92
+ dataZoom: mergeConfig.value.dataZoom,
93
+ series: [
94
+ {
95
+ name: "热力图",
96
+ type: "heatmap",
97
+ data: props.data.data.reduce((res: any[], item: any, index: number) => {
98
+ return [
99
+ ...res,
100
+ ...item.data.reduce((res: any[], i: any, iIndex: number) => {
101
+ if (i === null) {
102
+ return res
103
+ }
104
+ return [
105
+ ...res,
106
+ {
107
+ value: [iIndex, index, i.value],
108
+ itemStyle: {
109
+ color: i.color
110
+ },
111
+ }
112
+ ]
113
+ }, [])
114
+ ]
115
+ }, []),
116
+ label: {
117
+ show: false, // 是否展示方块格上的数字
118
+ },
119
+ },
120
+ ],
121
+ }
122
+ )
123
+ }
124
+
125
+ defineExpose({
126
+ reset: () => {
127
+ draw()
128
+ }, // 重置
129
+ })
130
+ </script>
131
+
132
+ <style lang="scss" scoped>
133
+ .chart {
134
+ width: 100%;
135
+ height: 100%;
136
+ overflow: hidden;
137
+ }
9
138
  </style>