Export a Highcharts chart as a picture or PDF document

Source: Internet
Author: User

Instance:

function Brandpie (args_datetime) {

var Demis=window.document.getelementbyid ("First"). Value;

var chart;

$.ajax ({

Type: "Post",

Data: {demision: "3", querydate:args_datetime},

URL: "Report/dogetitempieorcolumn.action?querytype=" +querytype,

Success:function (data) {

var jsonarray=eval (' (' +data+ ') ');

var piedata=jsonarray.data;

Chart = new Highcharts.chart ({

Chart: {

Renderto: ' ContainerPie2 ',

Plotbackgroundcolor:null,

Plotborderwidth:null,

Plotshadow:false

},

Title: {

Text: ' Test '

},

Subtitle: {

Text: "Date:" +args_datetime,

X:-20

},

ToolTip: {

Percentagedecimals:2,

Pointformat: ' {series.name}:{point.percentage}% ',

},

Credits: {

Enabled:false,

} ,

exporting:{

filename: ' Test ',

URL: ' chart/doexport.action ',

                  },

Plotoptions: {

Pie: {

Allowpointselect:true,

Cursor: ' pointer ',

DataLabels: {

Enabled:false

},

Showinlegend:true

}

},

Series: [{

Type: ' Pie ',

Name: ' Percentage ',

Data:piedata

}]

});

}

});

}

In the example above: theexporting property is primarily used with an export, where filename represents the file name everywhere, and the URL represents the action that the export handles. The default request passes a parameter of type: Indicates the file type (. jpg/pdf) to export filename: Represents the exported file name; SVG: represents some of the parameters when exporting.

Action for background processing:

import java.io.IOException;

import Java.io.StringReader;

import Javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletResponse;

import Org.apache.batik.transcoder.Transcoder;

import org.apache.batik.transcoder.TranscoderException;

import org.apache.batik.transcoder.TranscoderInput;

import org.apache.batik.transcoder.TranscoderOutput;

import Org.apache.batik.transcoder.image.JPEGTranscoder;

import Org.apache.batik.transcoder.image.PNGTranscoder;

import Org.apache.fop.svg.PDFTranscoder;

@SuppressWarnings ("Serial")

Public class Chartsexportaction extends baseaction {

Private String type;

Private String svg;

Private String filename;

Public void setfilename (String filename) {

this. filename = filename;

}

Public String GetFileName () {

return filename;

}

Public void setsvg (String svg) {

this. svg = svg;

}

Public String Getsvg () {

return svg;

}

Public void setType (String type) {

this. Type = type;

}

Public String GetType () {

return type;

}

Public String Export () {

String type = this. GetType ();

String svg = this. getsvg ();

HttpServletResponse response = null;

SYSTEM.OUT.PRINTLN (SVG);

Servletoutputstream outputstream = null;

Try {

Response = this. GetResponse ();

Outputstream= Response.getoutputstream ();

System.out.println ("type:" +type);

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 ("Image/svg+xml")) {

ext = "SVG";

} else if (type.equals ("Application/pdf")) {

ext = "PDF";

t = new pdftranscoder ();

}

Response.AddHeader ("Content-disposition",

"Attachment;filename="

+ Java.net.URLEncoder.encode (

this. GetFileName (), "UTF-8") + "."

+ ext);

Response.AddHeader ("Content-type", Type);

if (null ! = t) {

Transcoderinput input = new transcoderinput (

New StringReader (SVG));

Transcoderoutput output = new transcoderoutput (OutputStream);

Try {

T.transcode (input, output);

} catch (Transcoderexception e) {

OutputStream

. Print ("Problem transcoding stream. See the Web logs for more details. ");

E.printstacktrace ();

}

} Else if (ext = = "svg") {

Outputstream.print (SVG);

} Else {

Outputstream.print ("Invalid type:" + type);

}

} Else {

Response.AddHeader ("Content-type", "text/html");

OutputStream

. println ("Usage:\n\tparameter[svg]: the DOM Element to being converted.\n\tparameter [type]: The Destinationmime type for the Elment to be transcoded. ");

}

Outputstream.flush ();

} catch (Exception e) {

TODO: handleexception

E.printstacktrace ();

} finally {

if (outputstream!= null)

Try {

Outputstream.close ();

} catch (IOException e) {

TODO Auto-generatedcatch block

E.printstacktrace ();

}

}

return null;

}

}

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.