Commit 5cbfb2a1f9ace8b991ac67c5b7d37b64eb2dbac8

Authored by vben
1 parent f69aaeab

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

src/components/Container/index.ts
1 1 import { withInstall } from '../util';
2   -
  2 +import CollapseContainer from './src/collapse/CollapseContainer.vue';
3 3 import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
4 4 export const ScrollContainer = createAsyncComponent(() => import('./src/ScrollContainer.vue'));
5   -export const CollapseContainer = createAsyncComponent(
6   - () => import('./src/collapse/CollapseContainer.vue')
7   -);
  5 +
  6 +// export const CollapseContainer = createAsyncComponent(
  7 +// () => import('./src/collapse/CollapseContainer.vue')
  8 +// );
8 9 export const LazyContainer = createAsyncComponent(() => import('./src/LazyContainer.vue'));
9 10  
10 11 withInstall(ScrollContainer, CollapseContainer, LazyContainer);
11 12  
  13 +export { CollapseContainer };
12 14 export * from './src/types';
... ...
src/hooks/web/useApexCharts.ts
... ... @@ -4,10 +4,14 @@ import { unref, Ref, nextTick } from 'vue';
4 4  
5 5 import ApexCharts from 'apexcharts';
6 6  
  7 +interface CallBackFn {
  8 + (instance: Nullable<ApexCharts>): void;
  9 +}
  10 +
7 11 export function useApexCharts(elRef: Ref<HTMLDivElement>) {
8 12 let chartInstance: Nullable<ApexCharts> = null;
9 13  
10   - function setOptions(options: any, callback) {
  14 + function setOptions(options: any, callback?: CallBackFn) {
11 15 nextTick(() => {
12 16 useTimeoutFn(() => {
13 17 const el = unref(elRef);
... ... @@ -16,37 +20,29 @@ export function useApexCharts(elRef: Ref&lt;HTMLDivElement&gt;) {
16 20 chartInstance = new ApexCharts(el, options);
17 21  
18 22 chartInstance && chartInstance.render();
19   -
  23 +
20 24 // setOptions增加callback方法,返回chartInstance,以便于对图表进行再操作,例如调用updateOptions方法更新图表
21 25 callback && callback(chartInstance);
22   -
23 26 }, 30);
24 27 });
25 28 }
26   -
  29 +
27 30 // 新增调用ApexCharts的updateOptions方法更新图表
28 31 function updateOptions(
29   - chartInstance: Nullable<ApexCharts>,
30   - options,
31   - redraw = false,
32   - animate = true,
33   - updateSyncedCharts = true,
34   - overwriteInitialConfig = true,
35   - callback) {
  32 + chartInstance: Nullable<ApexCharts>,
  33 + options: any,
  34 + redraw = false,
  35 + animate = true,
  36 + updateSyncedCharts = true,
  37 + callback: CallBackFn
  38 + ) {
36 39 nextTick(() => {
37 40 useTimeoutFn(() => {
  41 + chartInstance && chartInstance.updateOptions(options, redraw, animate, updateSyncedCharts);
38 42  
39   - chartInstance && chartInstance.updateOptions(
40   - options,
41   - redraw,
42   - animate,
43   - updateSyncedCharts,
44   - overwriteInitialConfig);
45   -
46 43 callback && callback(chartInstance);
47   -
48 44 }, 30);
49   - });
  45 + });
50 46 }
51 47  
52 48 tryOnUnmounted(() => {
... ...
src/hooks/web/useECharts.ts
... ... @@ -21,10 +21,10 @@ export function useECharts(
21 21  
22 22 function init() {
23 23 const el = unref(elRef);
24   -
25 24 if (!el || !unref(el)) {
26 25 return;
27 26 }
  27 +
28 28 chartInstance = echarts.init(el, theme);
29 29 const { removeEvent } = useEventListener({
30 30 el: window,
... ... @@ -33,7 +33,7 @@ export function useECharts(
33 33 });
34 34 removeResizeFn = removeEvent;
35 35 const { widthRef, screenEnum } = useBreakpoint();
36   - if (unref(widthRef) <= screenEnum.MD) {
  36 + if (unref(widthRef) <= screenEnum.MD || el.offsetHeight === 0) {
37 37 useTimeoutFn(() => {
38 38 resizeFn();
39 39 }, 30);
... ... @@ -41,6 +41,12 @@ export function useECharts(
41 41 }
42 42  
43 43 function setOptions(options: any, clear = true) {
  44 + if (unref(elRef)?.offsetHeight === 0) {
  45 + useTimeoutFn(() => {
  46 + setOptions(options);
  47 + }, 30);
  48 + return;
  49 + }
44 50 nextTick(() => {
45 51 useTimeoutFn(() => {
46 52 if (!chartInstance) {
... ... @@ -48,16 +54,15 @@ export function useECharts(
48 54  
49 55 if (!chartInstance) return;
50 56 }
51   - clear && chartInstance.clear();
  57 + clear && chartInstance?.clear();
52 58  
53   - chartInstance && chartInstance.setOption(options);
  59 + chartInstance?.setOption(options);
54 60 }, 30);
55 61 });
56 62 }
57 63  
58 64 function resize() {
59   - if (!chartInstance) return;
60   - chartInstance.resize();
  65 + chartInstance?.resize();
61 66 }
62 67  
63 68 tryOnUnmounted(() => {
... ...
src/layouts/default/sider/MixSider.vue
... ... @@ -110,7 +110,6 @@
110 110 getCanDrag,
111 111 getCloseMixSidebarOnChange,
112 112 getMenuTheme,
113   - getMixSidebarTheme,
114 113 } = useMenuSetting();
115 114 const { title } = useGlobSetting();
116 115  
... ... @@ -193,7 +192,6 @@
193 192 title,
194 193 openMenu,
195 194 getMenuTheme,
196   - getMixSidebarTheme,
197 195 };
198 196 },
199 197 });
... ... @@ -290,9 +288,12 @@
290 288 }
291 289 }
292 290  
  291 + > .scrollbar {
  292 + height: calc(100% - @header-height) !important;
  293 + }
  294 +
293 295 &-module {
294 296 position: relative;
295   - height: calc(100% - @header-height) !important;
296 297 padding-top: 1px;
297 298  
298 299 &__item {
... ...
src/views/dashboard/analysis/components/AnalysisPie.vue
... ... @@ -15,7 +15,6 @@
15 15 { value: 234, name: '其他', itemStyle: { color: '#7dd9b9' } },
16 16 ];
17 17 export default defineComponent({
18   - name: 'AnalysisLine',
19 18 props: basicProps,
20 19 setup() {
21 20 const chartRef = ref<HTMLDivElement | null>(null);
... ...
src/views/dashboard/analysis/components/TrendLine.vue
... ... @@ -8,7 +8,6 @@
8 8  
9 9 import { basicProps } from './props';
10 10 export default defineComponent({
11   - name: 'AnalysisLine',
12 11 props: basicProps,
13 12 setup() {
14 13 const chartRef = ref<HTMLDivElement | null>(null);
... ...