天天看點

将highcharts圖表導出為圖檔或pdf文檔

執行個體:

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: "日期:"+args_datetime,

                    x: -20

                   },

            tooltip: {

                percentageDecimals:2,

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

            },

            credits : {

             enabled : false ,

           } ,

exporting:{

                  filename:'測試',

                     url:'chart/doexport.action',

                  },

             plotOptions: {

                pie: {

                    allowPointSelect: true,

                    cursor: 'pointer',

                    dataLabels: {

                        enabled: false

                    },

                    showInLegend: true

                }

            },

            series: [{

                type: 'pie',

                name: '占比',

                data: pieData

            }]

        });

  }

 });

}

上例中:exporting屬性主要是用與導出的,其中filename表示到處後的檔案名,url表示導出處理的action。預設請求傳的參數為type:表示要導出的檔案類型(.jpg/pdf) filename:表示導出的檔案名;svg:表示導出時的一些參數。

背景處理的action:

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 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) {

              // TODO Auto-generatedcatch block

              e.printStackTrace();

           }

      }

      return null;

   }

}