Export highcharts charts as images or PDF documents

Source: Internet
Author: User

Instance:

Function brandpie (args_datetime ){

VaR demis=windodoc ument. 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:
'Containere2 ',

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: 'proportion ',

Data: piedata

}]

});

}

});

}

In the above example:The exporting attribute is mainly used and exported. filename indicates the name of the file that is everywhere, and URL indicates the action to be exported. The default request parameter isType: exported file (.jpg/PDF)
Filename: indicates the exported file name; SVG: indicates some parameters during export.

Background processing action:

ImportJava. Io. ioexception;

ImportJava. Io. stringreader;

 

ImportJavax. servlet. servletoutputstream;

ImportJavax. servlet. http. httpservletresponse;

 

ImportOrg. Apache. batik. Transcoder. Transcoder;

ImportOrg. Apache. batik. Transcoder. transcoderexception;

ImportOrg. Apache. batik. Transcoder. transcoderinput;

ImportOrg. Apache. batik. Transcoder. transcoderoutput;

ImportOrg. Apache. batik. Transcoder. image. Specify Transcoder;

ImportOrg. Apache. batik. Transcoder. image. pngtranscoder;

ImportOrg. Apache. fop. SVG. pdftranscoder;

 

@ Suppresswarnings ("serial ")

Public
Class
ChartsexportactionExtendsBaseaction {

 

PrivateString
Type;

PrivateString
SVG;

PrivateString
Filename;

 

Public
Void
Setfilename (string filename ){

This. Filename = filename;

}

 

PublicString getfilename (){

Return
Filename;

}

 

Public
Void
Setsvg (string SVG ){

This. SVG = SVG;

}

 

PublicString getsvg (){

Return
SVG;

}

 

Public
Void
Settype (string type ){

This. Type = type;

}

 

PublicString GetType (){

Return
Type;

}

 

PublicString 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 =NewPngtranscoder ();

 

}Else
If
(Type. Equals ("image/JPEG ")){

EXT = "jpg ";

T =NewJpegtranscoder ();

 

}Else
If
(Type. Equals ("image/SVG + XML ")){

EXT = "SVG ";

}Else
If
(Type. Equals ("application/pdf ")){

EXT = "pdf ";

T =NewPdftranscoder ();

}

 

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 =NewTranscoderinput (

NewStringreader (SVG ));

Transcoderoutput output =NewTranscoderoutput (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 be 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 ){

//TodoAuto-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.