页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts Example</title>
<script type="text/javascript" src="<%=path%>/21/js/jquery.min.js"></script>
<script src="<%=path%>/21/js/highcharts.js"></script>
<script src="<%=path%>/21/js/modules/exporting.js"></script>
<script type="text/javascript">
$(function() {
$.ajax({
type:"post",
dataType:"json",
url: '../../../testjs.do',
success : function(result){
var chart;
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'area'
},
title: {
text: 'US and USSR nuclear stockpiles'
},
subtitle: {
text: 'Source: <a href="http://thebulletin.metapress.com/content/c4120650912x74k7/fulltext.pdf">'+
'thebulletin.metapress.com</a>'
},
xAxis: {
labels: {
formatter: function() {
return this.value; // clean, unformatted number for year
}
}
},
yAxis: {
title: {
text: 'Nuclear weapon states'
},
labels: {
formatter: function() {
return this.value / 1000 +'k';
}
}
},
tooltip: {
formatter: function() {
return this.series.name +' produced <b>'+
Highcharts.numberFormat(this.y, 0) +'</b><br/>warheads in '+ this.x;
}
},
plotOptions: {
area: {
pointStart: 1940,
marker: {
enabled: false,
symbol: 'circle',
radius: 2,
states: {
hover: {
enabled: true
}
}
}
}
},
series:result
});
}
});
});
</script>
</head>
<body>
<div id="container"
style="min-width: 400px; height: 400px; margin: 0 auto"></div>
</body>
</html>
Model:
public class Zjjtest {
private String name;
private String data;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
Action:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import test.welcome.controller.Zjjtest;
@Controller
public class TestJSON {
@RequestMapping("/testjs")
public void test(HttpServletRequest request,HttpServletResponse response) throws IOException{
PrintWriter out=response.getWriter();
response.setContentType("application/json;charset=utf-8");
Zjjtest t=new Zjjtest();
t.setName("zjj");
t.setData("[null, null, null, null, null, 6 , 11, 32, 110, 235, 369, 640,1005, 1436, 2063, 3057, 4618, 6444, 9822, 15468, 20434, 24126,27387, 29459, 31056, 31982, 32040, 31233, 29224, 27342, 26662,26956, 27912, 28999, 28965, 27826, 25579, 25722, 24826, 24605,24304, 23464, 23708, 24099, 24357, 24237, 24401, 24344, 23586,22380, 21004, 17287, 14747, 13076, 12555, 12144, 11009, 10950,10871, 10824, 10577, 10527, 10475, 10421, 10358, 10295, 10104]");
Zjjtest t1=new Zjjtest();
t1.setName("zjj1");
t1.setData("[null, null, null, null, null, null, null , null , null ,null, 5, 25, 50, 120, 150, 200, 426, 660, 869, 1060, 1605, 2471, 3322,4238, 5221, 6129, 7089, 8339, 9399, 10538, 11643, 13092, 14478,15915, 17385, 19055, 21205, 23044, 25393, 27935, 30062, 32049,33952, 35804, 37431, 39197, 45000, 43000, 41000, 39000, 37000,35000, 33000, 31000, 29000, 27000, 25000, 24000, 23000, 22000,21000, 20000, 19000, 18000, 18000, 17000, 16000]");
List test=new ArrayList();
test.add(t);
test.add(t1);
out.print(JSONArray.fromObject(test).toString());
}
}
静态页面带有说明的:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Highcharts Demo</title>
<script type="text/javascript" src="<%=path %>/21/js/jquery.min.js"></script>
<script type="text/javascript" src="<%=path %>/21/js/highcharts.js"></script>
<script type="text/javascript" src="<%=path %>/21/js/excanvas.compiled.js"></script>
<script type="text/javascript">
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',//设置显示图表的容器
type: 'line',//设置图表样式,可以为line,spline, scatter, splinearea bar,pie,area,column
// defaultSeriesType: 'column', //图表的默认样式
// margin:[21, 23, 24, 54],//整个图表的位置(上下左右的空隙)
marginRight: 200,//右边间距
marginBottom: 25//底部间距/空隙
// inverted: false,//可选,控制显示方式,默认上下正向显示
// shadow:true,//外框阴影
// backgroundColor:"#FFF",
// animation:true,
// borderColor:"#888",
// borderRadius:5,
// borderWidth:1,
// ignoreHiddenSeries:true,
// reflow:true,
// plotBorderWidth:1,
// alignTicks:true
},
labels:{//在报表上显示的一些文本
items:[{
html:'本图表数据有误,仅用于说明相应的属性',
style:{left:'100px',top:'60px'}
}, {
html:'http://www.highcharts.com/demo',
style:{left:'100px',top:'100px'}
}]
},
credits:{//右下角的文本
enabled: true,
position: {//位置设置
align: 'right',
x: -10,
y: -10
},
href: "http://www.highcharts.com",//点击文本时的链接
style: {
color:'blue'
},
text: "Highcharts Demo"//显示的内容
},
// plotOptions:{//绘图线条控制
// spline:{
// allowPointSelect :true,//是否允许选中点
// animation:true,//是否在显示图表的时候使用动画
// cursor:'pointer',//鼠标移到图表上时鼠标的样式
// dataLabels:{
// enabled :true,//是否在点的旁边显示数据
// rotation:0
// },
// enableMouseTracking:true,//鼠标移到图表上时是否显示提示框
// events:{//监听点的鼠标事件
// click: function() {
// }
// },
// marker:{
// enabled:true,//是否显示点
// radius:3,//点的半径
// fillColor:"#888"
// lineColor:"#000"
// symbol: 'url(http://highcharts.com/demo/gfx/sun.png)',//设置点用图片来显示
// states:{
// hover:{
// enabled:true//鼠标放上去点是否放大
// },
// select:{
// enabled:false//控制鼠标选中点时候的状态
// }
// }
// },
// states:{
// hover:{
// enabled:true,//鼠标放上去线的状态控制
// lineWidth:3
// }
// },
// stickyTracking:true,//跟踪
// visible:true,
// lineWidth:2//线条粗细
// pointStart:100,
// }
// },
title: {
text: 'Monthly Average Temperature',//标题
x: -20 //center设置标题的位置
},
subtitle: {
text: 'Source: WorldClimate.com',//副标题
x: -20//副标题位置
},
xAxis: {//横轴的数据
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
// lineWidth:1,//纵轴一直为空所对应的轴,即X轴
// plotLines: [{//一条竖线
// color: '#FF0000',
// width: 2,
// value: 5.5
// }]
// labels: {//设置横轴坐标的显示样式
// rotation: -45,//倾斜度
// align: 'right',
// style: {
// font: 'normal 13px Verdana, sans-serif'
// color: 'white'
// }
// }
},
yAxis: {
// tickInterval: 200, //自定义刻度
// max:1000,//纵轴的最大值
// min: 0,//纵轴的最小值
title: {//纵轴标题
text: '百分数'
},
labels : {
formatter : function() {//设置纵坐标值的样式
return this.value + '%';
}
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {//鼠标移到图形上时显示的提示框
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y +'°C';
}
// crosshairs:[{//控制十字线
// width:1,
// color:"#CCC",
// dashStyle:"longdash"
// }
},
legend: {//方框所在的位置(不知道怎么表达)
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: [{//以下为纵轴数据
name: 'Tokyo',
data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
}, {
name: 'New York',
data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
}, {
name: 'Berlin',
data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
}, {
name: 'London',
data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
}]
});
});
</script>
</head>
<body>
<div id="container" ></div>
</body>
</html>