Application of JFreeChart in Webwork

xiaoxiao2021-03-06  176

Preface: WebWork is one of the popular J2EE's major architectures. In the actual project, we tend to call a chart Chart in the output stream of Webwork, instead of direct output with Response.out. So how should we do it? This article will explain in detail the application of JFreeChart in Webwork. This paper debugging environment is Windows2000 Tomcat5.0. A total of three parts: 1. JFreeChart introduction 2, WebWork Introduction and Webwork, JFreeChart Applications 3, JFreeChart's Application of Application in Webwork 1: JFreechart Introduction: JFreeChart is open source project, it is mainly used Various charts include: pie chart, histogram (ordinary column chart, stack column), line diagram, regional diagram, distribution map, mixing chart, Gantt chart, and some instrument panels, etc. I. The basic knowledge of JFreeChart creates a web chart is mainly consisting of three classes: org.jfree.chart.servlet.chartdeleterorog.jfree.chart dervlet.displayChartorg.jfree.chart.Servlet.ServletUtilities are simply introduced below : Chartdeleter inherits from httpsessionBindingListener for implementation When the session is closed, delete the image file in the temporary cat. ServletUTILITIES has a series of methods: SaveChartas *; SaveChartas * is stored as an image in different forms; the SendTempFile method is overloaded many times, used to send the file stream to response; DisplayChart inherits from httpservlets to process display images; 2: WebWork Introduction and Webwork, JFreeChart Application Environment Configuration WebWork is an open web application framework for simplifying web-based applications. The greatest advantage of Webwork is its simplicity and flexibility. Webwork has a small API that enables developers to develop work quickly. WebWork is a combination of many features and applicability, including using Variour View technology, such as JavaServer Pages (JSP), Velocity, Extensible Stylesheet Language Transformations Specification (XSLT), and JasperReporters. Also, Webwork has very good support to JFreeChart. Download Webwork-2.0-beta2.zip from here. (

https://webwork.dev.java.net/servlets/projectdocumentlistListListListListListListListListListListListListListList

) Unpack files, you can find JFreeChart-0.9.13.jar in the web-2.0 / lib / optional directory. You can also download the latest JFreeChart-0.9.15.zip from here. (

http://www.jfree.org/jfreechart/index.html

) This article uses JFreeChart-0.9.15.zip. Directory structure diagram of the web application as follows: / WebApp / Web-INF /WEBAPP/Web-inf/web.xml /Webapp/web-inf/webwork.tld / webApp / web-inf / class / webapp / web-inf /classes/xwork.xml / webapp / web-inf / lib Next Please be sure to put the following packages under the / WebApp / Web-INF / LIB directory. 1. Commons-logging.jar 2. OGNL-2.5.1.jar 3. Oscore-2.2.1.jar 4. XWORK-1.0-beta.jar 5. WebWork-2.0-beta.ja Next is a package that can be selected, specifically determined whether to add: cos-multipart.jar pell-multiple.jar velocity-dep-1.3.jar2.1 Configure your xwork.xml file image As in the following example, be careful to include WebWork-Default.xml. ... Webwork-default.xml This file is in web-2.0-beta.jar. 2.2 Modify the web.xml file to modify the web.xml file to include the WebWork setting, how to set the content, please see the $ web / src / resource / web / web-inf / web.xml file. 2.3 Selection of Taglib section If you don't plan to use taglib, velocity, or xslt, just just need to include these content in web.xml. 2.4 Log setting WebWork Use log4j, if your app server is not installed, you need to add log4j, copy the log4j.jar file to the appropriate lib directory. If you use Tomcat, the installation directory is $ Tomcat_home / lib, of course, you can also install the path to your web application. But what you need to pay attention is if appserver also applies to Log4j, you need to be careful to conflict. 2.5 Decompress the JFreeChart-0.9.15.zipjfreechart-0.9.15 directory JFreechart-0.9.15.jar and lib / jcommon-0.9.0.jar, lib / gnujaxp.jar are the development of the file that runs, put this Three files are placed in the / webApp / web-inf / lib directory. Three JFreechart's specific example of the application in WebWork After the above steps, we will make specific development processes.

