123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- import type { EChartsOption } from 'echarts';
- import type EchartsUI from './echarts-ui.vue';
- import type { Ref } from 'vue';
- import { computed, nextTick, watch } from 'vue';
- import { preferences, usePreferences } from '@vben-core/preferences';
- import {
- tryOnUnmounted,
- useDebounceFn,
- useTimeoutFn,
- useWindowSize,
- } from '@vueuse/core';
- import echarts from './echarts';
- type EchartsUIType = typeof EchartsUI | undefined;
- type EchartsThemeType = 'dark' | 'light' | null;
- function useEcharts(chartRef: Ref<EchartsUIType>) {
- let chartInstance: echarts.ECharts | null = null;
- let cacheOptions: EChartsOption = {};
- const { isDark } = usePreferences();
- const { height, width } = useWindowSize();
- const resizeHandler: () => void = useDebounceFn(resize, 200);
- const getOptions = computed((): EChartsOption => {
- if (!isDark.value) {
- return cacheOptions;
- }
- return {
- backgroundColor: 'transparent',
- ...cacheOptions,
- };
- });
- const initCharts = (t?: EchartsThemeType) => {
- const el = chartRef?.value?.$el;
- if (!el) {
- return;
- }
- chartInstance = echarts.init(el, t || isDark.value ? 'dark' : null);
- return chartInstance;
- };
- const renderEcharts = (options: EChartsOption, clear = true) => {
- cacheOptions = options;
- return new Promise((resolve) => {
- if (chartRef.value?.offsetHeight === 0) {
- useTimeoutFn(() => {
- renderEcharts(getOptions.value);
- resolve(null);
- }, 30);
- return;
- }
- nextTick(() => {
- useTimeoutFn(() => {
- if (!chartInstance) {
- const instance = initCharts();
- if (!instance) return;
- }
- clear && chartInstance?.clear();
- chartInstance?.setOption(getOptions.value);
- resolve(null);
- }, 30);
- });
- });
- };
- function resize() {
- chartInstance?.resize({
- animation: {
- duration: 300,
- easing: 'quadraticIn',
- },
- });
- }
- watch([width, height], () => {
- resizeHandler?.();
- });
- watch(isDark, () => {
- if (chartInstance) {
- chartInstance.dispose();
- initCharts();
- renderEcharts(cacheOptions);
- resize();
- }
- });
- watch(
- [
- () => preferences.sidebar.collapsed,
- () => preferences.sidebar.extraCollapse,
- () => preferences.sidebar.hidden,
- ],
- () => {
- // 折叠动画200ms
- setTimeout(() => {
- resize();
- }, 200);
- },
- );
- tryOnUnmounted(() => {
- // 销毁实例,释放资源
- chartInstance?.dispose();
- });
- return {
- renderEcharts,
- resize,
- };
- }
- export { useEcharts };
- export type { EchartsUIType };
|