From d9ac7e4de065cf686a52d7128307f66e5e0b9163 Mon Sep 17 00:00:00 2001 From: Soybean Date: Tue, 31 May 2022 00:26:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor(projects):=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ISSUES CLOSED: \ --- .../useEcharts.ts => composables/echarts.ts} | 33 ++-- src/composables/index.ts | 1 + src/hooks/business/index.ts | 5 +- src/typings/route.d.ts | 1 + .../analysis/components/TopChart/index.vue | 13 +- src/views/plugin/charts/echarts/index.vue | 186 ++++++++++++++++-- 6 files changed, 201 insertions(+), 38 deletions(-) rename src/{hooks/business/useEcharts.ts => composables/echarts.ts} (83%) diff --git a/src/hooks/business/useEcharts.ts b/src/composables/echarts.ts similarity index 83% rename from src/hooks/business/useEcharts.ts rename to src/composables/echarts.ts index f444b94e..b8d31aa6 100644 --- a/src/hooks/business/useEcharts.ts +++ b/src/composables/echarts.ts @@ -1,4 +1,4 @@ -import { ref, watch, nextTick, onUnmounted, computed } from 'vue'; +import { ref, watch, nextTick, onUnmounted } from 'vue'; import type { Ref, ComputedRef } from 'vue'; import * as echarts from 'echarts/core'; import { BarChart, LineChart, PieChart, ScatterChart } from 'echarts/charts'; @@ -23,6 +23,7 @@ import type { import { LabelLayout, UniversalTransition } from 'echarts/features'; import { CanvasRenderer } from 'echarts/renderers'; import { useElementSize } from '@vueuse/core'; +import { useThemeStore } from '@/store'; export type ECOption = echarts.ComposeOption< | BarSeriesOption @@ -57,20 +58,22 @@ echarts.use([ /** * Echarts hooks函数 * @param options - 图表配置 - * @param darkMode - 暗黑模式 * @param renderFun - 图表渲染函数(例如:图表监听函数) * @description 按需引入图表组件,没注册的组件需要先引入 */ -export default function useEcharts( +export function useEcharts( options: Ref | ComputedRef, - darkMode?: ComputedRef, renderFun?: (chartInstance: echarts.ECharts) => void ) { - let chart: echarts.ECharts | null = null; + const theme = useThemeStore(); + const domRef = ref(null); + const initialSize = { width: 0, height: 0 }; const { width, height } = useElementSize(domRef, initialSize); + let chart: echarts.ECharts | null = null; + function canRender() { return initialSize.width > 0 && initialSize.height > 0; } @@ -87,9 +90,9 @@ export default function useEcharts( async function render() { if (domRef.value) { - const theme = darkMode?.value ? 'dark' : 'light'; + const chartTheme = theme.darkMode ? 'dark' : 'light'; await nextTick(); - chart = echarts.init(domRef.value, theme); + chart = echarts.init(domRef.value, chartTheme); if (renderFun) { renderFun(chart); } @@ -110,7 +113,7 @@ export default function useEcharts( render(); } - watch([width, height], ([newWidth, newHeight]) => { + const stopSizeWatch = watch([width, height], ([newWidth, newHeight]) => { initialSize.width = newWidth; initialSize.height = newHeight; if (canRender()) { @@ -122,16 +125,22 @@ export default function useEcharts( } }); - watch(options, newValue => { + const stopOptionWatch = watch(options, newValue => { update(newValue); }); - watch(darkMode || computed(() => false), () => { - updateTheme(); - }); + const stopDarkModeWatch = watch( + () => theme.darkMode, + () => { + updateTheme(); + } + ); onUnmounted(() => { destroy(); + stopSizeWatch(); + stopOptionWatch(); + stopDarkModeWatch(); }); return { diff --git a/src/composables/index.ts b/src/composables/index.ts index 2d2a11c3..c5107972 100644 --- a/src/composables/index.ts +++ b/src/composables/index.ts @@ -1,3 +1,4 @@ export * from './system'; export * from './router'; export * from './layout'; +export * from './echarts'; diff --git a/src/hooks/business/index.ts b/src/hooks/business/index.ts index 3d63e91b..cbc26398 100644 --- a/src/hooks/business/index.ts +++ b/src/hooks/business/index.ts @@ -1,8 +1,5 @@ -import useEcharts from './useEcharts'; import useCountDown from './useCountDown'; import useSmsCode from './useSmsCode'; import useImageVerify from './useImageVerify'; -export { useEcharts, useCountDown, useSmsCode, useImageVerify }; - -export * from './useEcharts'; +export { useCountDown, useSmsCode, useImageVerify }; diff --git a/src/typings/route.d.ts b/src/typings/route.d.ts index 3029ef86..5f4e3b56 100644 --- a/src/typings/route.d.ts +++ b/src/typings/route.d.ts @@ -40,6 +40,7 @@ declare namespace AuthRoute { | 'plugin_charts_echarts' | 'plugin_charts_d3' | 'plugin_charts_antv' + | 'plugin_charts_chartjs' | 'auth-demo' | 'auth-demo_permission' | 'auth-demo_super' diff --git a/src/views/dashboard/analysis/components/TopChart/index.vue b/src/views/dashboard/analysis/components/TopChart/index.vue index 3d3d2919..efb630f8 100644 --- a/src/views/dashboard/analysis/components/TopChart/index.vue +++ b/src/views/dashboard/analysis/components/TopChart/index.vue @@ -31,13 +31,8 @@ diff --git a/src/views/plugin/charts/echarts/index.vue b/src/views/plugin/charts/echarts/index.vue index cbc921ce..f7e908f4 100644 --- a/src/views/plugin/charts/echarts/index.vue +++ b/src/views/plugin/charts/echarts/index.vue @@ -16,13 +16,8 @@