Commit 5cbfb2a1f9ace8b991ac67c5b7d37b64eb2dbac8

Authored by vben
1 parent f69aaeab

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

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