use-echarts.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import type { EChartsOption } from 'echarts';
  2. import type EchartsUI from './echarts-ui.vue';
  3. import type { Ref } from 'vue';
  4. import { computed, nextTick, watch } from 'vue';
  5. import { preferences, usePreferences } from '@vben-core/preferences';
  6. import {
  7. tryOnUnmounted,
  8. useDebounceFn,
  9. useTimeoutFn,
  10. useWindowSize,
  11. } from '@vueuse/core';
  12. import echarts from './echarts';
  13. type EchartsUIType = typeof EchartsUI | undefined;
  14. type EchartsThemeType = 'dark' | 'light' | null;
  15. function useEcharts(chartRef: Ref<EchartsUIType>) {
  16. let chartInstance: echarts.ECharts | null = null;
  17. let cacheOptions: EChartsOption = {};
  18. const { isDark } = usePreferences();
  19. const { height, width } = useWindowSize();
  20. const resizeHandler: () => void = useDebounceFn(resize, 200);
  21. const getOptions = computed((): EChartsOption => {
  22. if (!isDark.value) {
  23. return cacheOptions;
  24. }
  25. return {
  26. backgroundColor: 'transparent',
  27. ...cacheOptions,
  28. };
  29. });
  30. const initCharts = (t?: EchartsThemeType) => {
  31. const el = chartRef?.value?.$el;
  32. if (!el) {
  33. return;
  34. }
  35. chartInstance = echarts.init(el, t || isDark.value ? 'dark' : null);
  36. return chartInstance;
  37. };
  38. const renderEcharts = (options: EChartsOption, clear = true) => {
  39. cacheOptions = options;
  40. return new Promise((resolve) => {
  41. if (chartRef.value?.offsetHeight === 0) {
  42. useTimeoutFn(() => {
  43. renderEcharts(getOptions.value);
  44. resolve(null);
  45. }, 30);
  46. return;
  47. }
  48. nextTick(() => {
  49. useTimeoutFn(() => {
  50. if (!chartInstance) {
  51. const instance = initCharts();
  52. if (!instance) return;
  53. }
  54. clear && chartInstance?.clear();
  55. chartInstance?.setOption(getOptions.value);
  56. resolve(null);
  57. }, 30);
  58. });
  59. });
  60. };
  61. function resize() {
  62. chartInstance?.resize({
  63. animation: {
  64. duration: 300,
  65. easing: 'quadraticIn',
  66. },
  67. });
  68. }
  69. watch([width, height], () => {
  70. resizeHandler?.();
  71. });
  72. watch(isDark, () => {
  73. if (chartInstance) {
  74. chartInstance.dispose();
  75. initCharts();
  76. renderEcharts(cacheOptions);
  77. resize();
  78. }
  79. });
  80. watch(
  81. [
  82. () => preferences.sidebar.collapsed,
  83. () => preferences.sidebar.extraCollapse,
  84. () => preferences.sidebar.hidden,
  85. ],
  86. () => {
  87. // 折叠动画200ms
  88. setTimeout(() => {
  89. resize();
  90. }, 200);
  91. },
  92. );
  93. tryOnUnmounted(() => {
  94. // 销毁实例,释放资源
  95. chartInstance?.dispose();
  96. });
  97. return {
  98. renderEcharts,
  99. resize,
  100. };
  101. }
  102. export { useEcharts };
  103. export type { EchartsUIType };