您的位置:首页 > 其它

echarts图表ajax获取数据填充

2017-12-06 20:19 429 查看


js部分

/* ---------------- 折线图 --------------- */

var line = echarts.init(document.getElementById('line'));//折线图

$.ajax({
type: "post",
url: '<%=basePath%>product.do?method=selectProductNum2',
cache: false,
dataType: "json",
success: function(data) {

//------------定义折线图数据---------------
line.setOption({
color : [ "#32d2c9" ],
title : {
x : 'left',
text : '本年度新增产品统计',
textStyle : {
fontSize : '18',
color : '#4c4c4c',
fontWeight : 'bolder'
}
},
tooltip : {
trigger : 'axis'
},
toolbox : {
show : true,
feature : {
dataZoom : {
yAxisIndex : 'none'
},
dataView : {
readOnly : false
},
magicType : {
type : [ 'line', 'bar' ]
}
}
},
xAxis : {
type : 'category',
boundaryGap : false,
data : [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月','九月' ,'十月' ,'十一月' ,'十二月'],
axisLabel : {
interval : 0
}
},
yAxis : {
type : 'value'
},
series : [ {
name : '平均',
type : 'line',
data : data, // 这个地方填充获得的数据[ 23, 42, 18, 45, 48, 49, 100, 49, 100 ,49, 100, 49 ]
markLine : {
data : [ {
type : 'average',
name : '平均值'
} ]
}
} ]
});

//-----------------------------
}
});

java后台代码

/**
* 折线图表展示
* @return
*/
@SuppressWarnings("deprecation")
public String selectProjectNum2(){
//得到今年的第一天
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy");
String format = dateFormat1.format(new Date());
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = format+"-01-01 00:00:01";
Date parse = new Date();
try {
parse = dateFormat2.parse(str);
} catch (ParseException e2) {
e2.printStackTrace();
}
//得到今年的所有产品
List<ProjectVO> findAll = projectService.findProjectAllByYear(parse.getTime());
int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0;
for (ProjectVO projectVO : findAll) {
long createTime = projectVO.getCreateTime();
Date date = new Date(createTime);
int month = date.getMonth()+1;
switch (month) {
case 1:
a++;
break;
case 2:
b++;
break;
case 3:
c++;
break;
case 4:
d++;
break;
case 5:
e++;
break;
case 6:
f++;
break;
case 7:
g++;
break;
case 8:
h++;
break;
case 9:
i++;
break;
case 10:
j++;
break;
case 11:
k++;
break;
case 12:
l++;
break;
default:
break;
}
}

List<Integer> months =new ArrayList<Integer>(0);
months.add(a);
months.add(b);
months.add(c);
months.add(d);
months.add(e);
months.add(f);
months.add(g);
months.add(h);
months.add(i);
months.add(j);
months.add(k);
months.add(l);

JSONArray fromObject = JSONArray.fromObject(months);
try {
getResponse().setCharacterEncoding("utf-8");
getResponse().getWriter().write(fromObject.toString());
} catch (IOException e1) {
e1.printStackTrace();
}

return null;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: