美文网首页
Echarts中国地图(数据纯属虚构)

Echarts中国地图(数据纯属虚构)

作者: 小睿同学 | 来源:发表于2019-08-20 17:23 被阅读0次

    一:引包

    <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts.min.js"></script>
        <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts-gl/echarts-gl.min.js"></script>
        <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts-stat/ecStat.min.js"></script>
        <script type="text/javascript"
                src="http://echarts.baidu.com/gallery/vendors/echarts/extension/dataTool.min.js"></script>
        <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/map/js/china.js"></script>
        <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/map/js/world.js"></script>
        <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=ZUONbpqGBsYGXNIYHicvbAbM"></script>
        <script type="text/javascript"
                src="http://echarts.baidu.com/gallery/vendors/echarts/extension/bmap.min.js"></script>
        <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/simplex.js"></script>
    

    二:一个div,用于放图表

    <body >
    <div id="container" style="width: 80%;height: 700px"></div>
    </body>
    

    三:初始化echarts

    var dom = document.getElementById("container");
    var myChart = echarts.init(dom);
    
    两句可以合成一句(个人比较喜欢)
    var myChart = echarts.init(document.getElementById('container'));
    

    四:指定图表的配置项和数据

    option = {
            title: {
                text: '中国地图',
                subtext: '数据纯属虚构',
                left: 'center'
            },
            tooltip: {
                trigger: 'item'
            },
    
            //左侧小导航图标
            visualMap: {
                min: 0,
                max: 2500,
                left: 'left',
                top: 'bottom',
                text: ['高', '低'],
                calculable: true
            },
            //右侧小导航
            toolbox: {
                show: true,
                orient: 'vertical',
                left: 'right',
                top: 'center',
                feature: {
                    mark: {show: true},
                    dataView: {show: true, readOnly: false},
                    restore: {show: true},
                    saveAsImage: {show: true}
                },
            },
            series: [
                {
                    name: '中国',
                    type: 'map',
                    mapType: 'china',
                    roam: true,
                    label: {
                        normal: {
                            show:true,//显示省份
                        },
                        emphasis: {
                            show: false
                        }
                    },
                    data: [
                        {name: '北京', value: Math.round(Math.random() * 1000)},
                        {name: '天津', value: Math.round(Math.random() * 1000)},
                        {name: '上海', value: Math.round(Math.random() * 1000)},
                        {name: '重庆', value: Math.round(Math.random() * 1000)},
                        {name: '河北', value: Math.round(Math.random() * 1000)},
                        {name: '河南', value: Math.round(Math.random() * 1000)},
                        {name: '云南', value: Math.round(Math.random() * 1000)},
                        {name: '辽宁', value: Math.round(Math.random() * 1000)},
                        {name: '黑龙江', value: Math.round(Math.random() * 1000)},
                        {name: '湖南', value: Math.round(Math.random() * 1000)},
                        {name: '安徽', value: Math.round(Math.random() * 1000)},
                        {name: '山东', value: Math.round(Math.random() * 1000)},
                        {name: '新疆', value: Math.round(Math.random() * 1000)},
                        {name: '江苏', value: Math.round(Math.random() * 1000)},
                        {name: '浙江', value: Math.round(Math.random() * 1000)},
                        {name: '江西', value: Math.round(Math.random() * 1000)},
                        {name: '湖北', value: Math.round(Math.random() * 1000)},
                        {name: '广西', value: Math.round(Math.random() * 1000)},
                        {name: '甘肃', value: Math.round(Math.random() * 1000)},
                        {name: '山西', value: Math.round(Math.random() * 1000)},
                        {name: '内蒙古', value: Math.round(Math.random() * 1000)},
                        {name: '陕西', value: Math.round(Math.random() * 1000)},
                        {name: '吉林', value: Math.round(Math.random() * 1000)},
                        {name: '福建', value: Math.round(Math.random() * 1000)},
                        {name: '贵州', value: Math.round(Math.random() * 1000)},
                        {name: '广东', value: Math.round(Math.random() * 1000)},
                        {name: '青海', value: Math.round(Math.random() * 1000)},
                        {name: '西藏', value: Math.round(Math.random() * 1000)},
                        {name: '四川', value: Math.round(Math.random() * 1000)},
                        {name: '宁夏', value: Math.round(Math.random() * 1000)},
                        {name: '海南', value: Math.round(Math.random() * 1000)},
                        {name: '台湾', value: Math.round(Math.random() * 1000)},
                        {name: '香港', value: Math.round(Math.random() * 1000)},
                        {name: '澳门', value: Math.round(Math.random() * 1000)}
                    ]
                }
            ]
        };
    

    五: 使用刚指定的配置项和数据显示图表。

    myChart.setOption(option);
    

    完整代码如下

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts.min.js"></script>
        <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts-gl/echarts-gl.min.js"></script>
        <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts-stat/ecStat.min.js"></script>
        <script type="text/javascript"
                src="http://echarts.baidu.com/gallery/vendors/echarts/extension/dataTool.min.js"></script>
        <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/map/js/china.js"></script>
        <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/map/js/world.js"></script>
        <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=ZUONbpqGBsYGXNIYHicvbAbM"></script>
        <script type="text/javascript"
                src="http://echarts.baidu.com/gallery/vendors/echarts/extension/bmap.min.js"></script>
        <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/simplex.js"></script>
    </head>
    <body >
    <div id="container" style="width: 80%;height: 700px"></div>
    </body>
    
    <script type="text/javascript">
        var myChart = echarts.init(document.getElementById('container'));
        //var dom = document.getElementById("container");
        //var myChart = echarts.init(dom);
    
        option = {
            title: {
                text: '中国地图',
                subtext: '数据纯属虚构',
                left: 'center'
            },
            tooltip: {
                trigger: 'item'
            },
    
            //左侧小导航图标
            visualMap: {
                min: 0,
                max: 2500,
                left: 'left',
                top: 'bottom',
                text: ['高', '低'],
                calculable: true
            },
            //右侧小导航
            toolbox: {
                show: true,
                orient: 'vertical',
                left: 'right',
                top: 'center',
                feature: {
                    mark: {show: true},
                    dataView: {show: true, readOnly: false},
                    restore: {show: true},
                    saveAsImage: {show: true}
                },
            },
            series: [
                {
                    name: '中国',
                    type: 'map',
                    mapType: 'china',
                    roam: true,
                    label: {
                        normal: {
                            show:true,//显示省份
                        },
                        emphasis: {
                            show: false
                        }
                    },
                    data: [
                        {name: '北京', value: Math.round(Math.random() * 1000)},
                        {name: '天津', value: Math.round(Math.random() * 1000)},
                        {name: '上海', value: Math.round(Math.random() * 1000)},
                        {name: '重庆', value: Math.round(Math.random() * 1000)},
                        {name: '河北', value: Math.round(Math.random() * 1000)},
                        {name: '河南', value: Math.round(Math.random() * 1000)},
                        {name: '云南', value: Math.round(Math.random() * 1000)},
                        {name: '辽宁', value: Math.round(Math.random() * 1000)},
                        {name: '黑龙江', value: Math.round(Math.random() * 1000)},
                        {name: '湖南', value: Math.round(Math.random() * 1000)},
                        {name: '安徽', value: Math.round(Math.random() * 1000)},
                        {name: '山东', value: Math.round(Math.random() * 1000)},
                        {name: '新疆', value: Math.round(Math.random() * 1000)},
                        {name: '江苏', value: Math.round(Math.random() * 1000)},
                        {name: '浙江', value: Math.round(Math.random() * 1000)},
                        {name: '江西', value: Math.round(Math.random() * 1000)},
                        {name: '湖北', value: Math.round(Math.random() * 1000)},
                        {name: '广西', value: Math.round(Math.random() * 1000)},
                        {name: '甘肃', value: Math.round(Math.random() * 1000)},
                        {name: '山西', value: Math.round(Math.random() * 1000)},
                        {name: '内蒙古', value: Math.round(Math.random() * 1000)},
                        {name: '陕西', value: Math.round(Math.random() * 1000)},
                        {name: '吉林', value: Math.round(Math.random() * 1000)},
                        {name: '福建', value: Math.round(Math.random() * 1000)},
                        {name: '贵州', value: Math.round(Math.random() * 1000)},
                        {name: '广东', value: Math.round(Math.random() * 1000)},
                        {name: '青海', value: Math.round(Math.random() * 1000)},
                        {name: '西藏', value: Math.round(Math.random() * 1000)},
                        {name: '四川', value: Math.round(Math.random() * 1000)},
                        {name: '宁夏', value: Math.round(Math.random() * 1000)},
                        {name: '海南', value: Math.round(Math.random() * 1000)},
                        {name: '台湾', value: Math.round(Math.random() * 1000)},
                        {name: '香港', value: Math.round(Math.random() * 1000)},
                        {name: '澳门', value: Math.round(Math.random() * 1000)}
                    ]
                }
            ]
        };
    
        //if (option && typeof option === "object") {
            myChart.setOption(option);
        //}
    </script>
    <style>
    
    </style>
    </html>
    

    附上图片

    image.png

    相关文章

      网友评论

          本文标题:Echarts中国地图(数据纯属虚构)

          本文链接:https://www.haomeiwen.com/subject/nwapsctx.html