用echarts绘制中国地图显示个省份数据

1,074 阅读1分钟

要求的样子

image.png

准备工作

首先引入echarts工具和中国地图数据js

import * as echarts from 'echarts'
import './china.js' // 引入中国地图数据`

我也是扒的别人的(- China.js链接:pan.baidu.com/s/1QLnaI0UZ…
提取码:2000)

series的设置

写完后的效果

initCharts() {
      let mapcharts = echarts.init(this.$refs.chinaMap)
      //窗口尺寸改变
      window.addEventListener('resize', function () {
        mapcharts.resize()
      })

      // 绘制图表
      mapcharts.setOption({
        title: {
          text: '',
          left: 'center',
          textStyle: {
            color: '#fff',
          },
        },
        tooltip: {
          trigger: 'item',
          formatter: function (params) {
            return params.name + ' : ' + params.value[2] ? params.value[2] : ''
          },
        },
        visualMap: {
          // min: 0,
          // max: 300,
          // left: 20,
          // bottom: 20,
          // calculable: true,
          // text: ['高','低'],
          show: false,
          inRange: {
            color: ['rgb(70, 240, 252)', 'rgb(250, 220, 46)', 'rgb(54, 229, 255)'],
          },
          textStyle: {
            color: '#fff',
          },
        },
        geo: {
          // map: name ? name : 'china', // 核心
          // zoom: 1.2,
          // // roam: 'scale', //是否开启鼠标缩放和平移漫游。默认不开启。如果只想要开启缩放或者平移,可以设置成 'scale' 或者 'move'。设置成 true 为都开启
          // label: {
          //   // 页面初始化加载的文字
          //   normal: {
          //     show: true,
          //     textStyle: {
          //       color: '#ccc', // 页面初始化的地图文字颜色
          //       fontSize: 10, // // 页面初始化的地图文字大小
          //     },
          //   },
          // },
          itemStyle: {
            //设置样式
            normal: {
              areaColor: 'rgba(48, 82, 153, 0.5)', // 区域的颜色
              borderColor: '#59e2f9', //边框颜色
            },
            emphasis: {
              areaColor: 'rgb(44, 165, 198, 0.6)', // 鼠标悬浮区域颜色
            },
          },
          emphasis: {
            label: {
              color: '#fff', // 鼠标悬浮文字颜色
            },
          },
        },
        series: [
          {
            type: 'map',
            map: 'china',
            layoutCenter: ['40%', '50%'], // 地图布局中心点
            layoutSize: 270,
            label: {
              show: true,
              color: '#ffffff',
              fontSize: 10,
            },
            itemStyle: {
              areaColor: '#60a1db',
              borderColor: '#24dafe',
            },
            roam: true,
            zoom: 1.2,
            emphasis: {
              label: {
                color: '#fff',
                fontSize: 12,
                fontWeight: 'bold',
              },
              itemStyle: {
                areaColor: 'none',
                borderColor: '#77ebff',
                borderWidth: 2,
              },
            },
            data: [
              { name: '北京', value: 17 },
              { name: '天津', value: 12 },
              { name: '上海', value: 15 },
              { name: '重庆', value: 15 },
              { name: '河北', value: 15 },
              { name: '河南', value: 15 },
              { name: '云南', value: 5 },
              { name: '辽宁', value: 15 },
              { name: '黑龙江', value: 15 },
              { name: '湖南', value: 2 },
              { name: '安徽', value: 15 },
              { name: '山东', value: 15 },
              { name: '新疆', value: 3 },
              { name: '江苏', value: 3 },
              { name: '浙江', value: 9 },
              { name: '江西', value: 15 },
              { name: '湖北', value: 4 },
              { name: '广西', value: 4 },
              { name: '甘肃', value: 10 },
              { name: '山西', value: 15 },
              { name: '内蒙古', value: 15 },
              { name: '陕西', value: 9 },
              { name: '吉林', value: 8 },
              { name: '福建', value: 9 },
              { name: '贵州', value: 9 },
              { name: '广东', value: 8 },
              { name: '青海', value: 3 },
              { name: '西藏', value: 9 },
              { name: '四川', value: 0 },
              { name: '宁夏', value: 15 },
              { name: '海南', value: 7 },
              { name: '台湾', value: 4 },
              { name: '香港', value: 4 },
              { name: '澳门', value: 3 },
            ],
          },
          {
            type: 'effectScatter',
            coordinateSystem: 'geo',
            label: {
              lineHeight: 20,
              normal: {
                show: true,
                color: '#081727',
                position: 'inside',
                padding: [5, 0, 0, 0],
                verticalAlign: 'middle',
                formatter: function (para) {
                  return '{name|' + para.data.value[2] + '}'
                },
                rich: {
                  cnNum: {
                    fontSize: 14,
                    color: '#081727',
                    lineHeight: 20,
                  },
                },
              },
            },
            tooltip: {
              formatter: function (para) {
                return para.name + ': ' + para.data.value[2]
              },
            },
            symbol: 'circle',
            symbolSize: [15, 15],
            data: this.sfList,
          },
        ],
      })
    },

image.png

写完后,涟漪效果不能实现,后面一段时间都在想涟漪效果要经纬度,但是后面在this.sfList里面加了经纬度还是没有出现涟漪效果,后面扒文档才知道,series的type类型设置'map'地图上显示各个省份的名称,把它改为type:'effectScatter'后,才能有涟漪的效果。

