index.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <v-chart
  3. ref="vChartRef"
  4. :theme="themeColor"
  5. :option="option"
  6. :manual-update="isPreview()"
  7. :update-options="{
  8. replaceMerge: replaceMergeArr
  9. }"
  10. autoresize
  11. ></v-chart>
  12. </template>
  13. <script setup lang="ts">
  14. import { ref, nextTick, computed, watch, PropType } from 'vue'
  15. import VChart from 'vue-echarts'
  16. import { use } from 'echarts/core'
  17. import { CanvasRenderer } from 'echarts/renderers'
  18. import { BarChart } from 'echarts/charts'
  19. import config, { includes, seriesItem } from './config'
  20. import { mergeTheme } from '@/packages/public/chart'
  21. import { useChartDataFetch } from '@/hooks'
  22. import { CreateComponentType } from '@/packages/index.d'
  23. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  24. import { isPreview } from '@/utils'
  25. import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
  26. import isObject from 'lodash/isObject'
  27. const props = defineProps({
  28. themeSetting: {
  29. type: Object,
  30. required: true
  31. },
  32. themeColor: {
  33. type: Object,
  34. required: true
  35. },
  36. chartConfig: {
  37. type: Object as PropType<config>,
  38. required: true
  39. }
  40. })
  41. use([DatasetComponent, CanvasRenderer, BarChart, GridComponent, TooltipComponent, LegendComponent])
  42. const replaceMergeArr = ref<string[]>()
  43. const option = computed(() => {
  44. return mergeTheme(props.chartConfig.option, props.themeSetting, includes)
  45. })
  46. // dataset 无法变更条数的补丁
  47. watch(
  48. () => props.chartConfig.option.dataset,
  49. (newData: { dimensions: any }, oldData) => {
  50. try {
  51. if (!isObject(newData) || !('dimensions' in newData)) return
  52. if (Array.isArray(newData?.dimensions)) {
  53. const seriesArr = []
  54. for (let i = 0; i < newData.dimensions.length - 1; i++) {
  55. seriesArr.push(seriesItem)
  56. }
  57. replaceMergeArr.value = ['series']
  58. props.chartConfig.option.series = seriesArr
  59. nextTick(() => {
  60. replaceMergeArr.value = []
  61. })
  62. }
  63. } catch (error) {
  64. console.log(error)
  65. }
  66. },
  67. {
  68. deep: false
  69. }
  70. )
  71. const { vChartRef } = useChartDataFetch(props.chartConfig, useChartEditStore)
  72. </script>