資源描述:
《highcharts獲取json數(shù)據(jù)展現(xiàn)》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫(kù)。
1、實(shí)際上很多時(shí)候圖表展現(xiàn)的數(shù)據(jù)都是從服務(wù)器端獲取,現(xiàn)在來(lái)做一個(gè)簡(jiǎn)單的異步獲取json數(shù)據(jù)的例子?! 》?wù)器端用Servlet3.0實(shí)現(xiàn),JSP頁(yè)面通過(guò)jquery異步請(qǐng)求json數(shù)據(jù)提供給Highcharts展現(xiàn)?! ?、用一個(gè)實(shí)體類封裝要展現(xiàn)的信息packagecn.luxh.app.entity;publicclassBrowserShare{//瀏覽器名稱privateStringname;//份額privatefloatshare;publicBrowserShare(Stringname,floatshare){super();this.n
2、ame=name;this.share=share;}publicfloatgetShare(){returnshare;}publicvoidsetShare(floatshare){this.share=share;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}} 2、處理請(qǐng)求的Servletpackagecn.luxh.app.servlet;importjava.io.IOException;importjava.io.Pr
3、intWriter;importjava.util.ArrayList;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.googl
4、e.gson.Gson;importcn.luxh.app.entity.BrowserShare;@WebServlet(name="dataServlet",value="/servlet/dataServlet")publicclassDataServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterE
5、ncoding("UTF-8");response.setCharacterEncoding("UTF-8");response.setContentType("application/json;charset=utf-8");ListresultList=getData();Gsongson=newGson();Stringresult=gson.toJson(resultList);//轉(zhuǎn)成json數(shù)據(jù)PrintWriterout=response.getWriter();out.write(result);out.
6、flush();out.close();}/***獲取數(shù)據(jù)*/privateListgetData(){ListresultList=newArrayList();resultList.add(newBrowserShare("Chrome",18.55F));resultList.add(newBrowserShare("Firefoc",19.99F));resultList.add(newBrowserShare("IE",54.13F));resultLis
7、t.add(newBrowserShare("Oher",0.49F));resultList.add(newBrowserShare("Oprea",1.63F));resultList.add(newBrowserShare("Safari",5.21F));returnresultList;}} 3、JSP頁(yè)面<%@pagelanguage="java"pageEncoding="UTF-8"%>
8、l;charset=utf-8">
HighchartsExample