echarts地图给不同区域设置不同颜色,动态改变地图颜色

在这里插入图片描述

动态切换地图颜色的关键方法

地图组件中对不通区域设置不同颜色:

const option = {
	geo: {
	    // areaColor: 区域颜色
		regions:[
        { name: '丘北县', itemStyle: { normal: { areaColor: '#FD9A9A' } } },
        { name: '文山市', itemStyle: { normal: { areaColor: '#FFDC63' } } },
        { name: '砚山县', itemStyle: { normal: { areaColor: '#54E6AB' } } },
        { name: '西畴县', itemStyle: { normal: { areaColor: '#489EFF' } } },
        { name: '麻栗坡县', itemStyle: { normal: { areaColor: '#54E6AB' } } },
        { name: '马关县', itemStyle: { normal: { areaColor: '#FD9A9A' } } },
        { name: '广南县', itemStyle: { normal: { areaColor: '#FFDC63' } } },
        { name: '富宁县', itemStyle: { normal: { areaColor: '#489EFF' } } }
      ]
	},
	seriers: []
}

数据改变时重绘地图的关键:
(地图组件中监听数据变化)

  watch: {
    // 关键方法,如果数据改变可以重绘地图
    'splitList': {
      handler(newVal, old) {
        this.setOption()
      },
    }
  },

父组件

<template>
  <div class=''>
    <ColorChart id="provinceChart" :mapData="mapData" width="100vw" height="40vh" :splitList="splitList"></ColorChart>
  </div>
</template>

<script>
import ColorChart from '@/components/echart-map/colorCharts.vue'

export default {
  name: '',
  props : { },
  components: { ColorChart },
  data() {
    return {
      mapData: {
        data: [
          { name: '丘北县', deptId: '22', value: 1 },
          { name: '文山市', deptId: '19', value: 2 },
          { name: '砚山县', deptId: '21', value: 3 },
          { name: '西畴县', deptId: '43', value: 4 },
          { name: '麻栗坡县', deptId: '24', value: 5 },
          { name: '马关县', deptId: '20', value: 6 },
          { name: '广南县', deptId: '23', value: 7 },
          { name: '富宁县', deptId: '', value: 8 }
        ]
      },
      splitList: [
        { name: '丘北县', itemStyle: { normal: { areaColor: '#FD9A9A' } } },
        { name: '文山市', itemStyle: { normal: { areaColor: '#FFDC63' } } },
        { name: '砚山县', itemStyle: { normal: { areaColor: '#54E6AB' } } },
        { name: '西畴县', itemStyle: { normal: { areaColor: '#489EFF' } } },
        { name: '麻栗坡县', itemStyle: { normal: { areaColor: '#54E6AB' } } },
        { name: '马关县', itemStyle: { normal: { areaColor: '#FD9A9A' } } },
        { name: '广南县', itemStyle: { normal: { areaColor: '#FFDC63' } } },
        { name: '富宁县', itemStyle: { normal: { areaColor: '#489EFF' } } }
      ]
    }
  },
  created() { },
  mounted() { },
  methods: { }
}
</script>

<style scoped lang='scss'></style>

子组件

位置 @/components/echart-map/colorCharts.vue
<template>
  <div :id="id" :style="{ width, height }"></div>
</template>

<script>
import * as echarts from 'echarts'
import wenshan from './city-data.json'
export default {
  name: 'ColorChart',
  props: ['id', 'width', 'height', 'mapData', 'splitList'],
  computed: {},
  data() {
    return {
      myMap: {}
    }
  },
  watch: {
    // 关键方法,如果数据改变可以重绘地图
    'splitList': {
      handler(newVal, old) {
        this.setOption()
      },
    }
  },
  created() { },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      const _this = this
      echarts.registerMap("wenshan", wenshan);
      this.myMap = echarts.init(document.getElementById(this.id),);

      window.addEventListener("resize", function () {
        _this.myMap.resize();
      });
      this.setOption()
    },
    setOption() {
      // 设置基础配置项
      const option = {
        geo: {
          map: 'wenshan',
          roam: false,//滚轮 放大缩小
          zoom: 1.1,
          label: {
            show: true,
            fontSize: '14px',
            color: '#49546B',
            emphasis: {
              show: true, // 选中时是否展示label
            }
          },
          //是否允许拖拽
          roam: false,
          itemStyle: {
            normal: {
              areaColor: '#323c48',//默认区块颜色
              // borderColor: '#03b8c0',//区块描边颜色
              borderWidth: 0//区块描边颜色
            },
            // emphasis: {
            //   areaColor: '#45ad00'//鼠标划过区块的颜色
            // },

          },
          // 地图区域样式设置
          regions: this.splitList
        },
        series: [
          {
            name: '文山市',
            type: 'effectScatter',
            coordinateSystem: 'geo',
            map: "wenshan",
            showEffectOn: 'render',
            rippleEffect: {
              brushType: 'stroke'
            },
            hoverAnimation: true,
            zlevel: 1,
            data: this.mapData ? this.mapData.data : [],
          },
        ],
      };
      // 将配置应用到地图上
      this.myMap.setOption(option);
      // this.myMap.on('click', function (params) {
      //   this.$emit('handleClick', params.data)
      // });
    }
  }
}
</script>

<style scoped lang='scss'>
</style>

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
echarts 中,可以通过设置地图区域的 itemStyle 属性来实现鼠标划过区域颜色的变化。 具体实现步骤如下: 1. 在 echarts 中引入地图数据,并配置地图的基本属性。 2. 设置地图区域的 itemStyle 属性,并定义鼠标划过和普通状态下的区域颜色。 3. 使用 echarts 的事件监听器,监听地图区域的鼠标悬停事件。 4. 在事件回调函数中,根据当前区域的状态,设置对应的颜色。 以下是一个简单的示例代码: ```javascript // 引入 echarts import echarts from 'echarts'; // 初始化 echarts 实例 const myChart = echarts.init(document.getElementById('myChart')); // 配置地图属性 const option = { // 引入地图数据 geo: { map: 'china', }, // 设置地图区域颜色 series: [{ type: 'map', map: 'china', itemStyle: { normal: { // 普通状态下的颜色 areaColor: '#ccc', }, emphasis: { // 鼠标悬停时的颜色 areaColor: '#ff0', }, }, }], }; // 监听地图区域的鼠标悬停事件 myChart.on('mouseover', function (params) { // 获取当前区域的名称 const name = params.name; // 获取地图实例 const chart = params.event.target; // 设置当前区域颜色 chart.style.fill = '#f00'; }); // 渲染地图 myChart.setOption(option); ``` 以上代码中,我们先引入 echarts,并初始化一个 echarts 实例。然后,我们配置了地图的基本属性,并设置地图区域的 itemStyle 属性。 接着,我们使用 echarts 的事件监听器,监听了地图区域的鼠标悬停事件。在事件回调函数中,我们根据当前区域的名称和状态,设置了对应的颜色。 最后,我们调用了 setOption 方法,渲染了地图。这样,当用户鼠标悬停在某个区域时,该区域颜色就会发生变化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值