欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

echarts实现地图

程序员文章站 2022-07-02 15:29:50
文章以河南省为例 一、先下载eacherts相关js文件(echarts.min.js)和echarts使用到的地图插件(map) ecarts.min.js在echarts官网下载,map插件下载地址:https://github.com/zhxiangfei/echarts-map (包含全国、 ......

文章以河南省为例

一、先下载eacherts相关js文件(echarts.min.js)和echarts使用到的地图插件(map)

ecarts.min.js在下载,map插件下载地址: (包含全国、各省城市)

二、代码

<script type="text/javascript" src="/home/js/jquery.js"></script>
<script type="text/javascript" src="/home/js/echarts.min.js"></script>
<-- 我这里用的是河南地图,可以根据自己需要选择城市-->
<script type="text/javascript" src="/home/js/map/js/province/henan.js"></script>

<script>
    <--地址换成从github下载的文件路径-->
    var uploadeddataurl = "/home/js/map/json/province/henan.json";

    mychart.showloading();
    $.getjson(uploadeddataurl, function(geojson) {
        echarts.registermap('henan', geojson);
        mychart.hideloading();
        var geocoordmap = {
            '兰考': [114.385481, 34.825227],
            '郑州市': [113.4668, 34.6234],
            '开封市': [114.311523, 34.809969],
            '洛阳市': [112.471793, 34.62387],
            '平顶山市': [113.198486, 33.775454],
            '安阳市': [114.39891, 36.108225],
            '鹤壁市': [114.306924, 35.752846],
            '新乡市': [113.938978, 35.309189],
            '焦作市': [113.249079, 35.222425],
            '濮阳市': [115.038216, 35.764093],
            '许昌市': [113.860789, 34.040073],
            '漯河市': [114.021765, 33.590883],
            '三门峡市': [111.179383, 34.783409],
            '南阳市': [112.531584, 33.019279],
            '商丘市': [115.659125, 34.44116],
            '信阳市': [114.095355, 32.146995],
            '周口市': [114.693267, 33.644758],
            '驻马店市': [114.030964, 33.034777],
            '济源市': [112.623571, 35.078872],
        };
        var moveline = {
            'normal': [
                {"fromname": "兰考","toname": "郑州市",'coords':[[114.385481, 34.825227],[113.4668, 34.6234]]},
                {"fromname": "兰考","toname": "开封市",'coords': [[114.385481, 34.825227],[114.311523, 34.809969]]},
                {"fromname": "兰考","toname": "洛阳市",'coords': [[114.385481, 34.825227],[112.471793, 34.62387]]},
                {"fromname": "兰考","toname": "平顶山市",'coords': [[114.385481, 34.825227],[113.198486, 33.775454]]},
                {"fromname": "兰考","toname": "安阳市",'coords': [[114.385481, 34.825227],[114.39891, 36.108225]]},
                {"fromname": "兰考","toname": "鹤壁市",'coords': [[114.385481, 34.825227],[114.306924, 35.752846]]},
                {"fromname": "兰考","toname": "新乡市",'coords': [[114.385481, 34.825227],[113.938978, 35.309189]]},
                {"fromname": "兰考","toname": "焦作市",'coords': [[114.385481, 34.825227],[113.249079, 35.222425]]},
                {"fromname": "兰考","toname": "濮阳市",'coords': [[114.385481, 34.825227],[115.038216, 35.764093]]},
                {"fromname": "兰考","toname": "许昌市",'coords': [[114.385481, 34.825227],[113.860789, 34.040073]]},
                {"fromname": "兰考","toname": "漯河市",'coords': [[114.385481, 34.825227],[114.021765, 33.590883]]},
                {"fromname": "兰考","toname": "三门峡市",'coords': [[114.385481, 34.825227],[111.179383, 34.783409]]},
                {"fromname": "兰考","toname": "南阳市",'coords': [[114.385481, 34.825227],[112.531584, 33.019279]]},
                {"fromname": "兰考","toname": "商丘市",'coords': [[114.385481, 34.825227],[115.659125, 34.44116]]},
                {"fromname": "兰考","toname": "信阳市",'coords': [[114.385481, 34.825227],[114.095355, 32.146995]]},
                {"fromname": "兰考","toname": "周口市",'coords': [[114.385481, 34.825227],[114.693267, 33.644758]]},
                {"fromname": "兰考","toname": "驻马店市",'coords': [[114.385481, 34.825227],[114.030964, 33.034777]]},
                {"fromname": "兰考","toname": "济源市",'coords': [[114.385481, 34.825227],[112.623571, 35.078872]]},
            ],
            'warning': [

            ]
        };
        var data = [{
                name: '郑州市',
                value: 190
            },
            {
                name: '开封市',
                value: 190
            },
            {
                name: '洛阳市',
                value: 190
            },
            {
                name: '平顶山市',
                value: 190
            },
            {
                name: '安阳市',
                value: 190
            },
            {
                name: '鹤壁市',
                value: 90
            },
            {
                name: '新乡市',
                value: 120
            },
            {
                name: '焦作市',
                value: 120
            },
            {
                name: '濮阳市',
                value: 120
            },
            {
                name: '许昌市',
                value: 120
            },
            {
                name: '漯河市',
                value: 190
            },
            {
                name: '三门峡市',
                value: 190
            },
            {
                name: '南阳市',
                value: 190
            },
            {
                name: '商丘市',
                value: 190
            },
            {
                name: '信阳市',
                value: 190
            },
            {
                name: '周口市',
                value: 190
            },
            {
                name: '驻马店市',
                value: 90
            },
            {
                name: '济源市',
                value: 90
            },
        ];
        var max = 480,
            min = 9; // todo 
        var maxsize4pin = 100,
            minsize4pin = 20;

        var convertdata = function(data) {
            var res = [];
            for (var i = 0; i < data.length; i++) {
                var geocoord = geocoordmap[data[i].name];
                if (geocoord) {
                    res.push({
                        name: data[i].name,
                        value: geocoord.concat(data[i].value)
                    });
                }
            }
            return res;
        };
        

        option = {
            backgroundcolor: '#091c3d',
            // title: {
            //     top: 20,
            //     text: '',
            //     subtext: '',
            //     x: 'center',
            //     textstyle: {
            //         color: '#ccc'
            //     }
            // },
            title: {
                text: '河南省中医院分布图',
                x: 'center',
                textstyle: {
                    color: '#fff'
                }
            },
            tooltip: {
                trigger: 'item',
                formatter: function(params) {
                    console.log(params.value)
                    // if (typeof(params.value)[2] == "undefined") {
                    //     return params.name + ' : ' + params.value;
                    // } else {
                        return params.name + ' : ' + params.value[2];
                    // }
                }
            },
            legend: {
                orient: 'vertical',
                y: 'bottom',
                x: 'right',
                data: ['pm2.5'],
                textstyle: {
                    color: '#fff'
                }
            },
            visualmap: {
                show: false,
                min: 0,
                max: 500,
                left: 'left',
                top: 'bottom',
                text: ['高', '低'], // 文本,默认为数值文本
                calculable: true,
                seriesindex: [1],
                inrange: {
                    // color: ['#9074c3', '#4487d5', '#7e9e8e', '#51a2c2']
                    // color: ['#3b5077', '#031525'] // 蓝黑
                    // color: ['#ffc0cb', '#800080'] // 红紫
                    // color: ['#3c3b3f', '#605c3c'] // 黑绿
                    //color: ['#0f0c29', '#302b63', '#24243e'] // 黑紫黑
                    //color: ['#23074d', '#cc5333'] // 紫红
                    // color: ['#00467f', '#a5cc82'] // 蓝绿
                    // color: ['#1488cc', '#2b32b2'] // 浅蓝
                    // color: ['#00467f', '#a5cc82'] // 蓝绿

                }
            },
           
            geo: {
                show: true,
                map: 'henan',
                label: {
                    normal: {
                        show: false
                    },
                    emphasis: {
                        show: false,
                    }
                },
                roam: true,
                itemstyle: {
                    normal: {
                        areacolor: 'transparent',
                        bordercolor: '#3fdaff',
                        borderwidth: 2,
                        shadowcolor: 'rgba(63, 218, 255, 0.5)',
                        shadowblur: 30
                    },
                    emphasis: {
                        areacolor: '#2b91b7',
                    }
                }
            },
            series: [
                
                {
                    type: 'map',
                    map: 'henan',
                    geoindex: 0,
                    aspectscale: 0.75, //长宽比
                    showlegendsymbol: false, // 存在legend时显示
                    label: {
                        normal: {
                            show: true
                        },
                        emphasis: {
                            show: false,
                            textstyle: {
                                color: '#fff'
                            }
                        }
                    },
                    roam: true,
                    itemstyle: {
                        normal: {
                            areacolor: '#031525',
                            bordercolor: '#ffffff',
                        },
                        emphasis: {
                            areacolor: '#2b91b7'
                        }
                    },
                    animation: false,
                    data: data
                },
                
                {
                    name:  '城市',
                    type: 'effectscatter',
                    coordinatesystem: 'geo',
                    zlevel: 1,
                    showeffecton: 'render',
                    rippleeffect: {
                        scale: 3,
                        brushtype: 'stroke'
                    },
                    hoveranimation: true,   //鼠标移上去显示数据
                    label: {
                        normal: {
                            show: true,
                            position: 'right',
                            formatter: '{b}'
                        },
                        emphasis: {
                            show: true
                        }
                    },
                    symbolsize: function (val) {
                        return val[2] / 10;
                    },
                    itemstyle: {
                        normal: {
                            color: '#f4e925',
                            shadowblur: 10,
                            shadowcolor: '#05c3f9'
                        }
                    },
                    data: convertdata(data),
                },
        
                // 射线样式
                {
                    name: '线路',
                    type: 'lines',
                    coordinatesystem: 'geo',
                    zlevel: 2,  //数值越大越在上面
                    large: true,
                    symbol: ['none', 'arrow'],   
                    effect: {
                        show: true,
                        period: 6,
                        constantspeed: 30,
                        traillength: 0,
                        symbolsize:3    //圆点大小
                    },
                    linestyle: {
                        normal: {
                            // 线条颜色
                            color:'#0fff17',

                            width: 0.5,
                            opacity: 0.6,
                            curveness: 0.2
                        }
                    },
                    data: moveline.normal
                },
                // 射线样式
                {
                    name:  '线路',
                    type: 'lines',
                    coordinatesystem: 'geo',
                    zlevel: 3,
                    large: true,
                    effect: {
                        show: true,
                        period: 5,
                        traillength: 0.8,   //特效尾迹的长度
                        color: '#fff',
                        symbolsize: 3   //圆点大小
                    },
                    linestyle: {
                        normal: {
                            // color:'#0fff17',
                            color: new echarts.graphic.lineargradient(0, 0, 0, 1, [{
                                offset: 0,
                                color: '#0fff17'
                            }, {
                                offset: 1,
                                color: '#f4e925'
                            }], false),
                            width: 0.2,
                            curveness: 0.2
                        }
                    },
                    data: moveline.normal
                }

            ]
        };
        mychart.setoption(option);
    });
</script>

三、效果图

echarts实现地图