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