index.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <v-chart ref="vChartRef" :theme="themeColor" :option="option" :manual-update="isPreview()" autoresize></v-chart>
  3. </template>
  4. <script setup lang="ts">
  5. import { ref, computed, PropType, watch } from 'vue'
  6. import VChart from 'vue-echarts'
  7. import dataJson from './data.json'
  8. import { use } from 'echarts/core'
  9. import { CanvasRenderer } from 'echarts/renderers'
  10. import { TreemapChart } from 'echarts/charts'
  11. import { includes } from './config'
  12. import { mergeTheme } from '@/packages/public/chart'
  13. import { useChartDataFetch } from '@/hooks'
  14. import { CreateComponentType } from '@/packages/index.d'
  15. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  16. import { isPreview } from '@/utils'
  17. const props = defineProps({
  18. themeSetting: {
  19. type: Object,
  20. required: true
  21. },
  22. themeColor: {
  23. type: Object,
  24. required: true
  25. },
  26. chartConfig: {
  27. type: Object as PropType<CreateComponentType>,
  28. required: true
  29. }
  30. })
  31. use([CanvasRenderer, TreemapChart])
  32. const vChartRef = ref<typeof VChart>()
  33. const option = computed(() => {
  34. return mergeTheme(props.chartConfig.option, props.themeSetting, includes)
  35. })
  36. const dataSetHandle = (dataset: typeof dataJson) => {
  37. if (dataset) {
  38. props.chartConfig.option.series[0].data = dataset
  39. vChartRef.value?.setOption(props.chartConfig.option)
  40. }
  41. }
  42. watch(
  43. () => props.chartConfig.option.dataset,
  44. newData => {
  45. dataSetHandle(newData)
  46. },
  47. {
  48. deep: false
  49. }
  50. )
  51. useChartDataFetch(props.chartConfig, useChartEditStore, (newData: typeof dataJson) => {
  52. dataSetHandle(newData)
  53. })
  54. </script>