java导出highcharts_Highcharts导出代码Java版
Highcharts是一個(gè)用純JavaScript編寫的圖表庫,提供了一個(gè)交互式的圖表添加到您的網(wǎng)站或Web應(yīng)用程序的簡單方法。Highcharts目前支持線,樣條,面積,areaspline,柱形圖,條形圖,餅圖和散點(diǎn)圖類型。
同時(shí)Highcharts提供將圖表導(dǎo)出為圖片或者PDF格式文件,只需要在頁面中載入exporting.js文件。
由于生成的圖表是SVG格式,所以導(dǎo)出時(shí)需要將數(shù)據(jù)發(fā)送到服務(wù)器端來進(jìn)行轉(zhuǎn)換。在exporting.js中默認(rèn)導(dǎo)出地址是http://export.highcharts.com/,另外在demo中也提供了php版本。
本文是介紹如何在java web application中來實(shí)現(xiàn)導(dǎo)出功能。
首選需要在lib中加入batik?jar包,如果是使用maven來管理項(xiàng)目,則在庫中只能找到1.6的版本,同時(shí)需要另外下載一個(gè)包(xml-apis-ext.jar)。
public class ExportHighFreqChartServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
public ExportHighFreqChartServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServerException, IOException {
String type = request.getParameter("type");
String svg = request.getParameter("svg");
String filename = request.getParameter("filename");
filename = filename==null?"chart":filename;
ServletOutputStream out = response.getOutputStream();
if (null != type && null != svg) {
svg = svg.replaceAll(":rect", "rect");
String ext = "";
Transcoder t = null;
if (type.equals("image/png")) {
ext = "png";
t = new PNGTranscoder();
} else if (type.equals("image/jpeg")) {
ext = "jpg";
t = new JPEGTranscoder();
} else if (type.equals("application/pdf")) {
ext = "pdf";
t = new PDFTranscoder();
}
response.addHeader("Content-Disposition", "attachment; filename="+ filename + "."+ext);
response.addHeader("Content-Type", type);
if (null != t) {
TranscoderInput input = new TranscoderInput(new StringReader(svg));
TranscoderOutput output = new TranscoderOutput(out);
try {
t.transcode(input, output);
} catch (TranscoderException e) {
out.print("Problem transcoding stream. See the web logs for more details.");
e.printStackTrace();
}
} else if (ext.equals("svg")) {
out.print(svg);
} else {
out.print("Invalid type: " + type);
}
} else {
response.addHeader("Content-Type", "text/html");
out.println("Usage:\n\tParameter [svg]: The DOM Element to be converted.
\n\tParameter [type]: The destination MIME type for the elment to be transcoded.");
}
out.flush();
out.close();
}
}
總結(jié)
以上是生活随笔為你收集整理的java导出highcharts_Highcharts导出代码Java版的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 获取服务器网络名_java-s
- 下一篇: java录入会员信息_java-第三章-