index.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <div class="echart" :style="{ width: width, height: height }">
  3. <div ref="chartElement" class="echart__container"></div>
  4. </div>
  5. </template>
  6. <script lang="ts">
  7. import { defineComponent, ref, onMounted, PropType, watch, watchEffect } from 'vue';
  8. import { EChartsOption } from 'echarts';
  9. import { useEchart } from './setup';
  10. export default defineComponent({
  11. name: 'Echart',
  12. props: {
  13. // 图表宽度
  14. width: {
  15. type: String,
  16. default: '100%',
  17. },
  18. // 图表高度
  19. height: {
  20. type: String,
  21. default: '100%',
  22. },
  23. // 图表配置项
  24. options: {
  25. type: Object as PropType<EChartsOption>,
  26. required: true,
  27. },
  28. loading: {
  29. type: Boolean,
  30. default: true,
  31. },
  32. },
  33. setup(props, context) {
  34. // 图表容器元素
  35. const chartElement = ref<HTMLElement>();
  36. onMounted(() => {
  37. const { echart, setOptions, showLoading } = useEchart(chartElement.value!, context);
  38. watchEffect(() => {
  39. if (props.loading) {
  40. echart.clear();
  41. showLoading(true);
  42. }
  43. });
  44. watch(
  45. () => props.options,
  46. (option) => {
  47. // 设置图表配置项
  48. setOptions(option);
  49. }
  50. );
  51. });
  52. return {
  53. chartElement,
  54. };
  55. },
  56. });
  57. </script>
  58. <style lang="less">
  59. .echart {
  60. position: relative;
  61. overflow: hidden;
  62. &__container {
  63. width: 100%;
  64. height: 100%;
  65. }
  66. }
  67. </style>