Steps: First at xwork.xml - define result-type then in xwork.xml - definition Action 400 300 where param defines the length width of the Chart chart. Then add the following in the web.xml file: Displaychart org.jfree.chart.servlet.displaychart Displaychart / sertern> Some source code is as follows: package paw.test; import com .opensymphony.webwork.ServletActionContext; import com.opensymphony.xwork.ActionInvocation; import com.opensymphony.xwork.Result; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import java.io.OutputStream; Import javax.servlet.http.httpservletResponse; / * *

Description: Switch the Chart file into the output stream obtained by httpservletresponse * object Output in the browser * author: Pawpaw * @version 1.0 12/15/2003 * /

public class ChartResult implements Result {JFreeChart chart; boolean chartSet = false; private int height; private int width; public void setChart (JFreeChart chart) {this.chart = chart; chartSet = true;} // set the image length

public void setHeight (int height) {this.height = height;} // set the width of the image public void setWidth (int width) {this.width = width;} public void execute (ActionInvocation invocation) throws Exception {JFreeChart chart = null ; If (chartset) {chart = this.Chart;} else {chart = (jfreechart) Invocation.getStack (). FindValue ("chart");} if (chart == null) {throw new nullpointRexception ("no chart found ");} // Strong file streams into output streams obtained by HTTPSERVLETRESPONSE objects

HttpServletResponse response = ServletActionContext.getResponse (); OutputStream os = response.getOutputStream (); ChartUtilities.writeChartAsPNG (os, chart, width, height); os.flush ();}} Create JFreeChart action class. Package paw.test; import java.awt.insets; import java.awt.font; import java.io.printwriter; import javax.servlet.http.httpsession; import org.jfree.data. *; import org.jfree.chart Import.plot. *; import org.jfree.chart.entity. *; import org.jfree.chart.urls. *; import org.jfree.chart.servlet. *; import org. JFree.Chart.Labels.standardPietooltipGenerator; import Org.jfree.util.Rotation; import com.opensymphony.xwork.actionsupport;

/ * *

Description: Output a slash chart * author: Pawpaw * @version 1.0 12/15/2003 * /

Public class viewModerationchartAction Extends ActionSupport; public string execute () throws exception {// Create a Chart file data set

XYSeries DataSeries = new xyseries (null); for (int i = 0; i <= 100; i ) {dataseries.add (i, randomutils.nextint ());} xyseriescollection xydataset = new xyseriescollection (dataseries); valueaxis xaxis = New NumBeraxis ("Raw Marks"); // X Bar Coordinate Valueaxis Yaxis = New Numbraxis ("MODERATED MARKS"); // Y Bearings // Setting CHART Style Chart = New Jfreechart ("Modression Function", // Chart title

JFreeChart.default_title_font, new xyplot (XyDataSet, Xaxis, Yaxis, New StandardxyItemRenderer (StandardxyItemRenderer (StandardxyItemRenderer (StandardxyItemRereMrendere (StandardxyItemRenderer (STANDARDREITETEER.LINES)), // Data Set False // Generate URL Link

); Chart.setbackgroundpaint (java.awt.color.white); // Setting the background color font font = new font ("black body", font.center_baseline, 20); // Set the font and size of the picture title

Texttitle _title = new texttitle; _title.setfont (font); chart.settitle; return super.success;} public jfreechart getchart () {return chart;}} Summary: This is just a simple example, if If you want to know in depth, you can refer to the Webwork documentation and the JFreeChart website. Reference:

http://www.jfree.org/jfreechart/index.html

Webwork English documentation

转载请注明原文地址:https://www.9cbs.com/read-128249.html

New Post(0)