94 lines
2.2 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">
2022-10-09 16:04:29 +08:00
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";
import { useChartEditStore } from "@/store/modules/chartEditStore/chartEditStore";
import { chartColorsSearch, defaultTheme } from "@/settings/chartThemes/index";
import {
DatasetComponent,
GridComponent,
TooltipComponent,
LegendComponent,
} from "echarts/components";
import { useChartDataFetch } from "@/hooks";
import { isPreview } from "@/utils";
2022-01-15 21:05:11 +08:00
const props = defineProps({
themeSetting: {
type: Object,
2022-10-09 16:04:29 +08:00
required: true,
},
themeColor: {
type: Object,
2022-10-09 16:04:29 +08:00
required: true,
},
chartConfig: {
type: Object as PropType<config>,
2022-10-09 16:04:29 +08:00
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,
]);
const chartEditStore = useChartEditStore();
2022-01-15 21:05:11 +08:00
2022-03-11 10:22:54 +08:00
const option = reactive({
2022-10-09 16:04:29 +08:00
value: {},
});
2022-03-11 10:22:54 +08:00
// 渐变色处理
watch(
() => chartEditStore.getEditCanvasConfig.chartThemeColor,
(newColor: keyof typeof chartColorsSearch) => {
if (!isPreview()) {
2022-10-09 16:04:29 +08:00
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,
2022-10-09 16:04:29 +08:00
color: themeColor[3 + index],
},
{
offset: 1,
2022-10-09 16:04:29 +08:00
color: "rgba(0,0,0, 0)",
},
]);
});
}
2022-10-09 16:04:29 +08:00
option.value = mergeTheme(props.chartConfig.option, props.themeSetting, includes);
props.chartConfig.option = option.value;
},
{
2022-10-09 16:04:29 +08:00
immediate: true,
2022-03-20 18:11:26 +08:00
}
2022-10-09 16:04:29 +08:00
);
2022-03-20 18:11:26 +08:00
watch(
() => props.chartConfig.option.dataset,
() => {
2022-10-09 16:04:29 +08:00
option.value = props.chartConfig.option;
}
2022-10-09 16:04:29 +08:00
);
const { vChartRef } = useChartDataFetch(props.chartConfig, useChartEditStore);
</script>