index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <v-chart ref="vChartRef" :theme="themeColor" :option="option.value" :manual-update="isPreview()" autoresize>
  3. </v-chart>
  4. </template>
  5. <script setup lang="ts">
  6. import { PropType, reactive, watch } from 'vue'
  7. import config, { includes } from './config'
  8. import VChart from 'vue-echarts'
  9. import { use, registerMap } from 'echarts/core'
  10. import { EffectScatterChart, MapChart } from 'echarts/charts'
  11. import { CanvasRenderer } from 'echarts/renderers'
  12. import { useChartDataFetch } from '@/hooks'
  13. import { mergeTheme } from '@/packages/public/chart'
  14. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  15. import { isPreview } from '@/utils'
  16. import mapJson from './map.json'
  17. import mapJsonWithoutHainanIsLands from './mapWithoutHainanIsLands.json'
  18. import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent, GeoComponent } from 'echarts/components'
  19. const props = defineProps({
  20. themeSetting: {
  21. type: Object,
  22. required: true
  23. },
  24. themeColor: {
  25. type: Object,
  26. required: true
  27. },
  28. chartConfig: {
  29. type: Object as PropType<config>,
  30. required: true
  31. }
  32. })
  33. use([
  34. MapChart,
  35. DatasetComponent,
  36. CanvasRenderer,
  37. GridComponent,
  38. TooltipComponent,
  39. LegendComponent,
  40. GeoComponent,
  41. EffectScatterChart
  42. ])
  43. registerMap('china', { geoJSON: mapJson as any, specialAreas: {} })
  44. const option = reactive({
  45. value: mergeTheme(props.chartConfig.option, props.themeSetting, includes)
  46. })
  47. const dataSetHandle = (dataset: any) => {
  48. props.chartConfig.option.series.forEach((item: any) => {
  49. if (item.type === 'effectScatter' && dataset.point) item.data = dataset.point
  50. else if (item.type === 'map' && dataset.point) item.data = dataset.map
  51. option.value = props.chartConfig.option
  52. })
  53. }
  54. const mapTypeHandle = (show: boolean) => {
  55. show
  56. ? registerMap('china', { geoJSON: mapJson as any, specialAreas: {} })
  57. : registerMap('china', { geoJSON: mapJsonWithoutHainanIsLands as any, specialAreas: {} })
  58. option.value = props.chartConfig.option
  59. }
  60. watch(
  61. () => props.chartConfig.option.series[1].itemStyle.showHainanIsLands,
  62. newData => {
  63. mapTypeHandle(newData)
  64. },
  65. {
  66. deep: true,
  67. immediate: true
  68. }
  69. )
  70. watch(
  71. () => props.chartConfig.option.dataset,
  72. newData => {
  73. dataSetHandle(newData)
  74. },
  75. {
  76. immediate: true
  77. }
  78. )
  79. // 预览
  80. useChartDataFetch(props.chartConfig, useChartEditStore, (newData: any) => {
  81. dataSetHandle(newData)
  82. })
  83. </script>