useApexCharts.ts 753 B

1234567891011121314151617181920212223242526272829303132
  1. import { useTimeoutFn } from '/@/hooks/core/useTimeout';
  2. import { tryOnUnmounted } from '/@/utils/helper/vueHelper';
  3. import { unref, Ref, nextTick } from 'vue';
  4. import ApexCharts from 'apexcharts';
  5. export function useApexCharts(elRef: Ref<HTMLDivElement>) {
  6. let chartInstance: Nullable<ApexCharts> = null;
  7. function setOptions(options: any) {
  8. nextTick(() => {
  9. useTimeoutFn(() => {
  10. const el = unref(elRef);
  11. if (!el || !unref(el)) return;
  12. chartInstance = new ApexCharts(el, options);
  13. chartInstance && chartInstance.render();
  14. }, 30);
  15. });
  16. }
  17. tryOnUnmounted(() => {
  18. if (!chartInstance) return;
  19. chartInstance.destroy();
  20. chartInstance = null;
  21. });
  22. return {
  23. setOptions,
  24. };
  25. }