我的 Spring Controller 类看起来像这样
@SuppressWarnings("rawtypes")
@RequestMapping(value = "/cityGridView", method = RequestMethod.GET)
public @ResponseBody
List showLineChart(Map<String, Object> map,
HttpServletRequest request, HttpServletResponse response) {
List<Object> rows = new ArrayList<Object>();
List<MapTable> list = contactService.fin();
for (MapTable table : list) {
List<Object> dataRow = new ArrayList<Object>(1);
dataRow.add(table.getSRDate());
dataRow.add(table.getNumberOfSR());
rows.add(dataRow);
}
return rows;
}
在我的jsp中,我处理这样的响应。(这里似乎有问题??)
<div id="chart1" style="width: 800px;height: 500px" ></div>
<script type="text/javascript">
$(document).ready(function(){
var ajaxDataRenderer = function(url, plot, options) {
var ret = null;
$.ajax({
async: false,
url: url,
dataType:"json",
success: function(data) {
ret = data;
}
});
return ret;
};
var jsonurl = 'cityGridView.html';
var today = new Date();
var plot1 = $.jqplot('chart1', jsonurl, {
title:'Data Point Highlighting',
dataRenderer: ajaxDataRenderer,
dataRendererOptions: {
unusedOptionalUrl: jsonurl
},
axes:{
xaxis:{
label: "SR_DATES",
'numberTicks' : 7,
min: '2012-10-01',
max: '2012-10-07',
renderer:$.jqplot.DateAxisRenderer,
rendererOptions:{tickRenderer:$.jqplot.CanvasAxisTickRenderer},
tickInterval:'1 day',
tickOptions:{
formatString:'%Y-%#m-%#d'
}
},
yaxis:{
label: "SR COUNT",
tickOptions:{
formatString:'%d'
},
min:10,
max:30
}
},
highlighter: {
show: true,
sizeAdjust: 7.5
},
cursor: {
show: true
}
});
});
</script>
我的 JSON 是这样的
[["2012-10-01",15.0],["2012-10-02",20.0],["2012-10-03",25.0],["2012-10-04",18.0],[ "2012-10-05",22.0],["2012-10-06",24.0]]
这里显示了我的 MapTable 类
public class MapTable {
private Date SRDate;
private int numberOfSR;
public Date getSRDate() {
return SRDate;
}
public void setSRDate(Date sRDate) {
SRDate = sRDate;
}
public int getNumberOfSR() {
return numberOfSR;
}
public void setNumberOfSR(int numberOfSR) {
this.numberOfSR = numberOfSR;
}
}
联系服务.fin(); 调用服务类的方法,最后在 DAO 类中调用它。数据也正确地像上面提到的数组一样。这是我的 DAO 类
public List<MapTable> fin(){
@SuppressWarnings("unchecked")
List<MapTable> dashboardBeanList = jdbcTemplate
.query("select trunc(ASSIGNED_datetime) as SR_DATE, count(*) as COUNT " +
"from sbl_service_request_v " +
"where SR_TYPE ='Complaint' " +
"and DATE_COMMITED is not null " +
"and ASSIGNED_DIVISION in ('CSO','IT_IVR') " +
"and trunc(ASSIGNED_datetime) >= sysdate -30 " +
"group by trunc(ASSIGNED_datetime) " +
"order by trunc(ASSIGNED_datetime)",
new Object[] {},
new RowMapper() {
public MapTable mapRow(ResultSet rs, int rowNum)
throws SQLException {
MapTable dashboardBean=new MapTable();
dashboardBean.setSRDate(rs.getDate("SR_DATE"));
dashboardBean.setNumberOfSR(rs.getInt("COUNT"));
return dashboardBean;
}
});
return dashboardBeanList;
}