在数据可视化领域,ECharts以其强大的功能和易用性而备受推崇。地图图表作为ECharts中的一个重要模块,能够帮助我们直观地展示地理空间数据。对于新手来说,掌握ECharts地图图表的制作可能显得有些挑战,但不用担心,本文将为你提供30个实用案例,帮助你轻松入门。
案例一:中国地图基础展示
首先,让我们从最简单的中国地图基础展示开始。通过ECharts,我们可以轻松地绘制出中国地图,并添加一些基本的交互功能。
// 引入 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)
}, {
name: '广东',
value: Math.round(Math.random() * 1000)
}]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
案例二:世界地图展示
除了中国地图,ECharts还支持世界地图的绘制。以下是一个简单的世界地图展示案例。
// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入世界地图数据
require('echarts/map/js/world');
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: '世界地图展示'
},
tooltip: {
trigger: 'item',
formatter: '{b}: {c}'
},
series: [{
name: '世界',
type: 'map',
mapType: 'world',
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);
案例三:自定义地图
ECharts还允许我们自定义地图,以下是一个自定义地图的案例。
// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入自定义地图数据
require('echarts/map/js/custom');
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: '自定义地图'
},
tooltip: {
trigger: 'item'
},
series: [{
name: '自定义地图',
type: 'map',
mapType: 'custom',
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);
案例四:地图点击事件
在地图图表中,我们可以添加点击事件来响应用户的交互。以下是一个地图点击事件的案例。
// 引入 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)
}, {
name: '广东',
value: Math.round(Math.random() * 1000)
}]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
// 监听点击事件
myChart.on('click', function (params) {
console.log(params.name);
});
案例五:地图缩放与平移
ECharts地图图表支持缩放和平移操作。以下是一个地图缩放与平移的案例。
// 引入 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)
}, {
name: '广东',
value: Math.round(Math.random() * 1000)
}]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
// 监听鼠标滚轮事件
myChart.on('mouseover', function (params) {
myChart.dispatchAction({
type: 'dataZoom',
start: 0,
end: 100
});
});
myChart.on('mouseout', function (params) {
myChart.dispatchAction({
type: 'dataZoom',
start: 0,
end: 100
});
});
案例六:地图区域高亮
在地图图表中,我们可以通过高亮特定的区域来突出显示数据。以下是一个地图区域高亮的案例。
// 引入 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)
}, {
name: '广东',
value: Math.round(Math.random() * 1000)
}]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
// 高亮指定区域
myChart.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: 0
});
案例七:地图数据动态更新
在实际应用中,地图数据可能会实时更新。以下是一个地图数据动态更新的案例。
// 引入 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)
}, {
name: '广东',
value: Math.round(Math.random() * 1000)
}]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
// 定时更新数据
setInterval(function () {
var data = option.series[0].data;
for (var i = 0; i < data.length; i++) {
data[i].value = Math.round(Math.random() * 1000);
}
myChart.setOption({
series: [{
data: data
}]
});
}, 2000);
案例八:地图与折线图结合
在数据可视化中,地图与折线图结合可以更全面地展示数据。以下是一个地图与折线图结合的案例。
// 引入 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)
}, {
name: '广东',
value: Math.round(Math.random() * 1000)
}]
}, {
name: '折线图',
type: 'line',
data: [[0, Math.round(Math.random() * 1000)], [1, Math.round(Math.random() * 1000)], [2, Math.round(Math.random() * 1000)]]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
案例九:地图与柱状图结合
地图与柱状图结合可以更直观地展示地区间的数据对比。以下是一个地图与柱状图结合的案例。
// 引入 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)
}, {
name: '广东',
value: Math.round(Math.random() * 1000)
}]
}, {
name: '柱状图',
type: 'bar',
data: [[0, Math.round(Math.random() * 1000)], [1, Math.round(Math.random() * 1000)], [2, Math.round(Math.random() * 1000)]]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
案例十:地图与散点图结合
地图与散点图结合可以更直观地展示地区间的数据分布。以下是一个地图与散点图结合的案例。
// 引入 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)
}, {
name: '广东',
value: Math.round(Math.random() * 1000)
}]
}, {
name: '散点图',
type: 'scatter',
data: [[0, Math.round(Math.random() * 1000)], [1, Math.round(Math.random() * 1000)], [2, Math.round(Math.random() * 1000)]]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
案例十一:地图与饼图结合
地图与饼图结合可以更直观地展示地区间的数据占比。以下是一个地图与饼图结合的案例。
// 引入 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)
}, {
name: '广东',
value: Math.round(Math.random() * 1000)
}]
}, {
name: '饼图',
type: 'pie',
radius: ['30%', '50%'],
center: ['50%', '50%'],
data: [[0, Math.round(Math.random() * 1000)], [1, Math.round(Math.random() * 1000)], [2, Math.round(Math.random() * 1000)]]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
案例十二:地图与雷达图结合
地图与雷达图结合可以更全面地展示地区间的数据对比。以下是一个地图与雷达图结合的案例。
// 引入 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)
}, {
name: '广东',
value: Math.round(Math.random() * 1000)
}]
}, {
name: '雷达图',
type: 'radar',
data: [[Math.round(Math.random() * 1000)], [Math.round(Math.random() * 1000)], [Math.round(Math.random() * 1000)]]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
案例十三:地图与漏斗图结合
地图与漏斗图结合可以更直观地展示地区间的数据占比。以下是一个地图与漏斗图结合的案例。
”`javascript // 引入 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:
