教你轻松上手,echarts地图图表实操案例大解析

2026-10-08 0 阅读

了解ECharts地图图表

ECharts是一款使用JavaScript实现的开源可视化库,它提供了丰富的图表类型,其中地图图表是其中一种非常实用的类型。地图图表可以将数据与地理位置结合起来,直观地展示地域分布和地理趋势。在本文中,我们将通过一些实操案例来学习如何使用ECharts制作地图图表。

准备工作

在开始之前,请确保你已经安装了ECharts库。可以通过以下代码来引入ECharts:

<script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.3.2/echarts.min.js"></script>

实操案例一:基本地图展示

以下是一个简单的地图图表示例,展示了中国地图的基本结构:

<!DOCTYPE html>
<html style="height: 100%">
<head>
    <meta charset="utf-8">
</head>
<body style="height: 100%; margin: 0">
<div id="container" style="height: 100%"></div>
<script type="text/javascript">
    var myChart = echarts.init(document.getElementById('container'));

    var option = {
        title: {
            text: '中国地图示例'
        },
        tooltip: {},
        visualMap: {
            min: 0,
            max: 100,
            left: 'left',
            top: 'bottom',
            text: ['高','低'],           // 文本,默认为数值文本
            calculable: true
        },
        series: [{
            name: '销量',
            type: 'map',
            mapType: 'china',
            roam: false,
            label: {
                show: true
            },
            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);
</script>
</body>
</html>

在这个例子中,我们创建了一个中国地图的基本结构,并且随机生成了一些数据来表示不同省份的“销量”。

实操案例二:交互式地图

以下是一个交互式地图的示例,用户可以通过鼠标悬停和点击来查看更多信息:

<!DOCTYPE html>
<html style="height: 100%">
<head>
    <meta charset="utf-8">
</head>
<body style="height: 100%; margin: 0">
<div id="container" style="height: 100%"></div>
<script type="text/javascript">
    var myChart = echarts.init(document.getElementById('container'));

    var option = {
        title: {
            text: '交互式地图示例'
        },
        tooltip: {
            trigger: 'item',
            formatter: '{b}<br/>{c}'
        },
        visualMap: {
            min: 0,
            max: 100,
            left: 'left',
            top: 'bottom',
            text: ['高','低'],
            calculable: true
        },
        series: [{
            name: '销量',
            type: 'map',
            mapType: 'china',
            roam: true,
            label: {
                show: true
            },
            data: [
                {name: '北京',value: Math.round(Math.random() * 1000)},
                {name: '天津',value: Math.round(Math.random() * 1000)},
                {name: '上海',value: Math.round(Math.random() * 1000)},
                // ... 其他省份数据
            ]
        }]
    };

    myChart.setOption(option);
</script>
</body>
</html>

在这个例子中,我们添加了roam: true属性,使得地图可以自由缩放和平移。同时,我们为tooltip添加了formatter函数,用于自定义提示框的显示内容。

实操案例三:地图数据动态更新

以下是一个地图数据动态更新的示例,通过定时器来模拟数据的实时变化:

<!DOCTYPE html>
<html style="height: 100%">
<head>
    <meta charset="utf-8">
</head>
<body style="height: 100%; margin: 0">
<div id="container" style="height: 100%"></div>
<script type="text/javascript">
    var myChart = echarts.init(document.getElementById('container'));

    var option = {
        title: {
            text: '地图数据动态更新示例'
        },
        tooltip: {
            trigger: 'item',
            formatter: '{b}<br/>{c}'
        },
        visualMap: {
            min: 0,
            max: 100,
            left: 'left',
            top: 'bottom',
            text: ['高','低'],
            calculable: true
        },
        series: [{
            name: '销量',
            type: 'map',
            mapType: 'china',
            roam: true,
            label: {
                show: true
            },
            data: [
                {name: '北京',value: Math.round(Math.random() * 1000)},
                // ... 其他省份数据
            ]
        }]
    };

    myChart.setOption(option);

    setInterval(function () {
        option.series[0].data.forEach(function (item) {
            item.value = Math.round(Math.random() * 1000);
        });
        myChart.setOption(option);
    }, 1000);
</script>
</body>
</html>

在这个例子中,我们使用setInterval函数来每隔一秒钟更新一次地图数据。这样,地图上的数据就会不断变化,模拟出数据的动态更新效果。

总结

通过以上三个实操案例,我们可以了解到如何使用ECharts制作地图图表。在实际应用中,你可以根据自己的需求对地图进行定制化设计,例如添加自定义的地图样式、数据交互等功能。希望这些案例能够帮助你快速上手ECharts地图图表的制作。

分享到: