react使用echarts地图实现中国地图大区展示

本文介绍了如何在React项目中利用Echarts显示中国地图,并将其分为六个大区,每个区域使用不同颜色区分。通过create-react-app创建项目,安装Echarts依赖,并引入地图数据。详细代码已分享在码云。
摘要由CSDN通过智能技术生成

   日常项目中经常会用到百度地图或者echarts图标展示,今天给大家展示的是如何在react开发项目中使用百度echars的地图展示,把中国地图分为东北大区、华东大区、华南大区、华西大区、华中大区以及华北大区并用颜色标识方便识别。

    最终展示效果如下:

  

我是直接用的react官方提供的create-react-app快速构建开发环境,之后就是cnpm install --save echarts 安装echarts依赖了。

在项目中引入echarts以及中国地图数据,并引入模拟的大区地图数据。

import echarts from 'echarts';
import 'echarts/map/js/china';
import geoJson from 'echarts/map/json/china.json';
import {geoCoordMap,provienceData} from "./geo";

 

geo.js是模拟的大区地图数据,由于现在echarts地图数据在线生成工具不能使用,为我们的开发带来许多不便,但我们仍能从以前的开发列子中找到生成方法,具体推荐大家观看官方社区地图实例:
http://gallery.echartsjs.com/explore.html#charts=map~sort=rank~timeframe=all~author=all
 刚开始看echarts也被里面的各种配置参数所迷糊,但写了几遍后不难理解地图是如何构建的,最主要的是在setOption里面的option参数,其中尤其series配置参数最为重要,根据需要展示的地图,把地图分为map、scatter两类,在分别对两个参数进行配置修改,具体的实现是在initalECharts方法里面,并在componentDidMount生命周期中调用:
 
    componentDidMount() {
        this.initalECharts();
    }
    initalECharts() {
        const data = [
            {name: '上海', area: '华东大区', type: 'areaCenterCity'},
            {name: '深圳', area: '华南大区', type: 'areaCenterCity'},
            {name: '成都', area: '华西大区', type: 'areaCenterCity'},
            {name: '北京', area: '华北大区', type: 'areaCenterCity'},
            {name: '武汉', area: '华中大区', type: 'areaCenterCity'},
            {name: '沈阳', area: '东北大区', type: 'areaCenterCity'},
        ];
        echarts.registerMap('zhongguo', geoJson);
        for(let item of provienceData){
            if(item.area === '东北大区'){
                item.itemStyle = {
                    normal: {
                        areaColor: "#3CA2FC",
                    },
                    emphasis: {
                        areaColor: "#3CA2FC",
                    }
                }
            }else 
function getGzMap(_data) { if (_chinaMap == undefined) { var dom = document.getElementById("container"); _chinaMap = echarts.init(dom); _chinaMap.on('click', function(params) { console.log(params); var _type = params.seriesType; if (_type == "map") { //window.parent.aaa('aa') //调用父页面方法 } else if (_type == "effectScatter") { window.parent.showMap(); } }) } var option = { backgroundColor: 'rgba(0,0,0,0)', visualMap: { type: 'piecewise', show: false, min: 0, max: 300, splitNumber: 3, itemWidth: 10, itemHeight: 10, itemGap: 5, seriesIndex: [1], pieces: [ { min: 0, max: 100, label: '优' }, { min: 101, max: 200, label: '良' }, { min: 201, max: 300, label: '高风险' } ], //color: ['#FA4D08', '#4BD94F', '#FBD32B'], //红、绿、黄 color: ['#F8DAE6', '#FEF9B5', '#B0D8B3'], //红、黄、绿 textStyle: { color: '#9EA8B1', fontSize: 10 } }, tooltip: { formatter: '{b}' }, geo: { map: 'guangdong', roam: true, aspectScale: 1, zoom: 1.5, layoutCenter: ['55%', '40%'], layoutSize: 500, label: { normal: { show: true }, emphasis: { show: true } }, itemStyle: { normal: { areaColor: '#323c48', borderColor: '#111', borderColor: '#3BB4DF', shadowColor: '#25A3FC', shadowBlur: 10 }, emphasis: { areaColor: '#ddb926' } } }, series: [{ type: 'effectScatter', coordinateSystem: 'geo', data: unitData, symbolSize: 10, symbol: 'image://../../../../Content/images/One/fire.png', //symbolRotate: 35, rippleEffect: { period: 4, scale: 5, brushType: 'fill', }, label: { normal: { formatter: '{b}', position: 'right', show: false }, emphasis: { show: false } }, itemStyle: { normal: { color: '#fff' } } }, { name: '', type: 'map', geoIndex: 0, mapType: 'guangdong', // 自定义扩展图表类型 label: { normal: { show: true, } }, itemStyle: { normal: { label: { show: true, fontSize: 10, color: '#111' }, shadowColor: '#ddb926', shadowBlur: 5, }, emphasis: { label: { show: true }, shadowColor: 'rgba(0, 0, 0, 0.5)', shadowBlur: 10 } }, data: _data }, { type: 'effectScatter', coordinateSystem: 'geo', data: windData, symbolSize: 10, symbol: 'image://../../../../Content/images/One/wind.png', //symbolRotate: 35, rippleEffect: { period: 4, scale: 5, brushType: 'fill', }, label: { normal: { formatter: '{b}', position: 'right', show: false }, emphasis: { show: false } }, itemStyle: { normal: { color: '#fff' } } }, ] }; $.getJSON('../../MapCN/guangdong.json', function(chinaJson) { echarts.registerMap('guangdong', chinaJson); _chinaMap.setOption(option, true); }); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值