react ts Echart地图散点地图

这篇博客介绍了如何在React应用中结合ECharts库,利用YunNan.json地理数据文件,绘制出云南省的地图,并在地图上展示散点数据。文章详细阐述了从安装ECharts到加载地图数据,再到配置选项和实现点击事件的整个过程,展示了地图上的散点分布和效果。
摘要由CSDN通过智能技术生成


在这里插入图片描述

项目引入Echart

npm install echarts --save

YunNan.json下载

http://datav.aliyun.com/portal/school/atlas/area_selector

index.tsx页面

import { useEffect } from "react"
import * as echarts from 'echarts'; //全局引入 ,可按需引入
import YunNan from "../../YunNan.json";
import "./map.css";

const seriesData = [
  { name: '昆明市', value: 245 },
  { name: '大理白族自治州', value: 100 },
  { name: '丽江市', value: 17 }
];
const Echarts = () => {
  let myChart: string | echarts.ECharts | null | undefined;
  useEffect(() => {
    loadingChina()
  }, [])

  const loadingChina = () => {
    mapOption("YunNan", YunNan)  //初始化-创建地图
  }

  const mapOption = (mapName: string, data: any) => {
    if (myChart !== null && myChart !== "" && myChart !== undefined) {
      myChart.dispose();//销毁(如果存在需要销毁,点击时会创建新的图表对象)
    }
    myChart = echarts.init(document.querySelector('.map'));//初始化

    echarts.registerMap(mapName, data)

    const geoCoordMap = data.features.map((item: { properties: { name: any; center: any; }; }) => {
      return {
        name: item.properties.name,
        value: item.properties.center
      }
    });

    const convertData = (data: string | any[]) => {
      const res = [];
      for (let i = 0; i < data.length; i++) {
        if (geoCoordMap.filter((item: { name: any; }) => item.name === data[i].name).length)
          res.push({
            name: data[i].name,
            value: geoCoordMap.filter((item: { name: any; }) => item.name === data[i].name)[0].value.concat(data[i].value)
          })
      }
      return res;
    }

    const option = {
      title: {
        top: 20,
        text: '地图展示',
        subtext: '',
        x: 'center',
        textStyle: {
          color: '#FFF',
          fontSize: "48px"
        }
      },
      tooltip: {
        trigger: 'item',
        formatter(params: { name: any; value: any[]; }) {
          return `${params.name}:${params.value[2]}`;
        },
      },
      roam: true,
      zoom: 1.5,   //设置地图放大
      legend: {
        orient: 'vertical',
        y: 'bottom',
        x: 'right',
        data: ['xxx分布'],
        textStyle: {
          color: '#fff',
          fontSize: 28
        },
      },
      geo: {
        map: mapName,
        roam: true,
        geoIndex: 1,
        zoom: 1.2,  //地图的比例
        label: {
          normal: {
            show: true,
            textStyle: {
              color: '#FFFFFF',  //字体颜色
              fontSize: 28
            }
          },
          emphasis: {
            textStyle: {
              color: '#FFFFFF',  //选中后的字体颜色
              fontSize: 28
            }
          }
        },
        itemStyle: {
          normal: {
            areaColor: 'rgb(119, 224, 242)', //地区填充颜色
            borderColor: 'rgba(0, 162, 255, 0.8)',//边界线颜色
            shadowColor: 'rgb(119, 224, 242)',//阴影颜色
            shadowBlur: 20,//模糊大小
            borderWidth: 2,
            colorStops: [{
              offset: 0, color: '#0AFBFF'
            }, {
              offset: 1, color: '#0157C9'
            }],
          },
          emphasis: {  //选中样式
            areaColor: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [{
                offset: 0, color: '#0AFBFF'
              }, {
                offset: 1, color: '#0157C9'
              }],
              global: false
            },
          }
        },
      },
      series: [
        {
          name: 'xxx分布',
          type: 'scatter',
          coordinateSystem: 'geo',
          data: convertData(seriesData),
          symbolSize(val: number[]) {
            return (val[2] / 100) * 15 + 5;
          },
          label: {
            normal: {
              show: true,
              textStyle: {
                fontSize: 28,
              }
            },
            emphasis: {
              show: true,
            },
          },
          itemStyle: {
            color: 'red',
            normal: {
              label: {
                show: true,
                fontSize: 28
              },
              borderWidth: 20,
            },
            emphasis: {
              borderColor: '#fff',
              borderWidth: 20,
              fontSize: 28
            },
          },
        },
        {
          name: 'xxx分布',
          type: 'effectScatter',
          coordinateSystem: 'geo',
          data: convertData(
            seriesData
              .sort((a, b) => {
                return b.count - a.count;
              })
              .slice(0, 5),
          ),
          symbolSize(val: number[]) {
            return (val[2] / 100) * 15 + 5;
          },
          showEffectOn: 'render',
          rippleEffect: {
            brushType: 'stroke',
          },
          hoverAnimation: true,
          label: {
            normal: {
              formatter: '{b}',
              position: 'left',
              show: true,
            },
          },
          itemStyle: {
            normal: {
              color: 'red',
              shadowBlur: 100,
              shadowColor: 'red',
              fontSize: 28
            },
          },
          zlevel: 1,
        },
      ],
    };

    myChart.setOption(option); //绘图

    myChart.getZr().on('click', function (params) {
      const pixelPoint = [params.offsetX, params.offsetY];
      const dataPoint = myChart.convertFromPixel({ geoIndex: 0 }, pixelPoint);
      console.log(dataPoint);
    });
  }

  return (
    <div className="map" />
  )
}
export default Echarts;

map.css页面

.map{
    height: 100vh;
    width: 100%;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值