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

This commit is contained in:
vben
2020-12-23 00:14:51 +08:00
parent f69aaeab5e
commit 5cbfb2a1f9
6 changed files with 37 additions and 35 deletions

View File

@@ -4,10 +4,14 @@ import { unref, Ref, nextTick } from 'vue';
import ApexCharts from 'apexcharts';
interface CallBackFn {
(instance: Nullable<ApexCharts>): void;
}
export function useApexCharts(elRef: Ref<HTMLDivElement>) {
let chartInstance: Nullable<ApexCharts> = null;
function setOptions(options: any, callback) {
function setOptions(options: any, callback?: CallBackFn) {
nextTick(() => {
useTimeoutFn(() => {
const el = unref(elRef);
@@ -16,37 +20,29 @@ export function useApexCharts(elRef: Ref<HTMLDivElement>) {
chartInstance = new ApexCharts(el, options);
chartInstance && chartInstance.render();
// setOptions增加callback方法返回chartInstance以便于对图表进行再操作例如调用updateOptions方法更新图表
callback && callback(chartInstance);
}, 30);
});
}
// 新增调用ApexCharts的updateOptions方法更新图表
function updateOptions(
chartInstance: Nullable<ApexCharts>,
options,
redraw = false,
animate = true,
updateSyncedCharts = true,
overwriteInitialConfig = true,
callback) {
chartInstance: Nullable<ApexCharts>,
options: any,
redraw = false,
animate = true,
updateSyncedCharts = true,
callback: CallBackFn
) {
nextTick(() => {
useTimeoutFn(() => {
chartInstance && chartInstance.updateOptions(options, redraw, animate, updateSyncedCharts);
chartInstance && chartInstance.updateOptions(
options,
redraw,
animate,
updateSyncedCharts,
overwriteInitialConfig);
callback && callback(chartInstance);
}, 30);
});
});
}
tryOnUnmounted(() => {