您的位置:首页 > 其它

拼接符合highCharts格式的数据类型

2016-04-18 14:55 369 查看
我使用easyUI的方式传递数据到后台,将所需的数据拼接起来后返回一个result给easyui,其中result需要包含横坐标的值,还有分组后的多条数据,具体的highcharts代码是
$.ajax({
url : "login/registerChart.action",
cache : true,
type : "post",
dataType : "json",
data:{"agentName":daiLiShang,"areaName":diqu,"teamName":duan,"picName":fu,"businessName":shangWu,
"customerName":keFU,"projectName":xiangMu,"productName":chanPing,"agentName1":agentName1,
"areaName1":areaName1,"teamName1":teamName1,"picName1":picName1,"businessName1":businessName1,
"customerName1":customerName1,"projectName1":projectName1,"productName1":productName1,"statTime":statTime},
success : function(result) {
$('#container').highcharts({
chart: {
type: 'line'
},
title: {
text: 'Blue-Game'
},
subtitle: {
text: 'Build the most cutting-edge fishing game in the world'
},
xAxis: {
categories: result.date
},
yAxis: {
title: {
text: '数量'
}
},
tooltip: {
enabled: false,
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+this.x +': '+ this.y +'°C';
}
},
plotOptions: {
line: {
dataLabels: {
enabled: true
},
enableMouseTracking: false
}
},
series:result.line
});
},
error:function(data){
alert("网络异常,请稍候再试");
}
});
其中 X轴的result.date存放的是date中的值,也就是要表现在X轴上的值,date是我在后台自己定义的,result.line的值存放的是具体的数据,line是我在后台自己定义的因为数据不是单一的,所以我需要按要求拼接字符窜,拼接的格式如下{"date":["2016-03-29","2016-03-30"],"line":[{"name":"注册人数","data":[32,45]}]}line中可以包含多个,比如{"name":"产品1","data":[21,33]},{"name":"产品2","data":[11,12]}首先拼接的是line中的值{"name":"产品1","data":[21,33]},{"name":"产品2","data":[11,12]}思路是通过判断name是否重复,从而将值放入到对应的map中,如果重复,就把相对应的值存入到匹配的name中去,代码如下
if (map2.keySet().contains(line)) {
List vList = (List) map2.get(line); // 得到line所对应的list数据
vList.add(Integer.valueOf(map1.get("SUM").toString()));
map2.put(map1.get(name1).toString(), vList);
} else {
List vList = new ArrayList();
vList.add(Integer.valueOf(map1.get("SUM").toString()));
map2.put(map1.get(name1).toString(), vList);
}
通过上述代码中在以产品名分组的条件下,我们得到的是{项目b=[11, 12], 项目a=[21, 33]}这种格式,这时候要通过迭代的方式把他变成line,代码如下
Iterator it = map2.keySet().iterator();while (it.hasNext()) {JSONObject jo = new JSONObject();String mkey = it.next().toString();jo.accumulate("name", mkey);jo.accumulate("data", map2.get(mkey));ja.add(jo);}
通过这种方式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: