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

2026-07-08 0 阅读

在数据可视化领域,地图图表因其直观、生动的特点而受到广泛关注。ECharts作为国内流行的开源图表库,提供了丰富的地图图表绘制功能。本文将带你轻松上手ECharts地图图表,通过实操案例全解析,让你快速掌握其使用方法。

一、ECharts地图图表简介

ECharts地图图表是ECharts图表库中的一个重要组成部分,它可以展示地理信息数据,帮助用户更好地理解和分析地理空间分布特征。ECharts地图图表支持多种类型的地图,包括世界地图、中国地图、省市地图等,并提供丰富的配置项和自定义功能。

二、ECharts地图图表实操案例

1. 基础案例:世界地图

以下是一个使用ECharts绘制世界地图的基础案例:

<!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 src="https://cdn.bootcdn.net/ajax/libs/echarts/5.3.2/echarts.min.js"></script>
<script type="text/javascript">
    var myChart = echarts.init(document.getElementById('container'));

    var option = {
        title: {
            text: '世界地图'
        },
        tooltip: {},
        visualMap: {
            min: 0,
            max: 1000,
            left: 'left',
            top: 'bottom',
            text: ['高','低'],           // 文本,默认为数值文本
            calculable: true
        },
        geo: {
            map: 'world',
            roam: true,
            label: {
                emphasis: {
                    show: false
                }
            },
            itemStyle: {
                normal: {
                    areaColor: '#323c48',
                    borderColor: '#111'
                },
                emphasis: {
                    areaColor: '#2a333d'
                }
            }
        },
        series: [
            {
                name: '数据',
                type: 'map',
                mapType: 'world',
                data: [
                    {name: '印度尼西亚', value: Math.round(Math.random() * 1000)},
                    {name: '美国', value: Math.round(Math.random() * 1000)}
                ],
                label: {
                    normal: {
                        show: false
                    },
                    emphasis: {
                        show: true
                    }
                },
                itemStyle: {
                    emphasis: {
                        label: {
                            show: true
                        }
                    }
                }
            }
        ]
    };

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

2. 进阶案例:中国地图

以下是一个使用ECharts绘制中国地图的进阶案例:

<!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 src="https://cdn.bootcdn.net/ajax/libs/echarts/5.3.2/echarts.min.js"></script>
<script type="text/javascript">
    var myChart = echarts.init(document.getElementById('container'));

    var option = {
        title: {
            text: '中国地图'
        },
        tooltip: {},
        visualMap: {
            min: 0,
            max: 1000,
            left: 'left',
            top: 'bottom',
            text: ['高','低'],           // 文本,默认为数值文本
            calculable: true
        },
        geo: {
            map: 'china',
            roam: true,
            label: {
                emphasis: {
                    show: false
                }
            },
            itemStyle: {
                normal: {
                    areaColor: '#323c48',
                    borderColor: '#111'
                },
                emphasis: {
                    areaColor: '#2a333d'
                }
            }
        },
        series: [
            {
                name: '数据',
                type: 'map',
                mapType: 'china',
                data: [
                    {name: '广东', value: Math.round(Math.random() * 1000)},
                    {name: '北京', value: Math.round(Math.random() * 1000)}
                ],
                label: {
                    normal: {
                        show: false
                    },
                    emphasis: {
                        show: true
                    }
                },
                itemStyle: {
                    emphasis: {
                        label: {
                            show: true
                        }
                    }
                }
            }
        ]
    };

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

3. 高级案例:省市地图

以下是一个使用ECharts绘制省市地图的高级案例:

<!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 src="https://cdn.bootcdn.net/ajax/libs/echarts/5.3.2/echarts.min.js"></script>
<script type="text/javascript">
    var myChart = echarts.init(document.getElementById('container'));

    var option = {
        title: {
            text: '广东省地图'
        },
        tooltip: {},
        visualMap: {
            min: 0,
            max: 1000,
            left: 'left',
            top: 'bottom',
            text: ['高','低'],           // 文本,默认为数值文本
            calculable: true
        },
        geo: {
            map: 'guangdong',
            roam: true,
            label: {
                emphasis: {
                    show: false
                }
            },
            itemStyle: {
                normal: {
                    areaColor: '#323c48',
                    borderColor: '#111'
                },
                emphasis: {
                    areaColor: '#2a333d'
                }
            }
        },
        series: [
            {
                name: '数据',
                type: 'map',
                mapType: 'guangdong',
                data: [
                    {name: '深圳市', value: Math.round(Math.random() * 1000)},
                    {name: '广州市', value: Math.round(Math.random() * 1000)}
                ],
                label: {
                    normal: {
                        show: false
                    },
                    emphasis: {
                        show: true
                    }
                },
                itemStyle: {
                    emphasis: {
                        label: {
                            show: true
                        }
                    }
                }
            }
        ]
    };

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

三、总结

通过本文的实操案例解析,相信你已经掌握了ECharts地图图表的使用方法。在实际应用中,可以根据自己的需求选择合适的地图类型和配置项,绘制出丰富多样的地图图表。希望本文能对你有所帮助,祝你学习愉快!

分享到: