时间:2021-07-01 10:21:17 帮助过:2人阅读
注意需要return null;因为不希望struts导航到其他的地方,而是通过JQuery Ajax来请求。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
<meta charset="utf-8">
<title>ECharts</title>
<!-- 来自百度CDN -->
<script src="http://s1.bdstatic.com/r/www/cache/ecom/esl/1-6-10/esl.js"></script>
<script src="JS/jquery-1.7.1.js"></script>
</head>
<body>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div id="main1" style="height:400px"></div>
<button type="button" id="button1">显示/隐藏</button>
<script type="text/javascript">
$(function() {
$("#button1").click(function() {
$("#main1").slideToggle(999);
});
});
var actualPower = new Array();
var expectedPower = new Array();
$.ajax({
url : ‘Power.action‘,
type : ‘GET‘,
dataType : ‘json‘,
async : false,
success : function(jsonArray) {
for (x in jsonArray[0]) {
actualPower[x] = jsonArray[0][x];
}
for (x in jsonArray[0]) {
expectedPower[x] = jsonArray[1][x];
}
}
});
// 路径配置
require.config({
paths : {
‘echarts‘ : ‘http://echarts.baidu.com/build/echarts‘,
‘echarts/chart/bar‘ : ‘http://echarts.baidu.com/build/echarts‘
}
});
// 使用
require([ ‘echarts‘, ‘echarts/chart/bar‘ // 使用柱状图就加载bar模块,按需加载
], function(ec) {
// 基于准备好的dom,初始化echarts图表
var myChart = ec.init(document.getElementById(‘main1‘));
var option = {
title : {
text : ‘发电量对比‘,
subtext : ‘模拟测试‘
},
tooltip : {
trigger : ‘axis‘
},
legend : {
data : [ ‘逆变器‘, ‘汇流箱‘, ‘发电单元1‘, ‘发电单元2‘ ]
},
toolbox : {
show : true,
feature : {
mark : {
show : true
},
dataView : {
show : true,
readOnly : false
},
magicType : {
show : true,
type : [ ‘line‘, ‘bar‘ ]
},
restore : {
show : true
},
saveAsImage : {
show : true
}
}
},
calculable : true,
xAxis : [ {
type : ‘category‘,
data : [ ‘1月‘, ‘2月‘, ‘3月‘, ‘4月‘, ‘5月‘, ‘6月‘, ‘7月‘, ‘8月‘,
‘9月‘, ‘10月‘, ‘11月‘, ‘12月‘ ]
} ],
yAxis : [ {
type : ‘value‘
} ],
series : [ {
name : ‘逆变器‘,
type : ‘line‘,
data : actualPower,
markPoint : {
data : [ {
type : ‘max‘,
name : ‘最大值‘
}, {
type : ‘min‘,
name : ‘最小值‘
} ]
},
markLine : {
data : [ {
type : ‘average‘,
name : ‘平均值‘
} ]
}
}, {
name : ‘汇流箱‘,
type : ‘bar‘,
data : actualPower,
markPoint : {
data : [ {
type : ‘max‘,
name : ‘最大值‘
}, {
type : ‘min‘,
name : ‘最小值‘
} ]
},
markLine : {
data : [ {
type : ‘average‘,
name : ‘平均值‘
} ]
}
}, {
name : ‘发电单元1‘,
type : ‘bar‘,
data : actualPower,
markPoint : {
data : [ {
type : ‘max‘,
name : ‘最大值‘
}, {
type : ‘min‘,
name : ‘最小值‘
} ]
},
markLine : {
data : [ {
type : ‘average‘,
name : ‘平均值‘
} ]
}
}, {
name : ‘发电单元2‘,
type : ‘bar‘,
data : expectedPower,
markPoint : {
data : [ {
name : ‘年最高‘,
value : 182.2,
xAxis : 7,
yAxis : 183,
symbolSize : 18
}, {
name : ‘年最低‘,
value : 2.3,
xAxis : 11,
yAxis : 3
} ]
},
markLine : {
data : [ {
type : ‘average‘,
name : ‘平均值‘
} ]
}
} ]
};
// 为echarts对象加载数据
myChart.setOption(option);
});
</script>
</body>
$.ajax({
url : ‘Power.action‘,
type : ‘GET‘,
dataType : ‘json‘,
async : false,
success : function(jsonArray) {
for (x in jsonArray[0]) {
actualPower[x] = jsonArray[0][x];
}
for (x in jsonArray[0]) {
expectedPower[x] = jsonArray[1][x];
}
}
});
其中 url的参数是请求的action
dataType数据类型选择json
success是请求成功后的返回,jsonArray就是请求成功,上面的Action的方法通过Servlet传递过来的Json对象。二维数组jsonArray[0]直接能获取到第一个封装的ArrayList,而jsonArray[0][x]获取到ArrayList中的第x个数据。4.Echarts
Echarts中的图表数据填充就是Array()对象。
data:actualPower,
这里直接填充了JSON传递过来的数据。