提交 5cbfb2a1 作者: vben

fix(charts): fix echarts does not display after refresh #140

上级 f69aaeab
import { withInstall } from '../util'; import { withInstall } from '../util';
import CollapseContainer from './src/collapse/CollapseContainer.vue';
import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent'; import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
export const ScrollContainer = createAsyncComponent(() => import('./src/ScrollContainer.vue')); export const ScrollContainer = createAsyncComponent(() => import('./src/ScrollContainer.vue'));
export const CollapseContainer = createAsyncComponent(
() => import('./src/collapse/CollapseContainer.vue') // export const CollapseContainer = createAsyncComponent(
); // () => import('./src/collapse/CollapseContainer.vue')
// );
export const LazyContainer = createAsyncComponent(() => import('./src/LazyContainer.vue')); export const LazyContainer = createAsyncComponent(() => import('./src/LazyContainer.vue'));
withInstall(ScrollContainer, CollapseContainer, LazyContainer); withInstall(ScrollContainer, CollapseContainer, LazyContainer);
export { CollapseContainer };
export * from './src/types'; export * from './src/types';
...@@ -4,10 +4,14 @@ import { unref, Ref, nextTick } from 'vue'; ...@@ -4,10 +4,14 @@ import { unref, Ref, nextTick } from 'vue';
import ApexCharts from 'apexcharts'; import ApexCharts from 'apexcharts';
interface CallBackFn {
(instance: Nullable<ApexCharts>): void;
}
export function useApexCharts(elRef: Ref<HTMLDivElement>) { export function useApexCharts(elRef: Ref<HTMLDivElement>) {
let chartInstance: Nullable<ApexCharts> = null; let chartInstance: Nullable<ApexCharts> = null;
function setOptions(options: any, callback) { function setOptions(options: any, callback?: CallBackFn) {
nextTick(() => { nextTick(() => {
useTimeoutFn(() => { useTimeoutFn(() => {
const el = unref(elRef); const el = unref(elRef);
...@@ -19,7 +23,6 @@ export function useApexCharts(elRef: Ref<HTMLDivElement>) { ...@@ -19,7 +23,6 @@ export function useApexCharts(elRef: Ref<HTMLDivElement>) {
// setOptions增加callback方法,返回chartInstance,以便于对图表进行再操作,例如调用updateOptions方法更新图表 // setOptions增加callback方法,返回chartInstance,以便于对图表进行再操作,例如调用updateOptions方法更新图表
callback && callback(chartInstance); callback && callback(chartInstance);
}, 30); }, 30);
}); });
} }
...@@ -27,24 +30,17 @@ export function useApexCharts(elRef: Ref<HTMLDivElement>) { ...@@ -27,24 +30,17 @@ export function useApexCharts(elRef: Ref<HTMLDivElement>) {
// 新增调用ApexCharts的updateOptions方法更新图表 // 新增调用ApexCharts的updateOptions方法更新图表
function updateOptions( function updateOptions(
chartInstance: Nullable<ApexCharts>, chartInstance: Nullable<ApexCharts>,
options, options: any,
redraw = false, redraw = false,
animate = true, animate = true,
updateSyncedCharts = true, updateSyncedCharts = true,
overwriteInitialConfig = true, callback: CallBackFn
callback) { ) {
nextTick(() => { nextTick(() => {
useTimeoutFn(() => { useTimeoutFn(() => {
chartInstance && chartInstance.updateOptions(options, redraw, animate, updateSyncedCharts);
chartInstance && chartInstance.updateOptions(
options,
redraw,
animate,
updateSyncedCharts,
overwriteInitialConfig);
callback && callback(chartInstance); callback && callback(chartInstance);
}, 30); }, 30);
}); });
} }
......
...@@ -21,10 +21,10 @@ export function useECharts( ...@@ -21,10 +21,10 @@ export function useECharts(
function init() { function init() {
const el = unref(elRef); const el = unref(elRef);
if (!el || !unref(el)) { if (!el || !unref(el)) {
return; return;
} }
chartInstance = echarts.init(el, theme); chartInstance = echarts.init(el, theme);
const { removeEvent } = useEventListener({ const { removeEvent } = useEventListener({
el: window, el: window,
...@@ -33,7 +33,7 @@ export function useECharts( ...@@ -33,7 +33,7 @@ export function useECharts(
}); });
removeResizeFn = removeEvent; removeResizeFn = removeEvent;
const { widthRef, screenEnum } = useBreakpoint(); const { widthRef, screenEnum } = useBreakpoint();
if (unref(widthRef) <= screenEnum.MD) { if (unref(widthRef) <= screenEnum.MD || el.offsetHeight === 0) {
useTimeoutFn(() => { useTimeoutFn(() => {
resizeFn(); resizeFn();
}, 30); }, 30);
...@@ -41,6 +41,12 @@ export function useECharts( ...@@ -41,6 +41,12 @@ export function useECharts(
} }
function setOptions(options: any, clear = true) { function setOptions(options: any, clear = true) {
if (unref(elRef)?.offsetHeight === 0) {
useTimeoutFn(() => {
setOptions(options);
}, 30);
return;
}
nextTick(() => { nextTick(() => {
useTimeoutFn(() => { useTimeoutFn(() => {
if (!chartInstance) { if (!chartInstance) {
...@@ -48,16 +54,15 @@ export function useECharts( ...@@ -48,16 +54,15 @@ export function useECharts(
if (!chartInstance) return; if (!chartInstance) return;
} }
clear && chartInstance.clear(); clear && chartInstance?.clear();
chartInstance && chartInstance.setOption(options); chartInstance?.setOption(options);
}, 30); }, 30);
}); });
} }
function resize() { function resize() {
if (!chartInstance) return; chartInstance?.resize();
chartInstance.resize();
} }
tryOnUnmounted(() => { tryOnUnmounted(() => {
......
...@@ -110,7 +110,6 @@ ...@@ -110,7 +110,6 @@
getCanDrag, getCanDrag,
getCloseMixSidebarOnChange, getCloseMixSidebarOnChange,
getMenuTheme, getMenuTheme,
getMixSidebarTheme,
} = useMenuSetting(); } = useMenuSetting();
const { title } = useGlobSetting(); const { title } = useGlobSetting();
...@@ -193,7 +192,6 @@ ...@@ -193,7 +192,6 @@
title, title,
openMenu, openMenu,
getMenuTheme, getMenuTheme,
getMixSidebarTheme,
}; };
}, },
}); });
...@@ -290,9 +288,12 @@ ...@@ -290,9 +288,12 @@
} }
} }
> .scrollbar {
height: calc(100% - @header-height) !important;
}
&-module { &-module {
position: relative; position: relative;
height: calc(100% - @header-height) !important;
padding-top: 1px; padding-top: 1px;
&__item { &__item {
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
{ value: 234, name: '其他', itemStyle: { color: '#7dd9b9' } }, { value: 234, name: '其他', itemStyle: { color: '#7dd9b9' } },
]; ];
export default defineComponent({ export default defineComponent({
name: 'AnalysisLine',
props: basicProps, props: basicProps,
setup() { setup() {
const chartRef = ref<HTMLDivElement | null>(null); const chartRef = ref<HTMLDivElement | null>(null);
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
import { basicProps } from './props'; import { basicProps } from './props';
export default defineComponent({ export default defineComponent({
name: 'AnalysisLine',
props: basicProps, props: basicProps,
setup() { setup() {
const chartRef = ref<HTMLDivElement | null>(null); const chartRef = ref<HTMLDivElement | null>(null);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论