84 lines
2.1 KiB
Vue
Raw Normal View History

2022-01-15 21:05:11 +08:00
<template>
2022-10-09 16:04:29 +08:00
<v-chart
ref="vChartRef"
:theme="themeColor"
:option="option.value"
:manual-update="isPreview()"
autoresize>
</v-chart>
2022-01-15 21:05:11 +08:00
</template>
<script setup lang="ts">
import { reactive, watch, PropType } from 'vue'
import VChart from 'vue-echarts'
import { use, graphic } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { LineChart } from 'echarts/charts'
import config, { includes } from './config'
import { mergeTheme } from '@/packages/public/chart'
2022-03-11 10:22:54 +08:00
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
2022-03-20 18:11:26 +08:00
import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index'
import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
2022-03-25 19:58:39 +08:00
import { useChartDataFetch } from '@/hooks'
import { isPreview } from '@/utils'
2022-01-15 21:05:11 +08:00
const props = defineProps({
themeSetting: {
type: Object,
required: true
},
themeColor: {
type: Object,
required: true
},
chartConfig: {
type: Object as PropType<config>,
required: true
}
})
2022-01-15 21:05:11 +08:00
2022-10-09 16:04:29 +08:00
use([
DatasetComponent,
CanvasRenderer,
LineChart,
GridComponent,
TooltipComponent,
LegendComponent,
])
2022-03-11 10:22:54 +08:00
const chartEditStore = useChartEditStore()
2022-01-15 21:05:11 +08:00
2022-03-11 10:22:54 +08:00
const option = reactive({
value: {}
})
2022-03-11 10:22:54 +08:00
// 渐变色处理
2022-10-09 16:04:29 +08:00
watch(() => chartEditStore.getEditCanvasConfig.chartThemeColor, (newColor: keyof typeof chartColorsSearch) => {
if (!isPreview()) {
const themeColor = chartColorsSearch[newColor] || chartColorsSearch[defaultTheme]
props.chartConfig.option.series.forEach((value: any, index: number) => {
value.areaStyle.color = new graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: themeColor[3]
},
{
offset: 1,
color: 'rgba(0,0,0, 0)'
}
])
})
2022-03-20 18:11:26 +08:00
}
2022-10-09 16:04:29 +08:00
option.value = mergeTheme(props.chartConfig.option, props.themeSetting, includes)
props.chartConfig.option = option.value
}, {
immediate: true
})
2022-10-09 16:04:29 +08:00
watch(() => props.chartConfig.option.dataset, () => {
option.value = props.chartConfig.option
})
2022-03-25 19:58:39 +08:00
const { vChartRef } = useChartDataFetch(props.chartConfig, useChartEditStore)
</script>