因为工作需要,所以研究了几天百度的echarts来绘制地图,效果还是不错的:
1、下载echarts插件包,解压后找到 doc/example/www/js目录,可以找到echarts.js、echarts-map.js、esl.js三个js文件,理论上有这3个js文件就足够我们来使用echarts插件了
2、建立一个项目,在html中首先加载esl.js文件
<!--Step:1 Import a module loader, such as esl.js or require.js-->
<!--Step:1 引入一个模块加载器,如esl.js或者require.js-->
<script src="js/esl.js"></script>
3、指定一个div用来加载地图,例如id为mapChart的div
<!--Step:2 Prepare a dom for ECharts which (must) has size (width & hight)-->
<!--Step:2 为ECharts准备一个具备大小(宽高)的Dom-->
<div id="mapChart" style="width:1000px;height:500px;border:solid 1px #ccc;padding:10px;margin:100px auto 10px;"></div>
4、在script区域首先执行如下代码
// Step:3 conifg ECharts's path, link to echarts.js from current page.
// Step:3 为模块加载器配置echarts的路径,从当前页面链接到echarts.js,定义所需图表路径
require.config({
paths:{
'echarts':'./js/echarts',
'echarts/chart/map' : './js/echarts-map'//不能随意变更
}
});
5、在页面加载完成的onload事件中,执行
window.onload = init;
function init(){
map = document.getElementById('mapChart');
require(
[
'echarts',
'echarts/chart/map'
],
requireCallback //插件加载完之后的回调函数
);
}
6、requireCallback作为echarts插件加载完成的回调事件,方便我们自定义
var echarts;
var mapChart;
var map;
function requireCallback(ec){
echarts = ec;
mapChart = echarts.init(map);
refresh();
}
function refresh(){
if (mapChart && mapChart.dispose) {
mapChart.dispose();
}
mapChart = echarts.init(map);
var options = getoptions();
mapChart.setOption(options,true);
}
7、最后便是关键的echarts插件参数传递了
function getoptions(){
options = {
legend: {
orient: 'vertical',
x:'left'
},
dataRange: {
min: 0,
max: 2500,
color:["#428bca","#fff"],
textStyle:{
color:"#fff"
},
calculable:true
},
series : [
{
type: 'map',
mapType: 'china',
itemStyle:{
normal:{label:{show:true}},
emphasis:{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: '广东',selected:true,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)}
]
}
]
}
return options;
}