vue-vben-admin/src/hooks/web/useApexCharts.ts

33 lines
749 B
TypeScript
Raw Normal View History

import { useTimeout } from '/@/hooks/core/useTimeout';
import { tryOnUnmounted } from '/@/utils/helper/vueHelper';
import { unref, Ref, nextTick } from 'vue';
import ApexCharts from 'apexcharts';
export function useApexCharts(elRef: Ref<HTMLDivElement>) {
let chartInstance: Nullable<ApexCharts> = null;
function setOptions(options: any) {
2020-10-11 22:59:44 +08:00
nextTick(() => {
useTimeout(() => {
const el = unref(elRef);
if (!el || !unref(el)) return;
chartInstance = new ApexCharts(el, options);
chartInstance && chartInstance.render();
}, 30);
});
}
tryOnUnmounted(() => {
if (!chartInstance) return;
2020-10-11 16:04:54 +08:00
chartInstance.destroy();
chartInstance = null;
});
return {
setOptions,
};
}