Charts.vue 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <!-- charts基础组件 -->
  2. <template>
  3. <Spin :spinning="loading" :indicator="indicator">
  4. <div ref="chartRef" :style="{width: width + 'px', height: height + 'px'}"></div>
  5. </Spin>
  6. </template>
  7. <script setup lang="ts">
  8. import { ref, Ref, defineProps, watch, nextTick, h } from "vue";
  9. import { Spin } from "ant-design-vue";
  10. import { LoadingOutlined } from "@ant-design/icons-vue";
  11. import { useEcharts } from "./hooks/useEcharts";
  12. import type { EChartsOption } from "echarts";
  13. import { throttle } from "lodash-es";
  14. const props = defineProps<{
  15. echartsOptions: EChartsOption;
  16. width: number;
  17. height: number;
  18. loading?: boolean;
  19. }>();
  20. const chartRef: Ref<null | HTMLDivElement> = ref(null);
  21. const { setOptions, resize } = useEcharts(chartRef as Ref<HTMLDivElement>);
  22. const indicator = h(LoadingOutlined, {
  23. style: {
  24. fontSize: "24px",
  25. },
  26. });
  27. watch(
  28. () => [props.width, props.height],
  29. throttle(async () => {
  30. resize();
  31. }, 200)
  32. );
  33. watch(
  34. () => props,
  35. async () => {
  36. await nextTick();
  37. const { echartsOptions } = props;
  38. setOptions(echartsOptions);
  39. },
  40. {
  41. immediate: true,
  42. deep: true,
  43. }
  44. );
  45. </script>
  46. <style scoped></style>