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

   日常项目中经常会用到百度地图或者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</
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要在项目中引入 echarts 和百度地图的 JS 库。可以通过 npm 安装,也可以直接在 html 中引入。 然后,在组件中引入 echarts 和百度地图的 JS 库,并创建一个 div 作为容器,用于存放 echarts 和地图。 ```jsx import React, { useEffect, useRef } from 'react'; import echarts from 'echarts'; import 'echarts-gl/dist/echarts-gl'; import 'echarts/map/js/world'; import BMap from 'BMap'; const MapChart = ({ data }) => { const chartRef = useRef(null); useEffect(() => { if (chartRef.current) { const myChart = echarts.init(chartRef.current); const option = { tooltip: { trigger: 'item', formatter: '{b}' }, series: [ { type: 'map3d', map: 'world', roam: true, itemStyle: { areaColor: '#fff', borderColor: '#333' }, label: { show: true }, data: [] } ] }; myChart.setOption(option); const map = new BMap.Map(chartRef.current); map.centerAndZoom(new BMap.Point(116.404, 39.915), 11); map.enableScrollWheelZoom(true); const overlay = new BMap.Overlay(); overlay.initialize = function (map) { this._map = map; const div = document.createElement('div'); div.style.position = 'absolute'; div.style.width = '10px'; div.style.height = '10px'; div.style.background = '#f00'; map.getPanes().labelPane.appendChild(div); this._div = div; return div; }; overlay.draw = function () { const position = map.pointToOverlayPixel(new BMap.Point(116.404, 39.915)); this._div.style.left = position.x + 'px'; this._div.style.top = position.y + 'px'; }; map.addOverlay(overlay); } }, [data]); return <div ref={chartRef} style={{ width: '100%', height: '600px' }} />; }; export default MapChart; ``` 在 useEffect 中,先初始化 echarts 实例,然后创建地图,并将 echarts 实例与地图容器绑定。然后,初始化地图,并添加一个自定义的覆盖物。 这里的 data 是从 props 中传递过来的,可以根据需要进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值