78 lines
2.3 KiB
Vue
Raw Normal View History

2022-01-15 22:35:32 +08:00
<template>
2023-03-09 18:26:50 +08:00
<v-chart ref="vChartRef" :init-options="initOptions" :theme="themeColor" :option="option" :manual-update="isPreview()" autoresize></v-chart>
2022-01-15 22:35:32 +08:00
</template>
<script setup lang="ts">
import { ref, computed, PropType, watch } from 'vue'
2022-09-06 15:52:41 +08:00
import VChart from 'vue-echarts'
2023-03-09 18:26:50 +08:00
import { useCanvasInitOptions } from '@/hooks/useCanvasInitOptions.hook'
import dataJson from './data.json'
2022-09-06 15:52:41 +08:00
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { RadarChart } from 'echarts/charts'
import { includes } from './config'
2023-02-15 09:20:48 +08:00
import { mergeTheme, setOption } from '@/packages/public/chart'
2022-09-06 15:52:41 +08:00
import { useChartDataFetch } from '@/hooks'
import { CreateComponentType } from '@/packages/index.d'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { isPreview } from '@/utils'
import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
2022-01-15 22:35:32 +08:00
2022-09-06 15:52:41 +08:00
const props = defineProps({
themeSetting: {
type: Object,
required: true
},
themeColor: {
type: Object,
required: true
},
chartConfig: {
type: Object as PropType<CreateComponentType>,
required: true
}
})
2023-03-09 18:26:50 +08:00
const initOptions = useCanvasInitOptions(props.chartConfig.option, props.themeSetting)
2022-09-06 15:52:41 +08:00
use([DatasetComponent, CanvasRenderer, RadarChart, GridComponent, TooltipComponent, LegendComponent])
2022-01-15 22:35:32 +08:00
const vChartRef = ref<typeof VChart>()
const option = computed(() => {
return mergeTheme(props.chartConfig.option, props.themeSetting, includes)
2022-09-06 15:52:41 +08:00
})
2022-01-15 22:35:32 +08:00
const dataSetHandle = (dataset: typeof dataJson) => {
2022-09-08 16:31:26 +08:00
if (dataset.seriesData) {
props.chartConfig.option.series[0].data = dataset.seriesData
2022-09-08 16:31:26 +08:00
// @ts-ignore
2022-09-08 16:07:16 +08:00
props.chartConfig.option.legend.data = dataset.seriesData.map((i: { name: string }) => i.name)
}
if (dataset.radarIndicator) {
props.chartConfig.option.radar.indicator = dataset.radarIndicator
}
if (vChartRef.value && isPreview()) {
2023-02-15 09:20:48 +08:00
setOption(vChartRef.value, props.chartConfig.option)
}
2022-09-07 11:22:13 +08:00
}
watch(
() => props.chartConfig.option.dataset,
newData => {
2022-10-12 21:57:30 +08:00
try {
dataSetHandle(newData)
} catch (error) {
console.log(error)
}
2022-09-07 11:22:13 +08:00
},
{
deep: false
2022-09-07 11:22:13 +08:00
}
)
useChartDataFetch(props.chartConfig, useChartEditStore, (newData: typeof dataJson) => {
2022-09-07 11:22:13 +08:00
dataSetHandle(newData)
})
2022-09-06 15:52:41 +08:00
</script>