Recently to do a project, the project manager wants to do a statistical report, on the Internet to view some information on the choice of highchars here and you share the use of experience.
It is important to note that this code is displayed for multiple items compared to one report.
Direct code: WEB-side
<script type= "Text/javascript" src= "Js/jquery/jquery-1.7.2.js" ></script>
<script type= "Text/javascript" src= "Js/highcharts/highcharts.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
var options = {
Chart: {
Renderto: ' container ',//div container ID
Type: ' column '//report type
},
Report Name
title:{
Text: ' Test '
},
//Supplemental Instructions
Subtitle: {
Text: ' Report description '
},
YAxis: {
min:0,
Title: {
Text: ' Unit (mm) '
}
},
X-Axis display content
Xaxis: {
Categories: []
},
//Data sources (multiple comparisons)
Series: [{},{},{},{}]
};
JSON URL address Here I use the servlet
var url = "Http://127.0.0.1:8080/servlet/JsonServlet";
$.getjson (url,function (data) {
var i,len=data.length;
for (i=0;i<len;i++) {
//Assignment Series
options.series[i].data = Data[i]. list;
options.series[i].name = data[i].name;
Assigning a value to the report X-axis display name
Options.xaxis.categories[i]=data[i].year;
}
var chart = new Highcharts.chart (options);
});
});
</script>
<body>
<div id= "Container" ></div>
</body>
Background servlet doget () method content:
Response.setcharacterencoding ("UTF-8");
Response.setcontenttype ("text/html");
Jsonarray members = new Jsonarray ();
PrintWriter out= Response.getwriter ();
try {
for (int i=1;i<5;i++) {
Building a JSON object
Jsonobject member = new Jsonobject ();
Parameters required to build the series
Member.put ("name", "Zhang Fei" +i); Correspondence Series.Name
Jsonarray list = new Jsonarray ();//corresponds to Series.data
for (int k=1;k<5;k++) {
List.put (k*100);
}
Member.put ("Year", (+ i));//corresponds to Y-axis display
Member.put ("list", list);
Member.put ("Color", "#FF0022");//If you need to set the bar chart color
Members.put (member);
}
Out.write (Members.tostring ());
} catch (Jsonexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Out.flush ();
Out.close ();
The picture is:
Highcharts Axja get JSON object dynamically generated report generation