ECharts 是一个使用 JavaScript 实现的开源可视化库,它提供了一系列丰富的图表类型,包括地图图表。地图图表能够将地理信息直观地展示出来,非常适合数据可视化分析。本文将带你通过实用案例解析,轻松入门地图可视化。
一、ECharts 地图图表简介
ECharts 地图图表基于地理坐标系进行绘制,可以展示各种地理信息,如行政区划、地理位置等。它支持多种地图类型,包括中国地图、世界地图、自定义地图等。
二、ECharts 地图图表的基本使用
1. 引入 ECharts 和地图数据
首先,你需要引入 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.0.0/echarts.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.0.0/map/js/china.js"></script>
<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: 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)},
{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>
2. 配置选项
在上面的示例中,我们配置了以下选项:
title:标题tooltip:提示框visualMap:视觉映射组件,用于控制图表的视觉元素,如颜色、大小等series:系列列表,包含图表的各个系列
3. 渲染图表
最后,我们调用 myChart.setOption(option) 方法将配置选项应用到图表上,从而渲染出地图图表。
三、实用案例解析
1. 地图热力图
地图热力图可以直观地展示某个地区的数据密度。以下是一个简单的地图热力图示例:
option = {
title: {
text: '中国地图热力图示例'
},
tooltip: {
trigger: 'item'
},
visualMap: {
min: 0,
max: 1000,
left: 'left',
top: 'bottom',
text: ['高','低'],
calculable: true
},
series: [{
name: '热力图',
type: 'heatmap',
coordinateSystem: 'geo',
data: [
{name: '北京', value: [116.46,39.92,100]},
{name: '上海', value: [121.47,31.23,500]},
{name: '广州', value: [113.23,23.16,300]},
{name: '深圳', value: [114.07,22.62,200]}
]
}]
};
2. 地图散点图
地图散点图可以展示多个地理位置的数据点。以下是一个简单的地图散点图示例:
option = {
title: {
text: '中国地图散点图示例'
},
tooltip: {
trigger: 'item'
},
series: [{
name: '散点图',
type: 'scatter',
coordinateSystem: 'geo',
data: [
{name: '北京', value: [116.46,39.92]},
{name: '上海', value: [121.47,31.23]},
{name: '广州', value: [113.23,23.16]},
{name: '深圳', value: [114.07,22.62]}
]
}]
};
四、总结
通过本文的介绍,相信你已经对 ECharts 地图图表有了初步的了解。在实际应用中,你可以根据自己的需求调整配置选项,制作出各种风格的地图图表。希望本文对你有所帮助!
