| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <template>
- <v-chart ref="vChartRef" :theme="themeColor" :option="option.value" :manual-update="isPreview()" autoresize>
- </v-chart>
- </template>
- <script setup lang="ts">
- import { PropType, reactive, watch, ref, nextTick } from 'vue'
- import config, { includes, MapDefaultConfig } from './config'
- import VChart from 'vue-echarts'
- import { use, registerMap, getMap } from 'echarts/core'
- import { EffectScatterChart, MapChart } from 'echarts/charts'
- import { CanvasRenderer } from 'echarts/renderers'
- import { useChartDataFetch } from '@/hooks'
- import { mergeTheme } from '@/packages/public/chart'
- import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
- import { isPreview } from '@/utils'
- import mapJsonWithoutHainanIsLands from './mapWithoutHainanIsLands.json'
- import {
- DatasetComponent,
- GridComponent,
- TooltipComponent,
- LegendComponent,
- GeoComponent,
- VisualMapComponent
- } from 'echarts/components'
- const props = defineProps({
- themeSetting: {
- type: Object,
- required: true
- },
- themeColor: {
- type: Object,
- required: true
- },
- chartConfig: {
- type: Object as PropType<config>,
- required: true
- }
- })
- use([
- MapChart,
- DatasetComponent,
- CanvasRenderer,
- GridComponent,
- TooltipComponent,
- LegendComponent,
- GeoComponent,
- EffectScatterChart,
- VisualMapComponent
- ])
- const option = reactive({
- value: mergeTheme(props.chartConfig.option, props.themeSetting, includes)
- })
- const vChartRef = ref<typeof VChart>()
- //动态获取json注册地图
- const getGeojson = (regionId: string) => {
- return new Promise<boolean>((resolve, reject) => {
- import(`./mapGeojson/${regionId}.json`).then(data => {
- registerMap(regionId, { geoJSON: data.default as any, specialAreas: {} })
- resolve(true)
- })
- })
- }
- //异步时先注册空的 保证初始化不报错
- registerMap(props.chartConfig.option.mapRegion.adcode, { geoJSON: {} as any, specialAreas: {} })
- // 进行更换初始化地图
- const registerMapInitAsync = async () => {
- await nextTick()
- await getGeojson(props.chartConfig.option.mapRegion.adcode)
- vEchartsSetOption()
- }
- registerMapInitAsync()
- const updateOptions = async () => {
- option.value = props.chartConfig.option
- }
- const vEchartsSetOption =()=>{
- vChartRef.value?.setOption(props.chartConfig.option)
- }
- const dataSetHandle = async (dataset: any) => {
- props.chartConfig.option.series.forEach((item: any) => {
- if (item.type === 'effectScatter' && dataset.point) item.data = dataset.point
- else if (item.type === 'map' && dataset.point) item.data = dataset.map
- })
- updateOptions()
- }
- // 更换地图
- const mapGeoHandle = async (regionId: string) => {
- await getGeojson(regionId)
- props.chartConfig.option.geo.map = regionId
- props.chartConfig.option.series.forEach((item: any) => {
- if (item.type === 'map') item.map = regionId
- })
- updateOptions()
- }
- //是否显示南海群岛
- const mapTypeHandle = async (show: boolean) => {
- if (show) {
- await getGeojson('china')
- } else {
- registerMap('china', { geoJSON: mapJsonWithoutHainanIsLands as any, specialAreas: {} })
- }
- vEchartsSetOption()
- }
- //层级发生变化
- const mapZoomHandle = async (newData: number) => {
- props.chartConfig.option.series[1].zoom = newData
- updateOptions()
- }
- //监听数据发生变化
- watch(
- () => props.chartConfig.option.dataset,
- newData => {
- dataSetHandle(newData)
- },
- {
- immediate: true,
- deep: false
- }
- )
- //监听是否显示南海群岛
- watch(
- () => props.chartConfig.option.mapRegion.showHainanIsLands,
- newData => {
- mapTypeHandle(newData)
- },
- {
- deep: false
- }
- )
- //监听地图区域发生变化
- watch(
- () => props.chartConfig.option.mapRegion.adcode,
- newData => {
- mapGeoHandle(newData)
- },
- {
- deep: false
- }
- )
- //监听大小发生变化
- watch(
- () => props.chartConfig.option.geo.zoom,
- newData => {
- mapZoomHandle(newData)
- },
- {
- deep: false
- }
- )
- // 预览
- useChartDataFetch(props.chartConfig, useChartEditStore, (newData: any) => {
- console.log('预览', newData)
- dataSetHandle(newData)
- })
- </script>
|