index.vue
4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<template>
<v-chart
ref="vChartRef"
:init-options="initOptions"
:theme="themeColor"
:option="option"
:manual-update="isPreview()"
:update-options="{
replaceMerge: replaceMergeArr
}"
autoresize
></v-chart>
</template>
<script setup lang="ts">
import { ref, computed, watch, PropType, nextTick } from 'vue'
import VChart from 'vue-echarts'
import { useCanvasInitOptions } from '@/hooks/useCanvasInitOptions.hook'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
//引入柱状图 折线图
import { BarChart, LineChart } from 'echarts/charts'
import config, { includes, barSeriesItem, lineSeriesItem } from './config'
import { mergeTheme } from '@/packages/public/chart'
import { useChartDataFetch } from '@/hooks'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { isPreview } from '@/utils'
import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
import { SocketReceiveMessageType } from '@/store/external/modules/socketStore.d'
import isObject from 'lodash/isObject'
import { useAssembleDataHooks } from '@/hooks/external/useAssembleData.hook'
import { useEchartsMapLegend } from '@/hooks/external/useEchartLegendMapChinese.hook'
import cloneDeep from 'lodash/cloneDeep'
const props = defineProps({
themeSetting: {
type: Object,
required: true
},
themeColor: {
type: Object,
required: true
},
chartConfig: {
type: Object as PropType<config>,
required: true
}
})
const initOptions = useCanvasInitOptions(props.chartConfig.option, props.themeSetting)
use([DatasetComponent, CanvasRenderer, BarChart, LineChart, GridComponent, TooltipComponent, LegendComponent])
const chartEditStore = useChartEditStore()
const replaceMergeArr = ref<string[]>()
const option = computed(() => {
return mergeTheme(props.chartConfig.option, props.themeSetting, includes)
})
// dataset 无法变更条数的补丁
watch(
() => props.chartConfig.option.dataset,
(newData: any, oldData) => {
try {
if (!isObject(newData) || !('dimensions' in newData)) return
if (Array.isArray((newData as any)?.dimensions)) {
const seriesArr: typeof barSeriesItem[] = []
// 对oldData进行判断,防止传入错误数据之后对旧维度判断产生干扰
// 此处计算的是dimensions的Y轴维度,若是dimensions.length为0或1,则默认为1,排除X轴维度干扰
const oldDimensions =
Array.isArray(oldData?.dimensions) && oldData.dimensions.length >= 1 ? oldData.dimensions.length : 1
const newDimensions = (newData as any).dimensions.length >= 1 ? (newData as any).dimensions.length : 1
const dimensionsGap = newDimensions - oldDimensions
if (dimensionsGap < 0) {
props.chartConfig.option.series.splice(newDimensions - 1)
} else if (dimensionsGap > 0) {
if (!oldData || !oldData?.dimensions || !Array.isArray(oldData?.dimensions) || !oldData?.dimensions.length) {
props.chartConfig.option.series = []
}
for (let i = 0; i < dimensionsGap; i++) {
seriesArr.push(cloneDeep(barSeriesItem))
}
props.chartConfig.option.series.push(...seriesArr)
}
useEchartsMapLegend(props.chartConfig, props.chartConfig.option.series)
replaceMergeArr.value = ['series']
nextTick(() => {
replaceMergeArr.value = []
})
}
} catch (error) {
console.log(error)
}
},
{
deep: false
}
)
//fix 修复v-chart图表绑定联动组件视图不更新问题
const updateVChart =async (newData:SocketReceiveMessageType) => {
//区分是普通请求还是ws请求
if (!isObject(newData) || !('dimensions' in newData)) {
const { data } = newData
const { keys, record } = useAssembleDataHooks(data)
vChartRef.value?.setOption({
dataset: {
dimensions: ['ts', ...keys],
source: [record]
}
})
} else {
//异步更新,同步更新会造成联动组件控制,图表不及时更新
await nextTick().then(()=>{
vChartRef.value?.setOption(
{
...option.value,
dataset: newData
},
{
notMerge: true
}
)
})
}
}
const {vChartRef} = useChartDataFetch(props.chartConfig, useChartEditStore, (newData) => {
//联动支持分组
/**
* 修复多个分组,然后下拉框联动,会影响另一个组件
*/
chartEditStore.getComponentList.forEach(targetItem => {
if (targetItem.isGroup) {
targetItem.groupList?.forEach(groupItem => {
if (groupItem.id === props.chartConfig.id) {
groupItem.option.dataset = newData
}
})
} else {
if (targetItem.id === props.chartConfig.id) {
targetItem.option.dataset = newData
}
}
})
//
updateVChart(newData)
})
</script>