react中使用ECharts绘制各省市地图

  1. 首先,安装对应依赖
npm install echarts --save

2.引入

import React from "react";
// 模块化样式表
import s from "./style.css";
import echarts from 'echarts/lib/echarts';
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';
import 'echarts/lib/component/visualMap';
import 'echarts/lib/chart/map';
import 'echarts/lib/chart/bar';
import 'echarts/map/js/china';
import 'echarts/lib/chart/line';
import 'echarts/lib/chart/gauge';
import 'echarts/lib/chart/pie';
//json地图数据文件,各省市坐标获取方法见文末
var uploadedDataURL = "/asset/get/s/data-1593671529129-u-DcAWe3h.json";

2.创建一个区域快,区域快必须有宽高


var points = [{
    name: '供应商A',
    value: [115.974519, 40.457009]
}, {
    name: '供应商B',
    value: [116.143267, 39.749144]
}, {
    name: 'BossSoft',
    value: [116.843177, 40.376834]
}]

var colors = ['#FF8C00', '#00acea', '#1DE9B6'];

export default class jinNanMapComponent extends BaseComponent {
//生命周期钩子函数
    componentDidMount() {
        this.chart0()
    }
    // 将echarts实例地图代码放到一个函数中,该函数在生命周期中调用
    chart0(){
    //获取展示块的id,展示块必须有宽高
        var myChart = echarts.init(document.getElementById('main'));
        // 第二个参数为导入地图文件
        echarts.registerMap('china', uploadedDataURL);
       let option = {
            //背景色
            backgroundColor: '#fff',
            color: ['#FF8C00', '#00acea', '#1DE9B6'],
            tooltip: {
                trigger: 'item',
                formatter: '{b}'
            },
            legend: {
                orient: 'vertical',
                x: 'left',
                data: ['供应商A', '供应商B'],
                textStyle: {
                    color: '#fff'
                }
            },
            visualMap: {
                type: 'continuous',
                seriesIndex: [2, 3],
                min: 0,
                max: 100,
                calculable: true,
                color: ['#ff3333', 'orange', 'yellow', 'lime', 'aqua'],
                textStyle: {
                    color: '#fff'
                }
            },
            geo: {
                map: 'china',
                show: false
            },
            series: [{
                type: 'map',
                map: 'china',
                aspectScale: 0.75,
                itemStyle: {
                    borderColor: 'rgba(100,149,237,1)',
                    borderWidth: 0.5,
                    areaColor: 'rgba(0,0,0,0)',
                },
                label: {
                    show: true,
                    textStyle: {
                        color: '#fff'
                    },
                    emphasis: {
                        textStyle: {
                            color: '#fff'
                        }
                    }
                },
                emphasis: {
                    label: {
                        show: false
                    },
                    itemStyle: {
                        areaColor: 'rgba(0,0,0,0)',
                    }
                }

            }, {
                type: 'effectScatter',
                coordinateSystem: 'geo',
                showEffectOn: 'render',
                rippleEffect: {
                    period: 5,
                    scale: 5,
                    brushType: 'fill'
                },
                hoverAnimation: true,
                label: {
                    formatter: '{b}',
                    position: 'right',
                    offset: [15, 0],
                    color: param => colors[param.dataIndex % colors.length],
                    show: true
                },
                itemStyle: {
                    color: (param) => {
                        return colors[param.dataIndex % colors.length];
                    },
                    shadowBlur: 10,
                    shadowColor: '#333',
                    opacity: 0.75
                },
                emphasis: {
                    itemStyle: {
                        opacity: 1, //线条宽度
                    }
                },
                data: points
            }, {
                name: '供应商A',
                type: 'lines',
                zlevel: 2,
                symbol: ['none', 'arrow'],
                symbolSize: 7,
                effect: {
                    show: true,
                    period: 4,
                    trailLength: 0.02,
                    symbol: 'circle',
                    symbolSize: 4,
                    color: '#fff'
                },
                lineStyle: {
                    width: 0.5, //线条宽度
                    opacity: 0.5, //尾迹线条透明度
                    curveness: .3, //尾迹线条曲直度
                    shadowBlur: 10,
                },
                emphasis: {
                    lineStyle: {
                        width: 2, //线条宽度
                    }
                },
                data: [{
                    name: '供应商A->BossSoft',
                    value: 40,
                    coords: [
                        [38.936325, 117.402547],
                        [38.936325, 120.402547]
                    ],
                }]
            }, {
                name: '供应商B',
                type: 'lines',
                zlevel: 2,
                symbol: ['none', 'arrow'],
                symbolSize: 7,
                effect: {
                    show: true,
                    period: 4,
                    trailLength: 0.02,
                    symbol: 'circle',
                    symbolSize: 4,
                    color: '#fff'
                },
                lineStyle: {
                    width: 0.5, //线条宽度
                    opacity: 0.5, //尾迹线条透明度
                    curveness: 0.3, //尾迹线条曲直度
                    shadowBlur: 10,
                },
                emphasis: {
                    lineStyle: {
                        width: 2, //线条宽度
                    }
                },
                data: [{
                    name: '供应商B->BossSoft',
                    value: 80,
                    coords: [
                        [38.936325, 117.402547],
                        [38.936325, 118.402547]
                    ]
                }]
            }]
        };
        myChart.setOption(option);
    }

    render() {
        let {data, timestamp} = this.props;
        this.scrollRow = 1;
        return (
            <div className={s.container}>
                <div className={s.title}>分布地</div>
                <div className={s.mapbox} id="main"></div>
            </div>
        )
    }

}

3.获取各省市地图轮框坐标json文件方法:

echarts.registerMap('china', uploadedDataURL);

总结:实现了在react中绘制echarts地图的方法
文末彩蛋,附一个echarts图表社区
里面有大量用户制作的精美图表,安利给大家

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值