chart.vue 865 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <script setup lang="ts">
  2. import { echartsInstance, ECOption } from './index';
  3. import { onMounted, ref, unref, warn } from 'vue';
  4. import { usePreferences } from '@vben-core/preferences';
  5. const { isDark } = usePreferences();
  6. interface Props {
  7. height?: string;
  8. width?: string;
  9. }
  10. withDefaults(defineProps<Props>(), {
  11. height: '500px',
  12. width: '100%',
  13. });
  14. const instance = ref();
  15. const instanceRef = ref(HTMLElement);
  16. onMounted(() => {
  17. instance.value = echartsInstance.init(
  18. instanceRef.value,
  19. isDark.value ? 'dark' : '',
  20. );
  21. });
  22. const setChart = (option: ECOption, clear: boolean = true) => {
  23. const c = unref(instance);
  24. if (!c) {
  25. warn('instance is null');
  26. return;
  27. }
  28. if (clear) c.clear();
  29. c.setOption(option);
  30. };
  31. defineExpose({ setChart });
  32. </script>
  33. <template>
  34. <div ref="instanceRef" :style="{ height, width }"></div>
  35. </template>