2022-01-14 16:17:14 +08:00
|
|
|
|
<template>
|
2023-04-01 19:18:15 +08:00
|
|
|
|
<v-chart ref="vChartRef" :init-options="initOptions" :theme="themeColor" :option="option" :manual-update="isPreview()"
|
2022-09-11 22:53:00 +08:00
|
|
|
|
:update-options="{
|
|
|
|
|
replaceMerge: replaceMergeArr
|
2023-04-01 19:18:15 +08:00
|
|
|
|
}" autoresize></v-chart>
|
2022-01-14 16:17:14 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2022-09-11 22:53:00 +08:00
|
|
|
|
import { ref, nextTick, computed, watch, PropType } from 'vue'
|
2022-01-25 22:29:44 +08:00
|
|
|
|
import VChart from 'vue-echarts'
|
2023-03-09 18:26:50 +08:00
|
|
|
|
import { useCanvasInitOptions } from '@/hooks/useCanvasInitOptions.hook'
|
2022-02-06 01:04:05 +08:00
|
|
|
|
import { use } from 'echarts/core'
|
2022-01-25 22:29:44 +08:00
|
|
|
|
import { CanvasRenderer } from 'echarts/renderers'
|
|
|
|
|
import { BarChart } from 'echarts/charts'
|
2022-09-11 22:53:00 +08:00
|
|
|
|
import config, { includes, seriesItem } from './config'
|
2022-03-20 16:13:33 +08:00
|
|
|
|
import { mergeTheme } from '@/packages/public/chart'
|
2022-03-25 19:58:39 +08:00
|
|
|
|
import { useChartDataFetch } from '@/hooks'
|
2022-03-23 20:41:50 +08:00
|
|
|
|
import { CreateComponentType } from '@/packages/index.d'
|
2022-03-25 19:58:39 +08:00
|
|
|
|
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
|
2022-03-24 14:19:07 +08:00
|
|
|
|
import { isPreview } from '@/utils'
|
2022-09-11 22:53:00 +08:00
|
|
|
|
import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
|
2022-09-30 16:03:35 +08:00
|
|
|
|
import isObject from 'lodash/isObject'
|
2023-03-30 19:37:18 +08:00
|
|
|
|
import cloneDeep from 'lodash/cloneDeep'
|
2022-01-25 22:29:44 +08:00
|
|
|
|
|
2022-01-24 21:12:18 +08:00
|
|
|
|
const props = defineProps({
|
2022-02-21 19:45:11 +08:00
|
|
|
|
themeSetting: {
|
|
|
|
|
type: Object,
|
|
|
|
|
required: true
|
|
|
|
|
},
|
|
|
|
|
themeColor: {
|
|
|
|
|
type: Object,
|
2022-02-06 01:04:05 +08:00
|
|
|
|
required: true
|
|
|
|
|
},
|
2022-02-24 17:23:20 +08:00
|
|
|
|
chartConfig: {
|
2022-09-11 22:53:00 +08:00
|
|
|
|
type: Object as PropType<config>,
|
2022-01-25 22:29:44 +08:00
|
|
|
|
required: true
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2023-03-09 18:26:50 +08:00
|
|
|
|
const initOptions = useCanvasInitOptions(props.chartConfig.option, props.themeSetting)
|
|
|
|
|
|
2022-09-11 22:53:00 +08:00
|
|
|
|
use([DatasetComponent, CanvasRenderer, BarChart, GridComponent, TooltipComponent, LegendComponent])
|
|
|
|
|
|
|
|
|
|
const replaceMergeArr = ref<string[]>()
|
2022-01-25 22:29:44 +08:00
|
|
|
|
|
|
|
|
|
const option = computed(() => {
|
2022-03-19 23:28:33 +08:00
|
|
|
|
return mergeTheme(props.chartConfig.option, props.themeSetting, includes)
|
2022-01-25 22:29:44 +08:00
|
|
|
|
})
|
2022-03-23 20:41:50 +08:00
|
|
|
|
|
2022-09-11 22:53:00 +08:00
|
|
|
|
// dataset 无法变更条数的补丁
|
|
|
|
|
watch(
|
|
|
|
|
() => props.chartConfig.option.dataset,
|
2022-09-30 16:03:35 +08:00
|
|
|
|
(newData: { dimensions: any }, oldData) => {
|
2022-10-12 21:57:30 +08:00
|
|
|
|
try {
|
2023-04-04 08:46:56 +08:00
|
|
|
|
debugger
|
2022-10-12 21:57:30 +08:00
|
|
|
|
if (!isObject(newData) || !('dimensions' in newData)) return
|
|
|
|
|
if (Array.isArray(newData?.dimensions)) {
|
|
|
|
|
const seriesArr = []
|
2023-04-01 19:28:39 +08:00
|
|
|
|
// 对oldData进行判断,防止传入错误数据之后对旧维度判断产生干扰
|
2023-04-01 19:18:15 +08:00
|
|
|
|
// 此处计算的是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.dimensions.length >= 1 ? newData.dimensions.length : 1;
|
|
|
|
|
const dimensionsGap = newDimensions - oldDimensions;
|
|
|
|
|
if (dimensionsGap < 0) {
|
|
|
|
|
props.chartConfig.option.series.splice(newDimensions - 1)
|
|
|
|
|
} else if (dimensionsGap > 0) {
|
2023-04-04 08:41:26 +08:00
|
|
|
|
if(!oldData || !oldData?.dimensions || !Array.isArray(oldData?.dimensions)) {
|
|
|
|
|
props.chartConfig.option.series=[]
|
|
|
|
|
}
|
2023-04-01 19:18:15 +08:00
|
|
|
|
for (let i = 0; i < dimensionsGap; i++) {
|
|
|
|
|
seriesArr.push(cloneDeep(seriesItem))
|
|
|
|
|
}
|
2023-04-04 08:46:56 +08:00
|
|
|
|
props.chartConfig.option.series.push(...seriesArr)
|
2022-10-12 21:57:30 +08:00
|
|
|
|
}
|
|
|
|
|
replaceMergeArr.value = ['series']
|
|
|
|
|
nextTick(() => {
|
|
|
|
|
replaceMergeArr.value = []
|
|
|
|
|
})
|
2022-09-11 22:53:00 +08:00
|
|
|
|
}
|
2022-10-12 21:57:30 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error)
|
2022-09-11 22:53:00 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
deep: false
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2022-03-25 19:58:39 +08:00
|
|
|
|
const { vChartRef } = useChartDataFetch(props.chartConfig, useChartEditStore)
|
2022-01-14 16:17:14 +08:00
|
|
|
|
</script>
|