近期画油量曲线须要用到ZoomLine官网看了好几天。如今整理出来供大家參考使用
zoomline.html源码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>FusionCharts Free Documentation</title> <!-- 脚本区域 start --> <script type="text/javascript" src="js/FusionCharts.js"></script> <script type="text/javascript" src="js/FusionChartsExportComponent.js"></script> <!-- 脚本区域 end --> <script type="text/javascript"> //导出图片调用的方法 function startExport(){ var chart = getChartFromId("myFusionExport"); //生成的FusionCharts图表本身的标识 if (chart.hasRendered()){ chart.exportChart(); }else{ alert("Please wait for the chart to finish rendering, before you can invoke exporting"); } } </script> </head> <table width="98%" border="0" cellspacing="0" cellpadding="3" align="center"> <tr> <td valign="top" class="text" align="center"> <div id="myFusion" align="center"> FusionCharts. </div> <script type="text/javascript"> var chart = new FusionCharts("charts/ZoomLine.swf", "myFusionExport", "790", "450"); chart.setDataURL("data/zoomLine.xml"); chart.render("myFusion"); </script> </td> </tr> <input type='button' value='导出FusionCharts图片' onClick="startExport();" /> </table> </html>
zoomLine.xml源码
<chart palette="1" showLabels="0" showBorder="1" chartLeftMargin="10" chartRightMargin="5" chartTopMargin="15" chartBottomMargin="15" exportEnabled="1" exportAtClient="0" exportHandler="FCExporter" showExportDialog="1" animation="0" exportFileName="fusioncharts" exportDialogMessage="正在导出。请稍候..." rotateLabels="0" DIVLINES="Blur" DATATOOLTIP="0x004E98" showValues="0" allowPinMode="1" showVDivLines="1" drawToolbarButtons="1" lineThickness="1" oolBarBtnHPadding="0" mouseCursorColor="#FF0000" toolTipBarColor="FF0000" pinPaneBgColor="#FFFFFF" lineColor="#0000FF" divLineColor="#FF0000" background="#0000FF" bgColor="#FFFFFF" scrollColor="#FFFFFF" canvasBorderColor="#FF0000" toolbarButtonColor="FFFFFF" btnZoomOutTitle=" " zoomOutMenuItemLabel=" " btnResetChartTitle="缩小" btnSwitchtoZoomModeTitle="切换Pin模式" btnSwitchToPinModeTitle="切换Zoom模式" showToolBarButtonTooltext="0" toolbarButtonFontColor="000000" > <categories> <category name="02-03 07:00:05" /> <category name="02-03 06:59:35" /> <category name="02-03 06:59:05" /> <category name="02-03 06:58:35" /> <category name="02-03 06:58:04" /> <category name="02-03 06:57:34" /> <category name="02-03 06:57:04" /> <category name="02-03 06:56:34" /> <category name="02-03 06:56:04" /> <category name="02-03 06:55:33" /> </categories> <dataset> <set value="300" /> <set value="349" /> <set value="627" /> <set value="445" /> <set value="623" /> <set value="575" /> <set value="567" /> <set value="576" /> <set value="745" /> <set value="409" /> </dataset> </chart>
以上基本实现了图表的显示工作,假设须要导出还须要下面代码
FCExporterFormat.java源码
package com.fusioncharts.exporter.resources; import javax.servlet.http.HttpServletResponse; import com.fusioncharts.exporter.beans.ExportBean; public abstract class FCExporterFormat { abstract public Object exportProcessor(ExportBean exportBean); abstract public String exportOutput(Object exportObj, HttpServletResponse response); }
FCExporterIMG.java源码
package com.fusioncharts.exporter.resources; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import com.fusioncharts.exporter.ErrorHandler; import com.fusioncharts.exporter.FusionChartsExportHelper; import com.fusioncharts.exporter.beans.ChartMetadata; import com.fusioncharts.exporter.beans.ExportBean; import com.fusioncharts.exporter.encoders.BasicEncoder; import com.fusioncharts.exporter.encoders.JPEGEncoder; import com.fusioncharts.exporter.generators.ImageGenerator; public class FCExporterIMG extends FCExporterFormat { private ExportBean exportBean = null; public String exportOutput(Object exportObj, HttpServletResponse response) { String action = (String)exportBean.getExportParameterValue("exportaction"); String exportFormat = (String)exportBean.getExportParameterValue("exportformat"); String exportTargetWindow = (String)exportBean.getExportParameterValue("exporttargetwindow"); String fileNameWithoutExt = (String)exportBean.getExportParameterValue("exportfilename"); String extension = FusionChartsExportHelper.getExtensionFor(exportFormat.toLowerCase());; String fileName = fileNameWithoutExt+"."+ extension; StringBuffer err_warn_Codes = new StringBuffer(); BufferedImage chartImage = (BufferedImage)exportObj; boolean isHTML = false; if(action.equals("download")) isHTML=true; String noticeMessage = ""; String meta_values= exportBean.getMetadataAsQueryString(null,false,isHTML); if(action.equals("download")){ try { response.setContentType(FusionChartsExportHelper.getMimeTypeFor(exportFormat.toLowerCase())); OutputStream os = response.getOutputStream(); if(exportTargetWindow.equalsIgnoreCase("_self")){ response.addHeader("Content-Disposition", "attachment; filename=""+fileName+"""); //response.addHeader("Content-length",""+ios.length()); } else { response.addHeader("Content-Disposition", "inline; filename=""+fileName+"""); } if( exportFormat.toLowerCase().equalsIgnoreCase("jpg") || exportFormat.toLowerCase().equalsIgnoreCase("jpeg")){ JPEGEncoder jpegEncoder = new JPEGEncoder(); try { jpegEncoder.encode(chartImage,os); os.flush(); }catch(Throwable e){ //Unable to encode the buffered image System.out.println("Unable to (JPEG) encode the buffered image"); err_warn_Codes.append("E516,"); //os.flush(); // Note forward will not work in this case, as the output stream has already been opened // Hence we have to output the error directly. meta_values =exportBean.getMetadataAsQueryString(null,true,isHTML); // Reset the response to set new content type and use out instead of outputstream response.reset(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print(meta_values+noticeMessage+ErrorHandler.buildResponse(err_warn_Codes.toString(),isHTML)); return null; } chartImage=null; } else { BasicEncoder basicEncoder = new BasicEncoder(); try { basicEncoder.encode(chartImage,os,1F,exportFormat.toLowerCase()); os.flush(); }catch(Throwable e){ System.out.println("Unable to encode the buffered image"); err_warn_Codes.append("E516,"); //os.flush(); // Note forward will not work in this case, as the output stream has already been opened // Hence we have to output the error directly. meta_values =exportBean.getMetadataAsQueryString(null,true,isHTML); // Reset the response to set new content type and use out instead of outputstream response.reset(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print(meta_values+noticeMessage+ErrorHandler.buildResponse(err_warn_Codes.toString(),isHTML)); return null; } chartImage=null; } } catch (IOException e) { e.printStackTrace(); } } if(err_warn_Codes.indexOf("E") != -1) { meta_values =exportBean.getMetadataAsQueryString(null,true,isHTML); PrintWriter out; try { out = response.getWriter(); out.print(meta_values+noticeMessage+"&statusCode=1&statusMessage=successful"); } catch (IOException e) { e.printStackTrace(); } } return null; } public Object exportProcessor(ExportBean pExportBean) { exportBean = pExportBean; String stream = (String)exportBean.getStream(); ChartMetadata metadata= exportBean.getMetadata(); BufferedImage chartImage = ImageGenerator.getChartImage(stream,metadata); return chartImage; } }
FCExporter.java源码
package com.fusioncharts.exporter.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.fusioncharts.exporter.ErrorHandler; import com.fusioncharts.exporter.FusionChartsExportHelper; import com.fusioncharts.exporter.beans.ExportBean; import com.fusioncharts.exporter.resources.FCExporterFormat; public class FCExporter extends HttpServlet { public FCExporter() { super(); } public void init(ServletConfig config) throws ServletException { super.init(config); } public void destroy() { super.destroy(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext sc = getServletContext(); String WEB_ROOT_PATH = sc.getRealPath("/"); ExportBean localExportBean = FusionChartsExportHelper.parseExportRequestStream(request); String exportFormat = (String)localExportBean.getExportParameterValue("exportformat"); String exportTargetWindow = (String)localExportBean.getExportParameterValue("exporttargetwindow"); StringBuffer err_warn_Codes = new StringBuffer(); if (localExportBean.getMetadata().getWidth() == -1 || localExportBean.getMetadata().getHeight() == -1 || localExportBean.getMetadata().getWidth() == 0 || localExportBean.getMetadata().getHeight() == 0 ) { //If Width/Height parameter is not sent, the ChartMetadata will have width/height as -1 //Raise Error E101 - Width/Height not found err_warn_Codes.append("E101,"); } if (localExportBean.getMetadata().getBgColor() == null) { //Background color not available err_warn_Codes.append("W513,"); } if (localExportBean.getStream() == null ) { //If image data not available //Raise Error E100 err_warn_Codes.append("E100,"); } String exportAction = (String)localExportBean.getExportParameterValue("exportaction"); boolean isHTML = false; if(exportAction.equals("download")) isHTML=true; if(!exportAction.equals("download")) { String fileNameWithoutExt = (String)localExportBean.getExportParameterValue("exportfilename"); String extension = FusionChartsExportHelper.getExtensionFor(exportFormat.toLowerCase());; String fileName = fileNameWithoutExt+"."+ extension; err_warn_Codes.append(ErrorHandler.checkServerSaveStatus(WEB_ROOT_PATH,fileName)); } String pathToWebAppRoot = getServletContext().getRealPath("/"); localExportBean.addExportParameter("webapproot", pathToWebAppRoot); String meta_values= localExportBean.getMetadataAsQueryString(null,true,isHTML); if(err_warn_Codes.indexOf("E") != -1) { // There are errors - forward to error page writeError(response,""+isHTML,err_warn_Codes.toString(),meta_values,exportTargetWindow); } else { // get the exporter for this format //String path = FusionChartsExportHelper.getExporterFilePath(exportFormat).replace(" ",""); // gives with .jsp extension, let us remove the extension - this is a work-around until next release, where Servlets will also be supported //int indexOfDot = path.lastIndexOf("."); //String exporterClassName = path.substring(0, indexOfDot); try { //Class exporterClass = Class.forName(exporterClassName); Class exporterClass = Class.forName("com.fusioncharts.exporter.resources.FCExporterIMG"); FCExporterFormat fcExporter = (FCExporterFormat)exporterClass.newInstance(); Object exportObject = fcExporter.exportProcessor(localExportBean); String status= fcExporter.exportOutput(exportObject, response); } catch (ClassNotFoundException e) { err_warn_Codes.append("E404"); writeError(response,""+isHTML,err_warn_Codes.toString(),meta_values,exportTargetWindow); } catch (InstantiationException e) { err_warn_Codes.append("E404"); writeError(response,""+isHTML,err_warn_Codes.toString(),meta_values,exportTargetWindow); } catch (IllegalAccessException e) { err_warn_Codes.append("E404"); writeError(response,""+isHTML,err_warn_Codes.toString(),meta_values,exportTargetWindow); } } } private void writeError(HttpServletResponse response, String isHTML, String errorCodes, String otherMessages, String exportTargetWindow) { if(isHTML==null){ isHTML = "true"; } if(otherMessages==null){ otherMessages=""; } if(errorCodes==null){ errorCodes=""; } response.setContentType("text/html"); if(exportTargetWindow.equalsIgnoreCase("_self")){ response.addHeader("Content-Disposition", "attachment;"); } else { response.addHeader("Content-Disposition", "inline;"); } PrintWriter out; try { out = response.getWriter(); out.print(ErrorHandler.buildResponse(errorCodes,new Boolean(isHTML).booleanValue())) ; out.print(otherMessages); } catch (IOException e) { } } }
web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <display-name>FCExporter</display-name> <servlet-name>FCExporter</servlet-name> <servlet-class>com.fusioncharts.exporter.servlet.FCExporter</servlet-class> </servlet> <servlet-mapping> <servlet-name>FCExporter</servlet-name> <url-pattern>/FCExporter</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>zoomline.html</welcome-file> </welcome-file-list> </web-app>