Highcharts Ajax gets the JSON object to dynamically generate report generation. __js

Source: Internet
Author: User

http://blog.csdn.net/wsk7860/article/details/8751061


Recently to do a project, the project manager want to do a statistical report, on the Internet to check some information on the selection of highchars here and you share the use of experience.

It is important to note that this code displays multiple item comparisons for a single report.

Direct Paste 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 Notes
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 I use the servlet here
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;

Assign a value to a 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 series
Member.put ("name", "Zhang Fei" +i); corresponding 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));//corresponding y axis display
Member.put ("list", list);
Member.put ("Color", "#FF0022"), or you can set a histogram color if you want
Members.put (member);
}

Out.write (Members.tostring ());

catch (Jsonexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

Out.flush ();
Out.close ();

Picture for effect Chart:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.