首页 > 程序开发 > web前端 > JavaScript >

etmvc+jQuery EasyUI Highcharts实现柱状图

2013-01-28

基于JQuery easyUI 使用Highcharts实现了数据柱状图的显示,前段显示主要使用Highchartst这个图表库,后端使用etmvc框架从数据库获取数据,前后端数据交互采用Json数据格式,主要代码如下:前段HTML:[html] ...

基于JQuery easyUI 使用Highcharts实现了数据柱状图的显示,前段显示主要使用Highchartst这个图表库,后端使用etmvc框架从数据库获取数据,前后端数据交互采用Json数据格式,主要代码如下:
前段HTML:
[html]
<div style="padding:5px;">
<fieldset>
<div>
<div style="margin: 0 1px">
<div id="containerliuliang" style="min-width: 368px; height: 368px; margin: 0 auto"></div>
</div>
</div>
</fieldset>
</div>
前段JS:
[html]
<script type="text/javascript">
/*获取json数据开始*/
//定义变量
$(document).ready(function () {
var jsonXData = [];
var jsonyD1 = [];
var jsonyD2 = [];
//获取数据
$.ajax({ url: &#39;/ciccpsMember/memberfee/memberfeeByYear&#39;,
cache: false,
async: false,
success: function (data) {
var json = eval("(" + data + ")");
if (json.Rows.length > 0) {
for (var i = 0; i < json.Rows.length; i++) {
var rows = json.Rows[i];
var Year = rows.year;
var ShouldPay = rows.shouldPay;
var TruePay = rows.truePay;
jsonXData.push(Year); //赋值
jsonyD1.push(ShouldPay);
jsonyD2.push(TruePay);
} //for
var chart;
chart = new Highcharts.Chart({
chart: {
renderTo: &#39;containerliuliang&#39;,//放置图表的容器
plotBackgroundColor: null,
plotBorderWidth: null,
defaultSeriesType: &#39;column&#39; //图表类型line, spline, area, areaspline, column, bar, pie , scatter
},
title: {
text: &#39;近年会费缴纳情况&#39;,
style: { font: &#39;normal 13px 宋体&#39; }
},
xAxis: {//X轴数据
categories: jsonXData,
lineWidth: 2,
labels: {
rotation: -45, //字体倾斜
align: &#39;right&#39;,
style: { font: &#39;normal 13px 宋体&#39; }
}
},
yAxis: {//Y轴显示文字
lineWidth: 2,
title: {
text: &#39;金额/万元&#39;
}
},
tooltip: {
formatter: function () {
return &#39;<b>&#39; + this.x + &#39;</b><br/>&#39; +
this.series.name + &#39;: &#39; + this.y+&#39;万元&#39;;
}
},
plotOptions: {
column: {
dataLabels: {
enabled: true
},
enableMouseTracking: true//是否显示title
}
},
series: [{
name: &#39;应缴&#39;,
data: jsonyD1
}, {
name: &#39;实缴&#39;,
data: jsonyD2
}]
});
//$("tspan:last").hide(); //把广告删除掉
} //if
}
});
});
</script>
后端代码:
[java]
/**
* 近几年会费
*/
public JsonView memberfeeByYear() throws Exception {
db connection = new db();
String sql = "select * from memberfeesview";
ResultSet rs = connection.executeQuery(sql);
List<Map<String, Object>> _list = new ArrayList<Map<String, Object>>();
if (rs != null) {
try {
while (rs.next()) {
/* 获取信息 */
Map<String, Object> _map = new HashMap<String, Object>();
_map.put("year", rs.getString("year"));
_map.put("shouldPay", rs.getFloat("shouldPay"));
_map.put("truePay", rs.getFloat("truePay"));
_list.add(_map);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
connection.closed();
}
}
Map<String, Object> result = new HashMap<String, Object>();
result.put("RowCount",_list.size());
result.put("Rows", _list);
JsonView view = new JsonView(result);
view.setContentType("text/html;charset=utf-8");
return view;
}
Json数据格式:
[plain]
{"Rows":[{"truePay":30.0,"shouldPay":140.0,"year":"2013"},{"truePay":140.0,"shouldPay":140.0,"year":"2012"},{"truePay":134.5,"shouldPay":140.0,"year":"2011"},{"truePay":141.0,"shouldPay":140.0,"year":"2010"},{"truePay":85.0,"shouldPay":86.5,"year":"2009"}],"RowCount":5}
运行截图:
先介绍到这,其他图可结合Highcharts相关的教程来实现,附带 多个JS+Json柱状图饼图折线图示例 里面有多个不同的JS图标库的示例代码!
相关文章
最新文章
热点推荐