ECharts 是一个使用 JavaScript 实现的开源可视化库,它能够帮助开发者轻松地将数据转换成图表。其中,地图图表是 ECharts 中非常实用且具有吸引力的功能之一。对于新手来说,通过学习一些实用的案例,可以快速掌握如何使用 ECharts 绘制中国地图。本文将为你介绍 30 个实用案例,帮助你轻松上手 ECharts 地图图表。
1. 基础中国地图绘制
首先,我们需要了解如何绘制一个基础的中国地图。以下是一个简单的示例代码:
// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入中国地图数据
require('echarts/map/js/china');
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: '中国地图示例'
},
tooltip: {
trigger: 'item'
},
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 1000)},
// ... 其他省份数据
]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
2. 省份自定义样式
在实际应用中,我们可能需要为不同省份设置不同的样式。以下是一个为特定省份设置自定义样式的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 1000)},
// ... 其他省份数据
],
itemStyle: {
normal: {
areaColor: '#323c48',
borderColor: '#111'
},
emphasis: {
areaColor: '#2a333d'
}
}
}]
3. 热力图效果
ECharts 地图图表支持热力图效果,以下是一个使用热力图效果的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 1000)},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
},
// 热力图效果
visualMap: {
min: 0,
max: 1000,
left: 'left',
top: 'bottom',
text: ['高','低'], // 文本,默认为数值文本
calculable: true
}
}]
4. 省份点击事件
在实际应用中,我们可能需要为省份添加点击事件。以下是一个为省份添加点击事件的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 1000)},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
},
// 省份点击事件
tooltip: {
trigger: 'item',
formatter: function(params) {
return params.name + ':' + params.value;
}
}
}]
5. 多级地图
ECharts 地图图表支持多级地图,以下是一个使用多级地图的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 1000)},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
},
// 多级地图
levels: [
{
itemStyle: {
areaColor: '#323c48',
borderColor: '#111'
}
},
{
itemStyle: {
areaColor: '#2a333d'
}
}
]
}]
6. 省份自定义标签
在实际应用中,我们可能需要为省份添加自定义标签。以下是一个为省份添加自定义标签的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true,
formatter: function(params) {
return params.name + '(' + params.value + ')';
}
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 1000)},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
}
}]
7. 省份自定义颜色
在实际应用中,我们可能需要为省份设置不同的颜色。以下是一个为省份设置不同颜色的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000), itemStyle: {color: '#ff7f50'}},
{name: '上海', value: Math.round(Math.random() * 1000), itemStyle: {color: '#87cefa'}},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
}
}]
8. 省份自定义边框
在实际应用中,我们可能需要为省份设置自定义边框。以下是一个为省份设置自定义边框的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000), itemStyle: {borderColor: '#ff7f50'}},
{name: '上海', value: Math.round(Math.random() * 1000), itemStyle: {borderColor: '#87cefa'}},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
}
}]
9. 省份自定义阴影
在实际应用中,我们可能需要为省份设置自定义阴影。以下是一个为省份设置自定义阴影的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000), itemStyle: {shadowBlur: 10, shadowColor: '#ff7f50'}},
{name: '上海', value: Math.round(Math.random() * 1000), itemStyle: {shadowBlur: 10, shadowColor: '#87cefa'}},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
}
}]
10. 省份自定义标签位置
在实际应用中,我们可能需要为省份设置自定义标签位置。以下是一个为省份设置自定义标签位置的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true,
position: 'right',
formatter: function(params) {
return params.name + '(' + params.value + ')';
}
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 1000)},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
}
}]
11. 省份自定义标签旋转角度
在实际应用中,我们可能需要为省份设置自定义标签旋转角度。以下是一个为省份设置自定义标签旋转角度的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true,
rotate: 45,
formatter: function(params) {
return params.name + '(' + params.value + ')';
}
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 1000)},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
}
}]
12. 省份自定义标签字体大小
在实际应用中,我们可能需要为省份设置自定义标签字体大小。以下是一个为省份设置自定义标签字体大小的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true,
fontSize: 12,
formatter: function(params) {
return params.name + '(' + params.value + ')';
}
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 1000)},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
}
}]
13. 省份自定义标签颜色
在实际应用中,我们可能需要为省份设置自定义标签颜色。以下是一个为省份设置自定义标签颜色的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true,
color: '#ff7f50',
formatter: function(params) {
return params.name + '(' + params.value + ')';
}
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 1000)},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
}
}]
14. 省份自定义标签背景颜色
在实际应用中,我们可能需要为省份设置自定义标签背景颜色。以下是一个为省份设置自定义标签背景颜色的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true,
backgroundColor: '#ff7f50',
formatter: function(params) {
return params.name + '(' + params.value + ')';
}
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 1000)},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
}
}]
15. 省份自定义标签边框颜色
在实际应用中,我们可能需要为省份设置自定义标签边框颜色。以下是一个为省份设置自定义标签边框颜色的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true,
borderColor: '#ff7f50',
formatter: function(params) {
return params.name + '(' + params.value + ')';
}
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 1000)},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
}
}]
16. 省份自定义标签阴影颜色
在实际应用中,我们可能需要为省份设置自定义标签阴影颜色。以下是一个为省份设置自定义标签阴影颜色的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true,
shadowColor: '#ff7f50',
formatter: function(params) {
return params.name + '(' + params.value + ')';
}
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 1000)},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
}
}]
17. 省份自定义标签阴影透明度
在实际应用中,我们可能需要为省份设置自定义标签阴影透明度。以下是一个为省份设置自定义标签阴影透明度的示例:
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true,
shadowBlur: 10,
shadowOpacity: 0.5,
formatter: function(params) {
return params.name + '(' + params.value + ')';
}
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 1000)},
// ... 其他省份数据
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
},
emphasis: {
label: {
show: true,
textStyle: {
color: '#fff'
}
}
}
}
}]
18. 省份自定义标签阴影方向
在实际应用中,我们可能需要为省份设置自定义标签阴影方向。以下是一个为省份设置自定义标签阴影方向的示例:
”`javascript series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
show: true,
shadowBlur: 10,
shadowOffsetX: 5,
shadowOffsetY: 5,
formatter: function(params) {
return params.name + '(' + params.value + ')';
}
},
data: [
{name: '北京', value: Math.round(Math.random() * 1000)},
{name: '上海', value: Math.round(Math.random() * 100