Java exports the chart generated by highcharts as the image source code.

Source: Internet
Author: User

 

Jar package:

Required JS file: (you can download the JS file corresponding to highchartsthrough http://www.highcharts.com/download)

 

 

Below are some main parts

Web. xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <servlet>    <description>This is the description of my J2EE component</description>    <display-name>This is the display name of my J2EE component</display-name>    <servlet-name>SaveAsImage</servlet-name>    <servlet-class>com.foxconn.highcharts.demo.SaveAsImage</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>SaveAsImage</servlet-name>    <url-pattern>/SaveAsImage</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>basic_line.jsp</welcome-file>  </welcome-file-list></web-app>

 

Servlet

Package COM. foxconn. highcharts. demo; import Java. io. ioexception; import Java. io. outputstreamwriter; import Java. io. stringreader; import javax. servlet. servletexception; import javax. servlet. servletoutputstream; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. apache. batik. transcoder. transcoder; import Org. apache. baTi K. transcoder. transcoderexception; import Org. apache. batik. transcoder. transcoderinput; import Org. apache. batik. transcoder. transcoderoutput; import Org. apache. batik. transcoder. image. using Transcoder; import Org. apache. batik. transcoder. image. pngtranscoder; // import Org. apache. FOP. SVG. extends Transcoder; public class saveasimage extends httpservlet {/*****/Private Static final long serialversionuid = 1l;/*** con Structor of the object. */Public saveasimage () {super ();}/*** destruction of the servlet. <br> */Public void destroy () {super. destroy (); // just puts "Destroy" string in log} public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {dopost (request, response );} public void dopost (httpservletrequest request, httpservletresponse response) throws se Rvletexception, ioexception {/* has been written into PDF for a long time, but it still cannot be done because * Java is always reported. lang. nosuchmethoderror: Org. apache. aveon. framework. configuration. defaultconfiguration. <init> (ljava/lang/string;) V error. * if you know how to solve this error, please let me know and thank you first. **/Request. setcharacterencoding ("UTF-8"); // sets the encoding to solve the garbled string type = request. getparameter ("type"); string SVG = request. getparameter ("SVG"); string filename = request. getparameter ("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 convert Transcoder ();}/* else if (type. equals ("application/pdf") {ext = "pdf"; t = (Transcoder) New encrypted Transcoder ();} */else if (type. equals ("image/SVG + XML") ext = "SVG"; response. addheader ("Content-disposition", "attachment; filename =" + filename + "." + ext); response. addheader ("Content-Type", type); If (null! = T) {transcoderinput input = new transcoderinput (New stringreader (SVG); transcoderoutput = 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); outputstreamwriter writer = new outputstreamwriter (Out, "UTF-8"); writer. append (SVG); writer. close ();} 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 ();}/*** initialization of the servlet. <br> ** @ throws servletexception if an error occurs */Public void Init () throws servletexception {// put your code here }}

JSP part

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> 

You can click here to download the project source code.

Turn: http://blog.csdn.net/dengsilinming/article/details/7352054 sorry, always turn his.

 

 

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.