index.vue 5.17 KB
<template>
  <!-- 原生方式,没有使用vue-echarts -->
  <div :style="`width:${w}px;height:${h}px;`" ref="map3DRef"></div>
</template>

<script setup lang="ts">
import { onMounted, ref, nextTick, PropType, toRefs, watch } from 'vue'
import * as echarts from 'echarts'
import { registerMap } from 'echarts/core'
import 'echarts-gl'
import config from './config'
import cityMap from '../OverrideMapBase/mapGeojson/china-main-city-map.json'

type historyDataType = { name: string; code: string }

const props = defineProps({
  chartConfig: {
    type: Object as PropType<config>,
    required: true
  }
})

const iconStr = ref(
  'path://M853.333333 245.333333H245.333333l93.866667-93.866666c12.8-12.8 12.8-34.133333 0-46.933334-12.8-12.8-34.133333-12.8-46.933333 0l-145.066667 145.066667c-12.8 12.8-12.8 34.133333 0 46.933333l145.066667 145.066667c6.4 6.4 14.933333 10.666667 23.466666 10.666667s17.066667-4.266667 23.466667-10.666667c12.8-12.8 12.8-34.133333 0-46.933333L256 311.466667h597.333333c6.4 0 10.666667 4.266667 10.666667 10.666666v426.666667c0 6.4-4.266667 10.666667-10.666667 10.666667H170.666667c-17.066667 0-32 14.933333-32 32s14.933333 32 32 32h682.666666c40.533333 0 74.666667-34.133333 74.666667-74.666667V320c0-40.533333-34.133333-74.666667-74.666667-74.666667z'
)

const historyData = ref<historyDataType[]>([])

const { w, h } = toRefs(props.chartConfig.attr)

const map3DRef = ref()

const chartInstance = ref()

const saveSelectValue = ref('')

const toolBoxOption = ref({
  show: true,
  right: 110,
  top: 20,
  feature: {
    myFullButton: {
      show: true,
      title: '返回',
      icon: iconStr.value,
      iconStyle: {
        color: ''
      },
      onclick: () => watchAdcode()
    }
  }
})

watch(
  () => props.chartConfig.option,
  newData => {
    const { iconColor, drillingIn, iconDistanceRight, iconDistanceTop } = newData
    toolBoxOption.value.feature.myFullButton.show = drillingIn
    toolBoxOption.value.feature.myFullButton.iconStyle.color = iconColor
    toolBoxOption.value.right = iconDistanceRight
    toolBoxOption.value.top = iconDistanceTop
  },
  {
    deep: true
  }
)

props.chartConfig.option = {
  ...props.chartConfig.option,
  ...{ toolbox: toolBoxOption.value }
}

//地图点击返回
const watchAdcode = () => {
  if (props.chartConfig.option.drillingIn) {
    const code = historyData.value.at(-2)?.code
    props.chartConfig.option.mapRegion.adcode = code ? code : 'china'
    historyData.value.pop()
  }
}

//地图点击
const handleMap3DClick = async (params: any) => {
  if (props.chartConfig.option.drillingIn) {
    const { name } = params
    saveSelectValue.value = name
    const findAdcode = (cityMap as any)[name]
    if (!findAdcode) return
    props.chartConfig.option.mapRegion.adcode = findAdcode
    historyData.value.push({
      name,
      code: findAdcode
    })
  }
}

//动态获取geojson文件进行注册地图
const getGeojson = (regionId: string) => {
  try {
    return new Promise<boolean>(resolve => {
      import(`../OverrideMapBase/mapGeojson/${regionId}.json`).then(data => {
        registerMap(regionId, { geoJSON: data.default as any, specialAreas: {} })
        resolve(true)
      })
    })
  } catch (e) {
    console.log(e)
  }
}

watch(
  () => w.value,
  (value: number) => {
    chartInstance.value.resize({
      width: value + 'px',
      height: h.value + 'px'
    })
  }
)

const initMap = async () => {
  chartInstance.value = echarts.init(map3DRef.value)
  await nextTick()
  await getGeojson(props.chartConfig.option.mapRegion.adcode)
  await nextTick().then(() => {
    handleSetOption(chartInstance.value, props.chartConfig.option)
  })
  chartInstance.value.on('click', (e: any) => {
    console.log(`地图点击`, e)
    // handleMap3DClick(e)
  })
}

// 手动触发渲染
const handleSetOption = (instance: any, option: any) => {
  if (!instance) return
  try {
    instance.clear()
    instance.setOption(option)
    // eslint-disable-next-line no-empty
  } finally {
  }
}

onMounted(() => {
  initMap()
})

//监听地图展示区域发生变化
watch(
  () => `${props.chartConfig.option.mapRegion.adcode}`,
  async (newData: any) => {
    try {
      await getGeojson(newData)
      props.chartConfig.option.geo3D.map = newData
      props.chartConfig.option.series.forEach((item: any) => {
        if (item.type === 'map3D') {
          item.map = newData
          item.data = props.chartConfig.option.dataset
        }
      })
      handleSetOption(chartInstance.value, props.chartConfig.option)
    } catch (error) {
      console.log(error)
    }
  },
  {
    immediate: true
  }
)

// 监听地图右侧配置项变化
watch(
  props.chartConfig.option,
  async newData => {
    try {
      handleSetOption(chartInstance.value, newData)
    } catch (error) {
      console.log(error)
    }
  },
  {
    deep: true
  }
)

// 监听地图dataset配置项变化
watch(
  () => props.chartConfig.option.dataset,
  newData => {
    try {
      props.chartConfig.option.series.forEach((item: any) => {
        if (item.type === 'map3D') {
          item.data = newData
        }
      })
      handleSetOption(chartInstance.value, props.chartConfig.option)
    } catch (error) {
      console.log(error)
    }
  },
  {
    deep: true
  }
)
</script>