series: [
          {
            type: 'effectScatter',
            coordinateSystem: 'geo',
            showEffectOn: 'render',
            symbolSize: [15, 15],
            rippleEffect: {
              scale: 10,
              period:5,
              brushType: 'stroke',
            },
            itemStyle: {
              normal: {
                color: function (params) {
                  var color = ''
                  var value = params.value
                  console.log(value[2]) 
                  if (value[2] < 10) {
                    color = '#16ffff'
                  }
                  if (value[2] >= 10 && value[2] < 50) {
                    color = '#ffe35c'
                  }
                  if (value[2] >= 50) {
                    color = '#84d74d'
                  }
                  return color
                },
              },
            },
            data: this.sfList,//这是你需要在地图上展示的数据
          },
        ],

更改后的效果

image.png

image.png

功能虽然实现了,但是样子越来越丑了。。。

完整代码如下:

<template>
  <div class="chinaMap" ref="chinaMap" style="width: 100%; height: 100%"></div>
</template>

<script>
import { getAction } from '@/api/manage'
import * as echarts from 'echarts'
import './china.js' // 引入中国地图数据

export default {
  data() {
    return {
      geoCoordMap: {
        台湾省: [121.5135, 25.0308],
        黑龙江省: [127.9688, 45.368],
        内蒙古自治区: [110.3467, 41.4899],
        吉林省: [125.8154, 44.2584],
        北京市: [116.4551, 40.2539],
        辽宁省: [123.1238, 42.1216],
        河北省: [114.4995, 38.1006],
        天津省: [117.4219, 39.4189],
        山西省: [112.3352, 37.9413],
        陕西省: [109.1162, 34.2004],
        甘肃省: [103.5901, 36.3043],
        宁夏回族自治区: [106.3586, 38.1775],
        青海省: [101.4038, 36.8207],
        新疆维吾尔自治区: [87.9236, 43.5883],
        西藏自治区: [91.11, 29.97],
        四川省: [103.9526, 30.7617],
        重庆市: [108.384366, 30.439702],
        山东省: [117.1582, 36.8701],
        河南省: [113.4668, 34.6234],
        江苏省: [118.8062, 31.9208],
        安徽省: [117.29, 32.0581],
        湖北省: [114.3896, 30.6628],
        浙江省: [119.5313, 29.8773],
        福建省: [119.4543, 25.9222],
        江西省: [116.0046, 28.6633],
        湖南省: [113.0823, 28.2568],
        贵州省: [106.6992, 26.7682],
        云南省: [102.9199, 25.4663],
        广东省: [113.12244, 23.009505],
        广西壮族自治区: [108.479, 23.1152],
        海南省: [110.3893, 19.8516],
        上海市: [121.4648, 31.2891],
      },
      sfList: [ ],
      data: [
        {
          name: '北京市',
          value: [116.4551, 39.9111, 110],
        },
        {
          name: '重庆市',
          value: [106.55, 29.56, 32], // value的前两项是经纬度坐标,第三项为专家人数数据
        },
        {
          name: '上海市',
          value: [121.4648, 31.2891, 25],
        },
      ],
    }
  },
  created() {
    this.convertData()
  },
  mounted() {
    // this.initCharts()
  },
  methods: {
    convertData() {
      getAction('/society/expert/getExpert', {}).then((res) => {
        const { success, result } = res
        let list = result.splice(0,4)  //去除除省份外的数据
        let arr = result
        if (success && result) {
          for (let v in arr) {
            for (let m in this.geoCoordMap) {
              if (arr[v].name == m) {
                this.sfList.push({
                  name: arr[v].name,
                  value: this.geoCoordMap[m].concat(arr[v].value),
                })
              }
            }
          }
          this.initCharts()
        }
      })
    },
    initCharts() {
      let mapcharts = echarts.init(this.$refs.chinaMap)
      //窗口尺寸改变
      window.addEventListener('resize', function () {
        mapcharts.resize()
      })

      // 绘制图表  配置各项属性
      mapcharts.setOption({
        geo: {
          map: 'china',
          silent: true,
          itemStyle: {
            color: '#004981',
            borderColor: 'rgb(54,192,118)',
          },
        },
        // 自定义提示框内容
        tooltip: {
          // alwaysShowContent: true, // 提示框总是显示(不再是鼠标离开就消失)
          // enterable: true,    // 允许提示框被点击
          formatter: function (params) {
            var value = params.value
            return '专家人数' + '<br/>' + params.name + ': ' + value[2] + '人'
          },
        },

        series: [
          {
            type: 'effectScatter',
            coordinateSystem: 'geo',
            showEffectOn: 'render',
            symbolSize: [15, 15],
            rippleEffect: {
              scale: 10,
              period:5,
              brushType: 'stroke',
            },
            itemStyle: {
              normal: {
                color: function (params) {
                  var color = ''
                  var value = params.value
                  console.log(value[2]) 
                  if (value[2] < 10) {
                    color = '#16ffff'
                  }
                  if (value[2] >= 10 && value[2] < 50) {
                    color = '#ffe35c'
                  }
                  if (value[2] >= 50) {
                    color = '#84d74d'
                  }
                  return color
                },
              },
            },
            data: this.sfList,
          },
        ],
      })
    },
  },
}
</script>