最近公司的一个项目需要使用地图来展现数据,而且在悬浮窗中展现一个省份的两组不同数据。如果不在
formatter里面写functions函数的话,那么展现的数据只是两者只和。
这只是个例子,代码结构很简单,如下:一个js,一个map.jsp页面
map.jsp页面代码如下:
<body>
<div id="<span style="color:#ff0000;">mainMap</span>" style="width:80%; height:100%; border:1px solid red;margin: auto;"></div>
</body>
<script type="text/javascript" src="js/<span style="color:#330099;">echarts-plain-map.js</span>"></script>
<script type="text/javascript">
_charts();
function _charts()
{
debugger;
var myChart2 = echarts.init(document.getElementById('<span style="color:#ff6666;">mainMap</span>'));
option = {
title : {
text: 'iphone销量',
subtext: '纯属虚构',
x:'center'
},
tooltip : {
trigger: 'item',
formatter: function (params,ticket,callback) {
var res = '' + params[1];
var iphone3 = "";
var iphone4 = "";
for(var i = 0; i <option.series[0].data.length; i++)
{
if(option.series[0].data[i].name == params[1])
{
iphone3 = option.series[0].data[i].value;
}
}
for(var j = 0; j <option.series[1].data.length; j++)
{
if(option.series[1].data[j].name == params[1])
{
iphone4 = option.series[1].data[j].value;
}
}
res += '<br/>'+"iphone3:"+iphone3+'<br/>'+"iphone4:"+iphone4;
setTimeout(function (){
// 仅为了模拟异步回调,每隔1000毫秒刷新一次
callback(ticket, res);
}, 1000)
return 'loading';
}
},
legend: {
orient: 'vertical',
x:'left',
data:['iphone3','iphone4']
},
dataRange: {
min: 0,
max: 2500,
x: 'left',
y: 'bottom',
text:['高','低'], // 文本,默认为数值文本
calculable : true
},
toolbox: {
show : true,
orient : 'vertical',
x: 'right',
y: 'center',
feature : {
mark : {show: true},
dataView : {show: true, readOnly: false},
restore : {show: true},
saveAsImage : {show: true}
}
},
series : [
{
name: 'iphone3',
type: 'map',
mapType: 'china',
roam: false,
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: '广东',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: 'iphone4',
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)}
]
}
]
};
myChart2.setOption(option);
}
</script>
展现结果如下:
其中的重要部分就是tooltip里面的functions函数,option是一个对象,里面含有两组数据。通过与param[1]进行省份对比,获得值。
对于series中data部分,可通过后台获得JSON数据串,以上述方式展现。