Application of jfreechart in webwork

Source: Internet
Author: User
Tags xslt
Preface:
Webwork is one of the most popular J2EE architectures. In actual projects, we often need to call a chart in the output stream of webwork, instead of directly using response. Out for output. So how should we implement it? This article will elaborate on the application of jfreechart in webwork. The debugging environment in this article is Windows + Tomcat. There are three parts:
1, Jfreechart Introduction
2, Introduction to webwork and configuration of jfreechart application environment
3, Example of jfreechart's Application in webwork

I:Jfreechart introduction:
Jfreechart is an open-source project. It is mainly used for a variety of charts, including pie charts, bar charts (common bar charts and stack bar charts), line chart, Area Chart, distribution chart, hybrid chart, Gantt chart, and some dashboard.
I. Basic knowledge of creating web charts using jfreechart
Jfreechart consists of three classes:
Org. jfree. Chart. servlet. chartdeleter
Org. jfree. Chart. servlet. displaychart
Org. jfree. Chart. servlet. servletutilities
Below is a brief introduction:
Chartdeleter inherits from httpsessionbindinglistener and is used to delete a session when it is closed.
The image file in the temporary object.
Servletutilities has a series of methods:
Savechartas *; savechartas * stores charts as images in different forms;
The sendtempfile method is overloaded many times to send the file stream to response;
Displaychart inherits from httpservlet for processing display images;

II:Introduction to webwork and configuration of jfreechart application environment
Webwork is an open-source Web application framework used to simplify Web-based application development.
The biggest advantage of webwork is its simplicity and flexibility. Webwork has a very small API that allows developers to quickly develop jobs.
Webwork is a combination of many features and applicability, including the use of variour view technology,
For example, assumerver pages (JSP), velocity, Extensible Stylesheet Language Transformations
Specification (XSLT) and jasperreporters. Also, webwork has excellent support for jfreechart.
Download webwork-2.0-beta2.zip from here.
Https://webwork.dev.java.net/servlets/ProjectDocumentList)
Extract the file, you can find the webwork-2.0 in the jfreechart-0.9.13.jar/lib/optional Directory.
You can download the latest jfreechart-0.9.15.zip from here.
Http://www.jfree.org/jfreechart/index.html)
This document uses jfreechart-0.9.15.zip.
The directory structure of the web application is as follows:
/Webapps/WEB-INF
/Webapp/WEB-INF/Web. xml
/Webapps/WEB-INF/webwork. TLD
/Webapp/WEB-INF/classes
/Webapps/WEB-INF/classes/xwork. xml
/Webapplication/WEB-INF/lib
Next, 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.jar
The following is a package that can be selected. You can decide whether to add the package based on the project requirements:
Cos-multipart.jar
Pell-multipart.jar
Velocity-dep-1.3.jar
2.1 configure your xwork. xml
File as in the following example, be sure to include the webwork-default.xml.
<! Doctype xwork public "-// opensymphony group // xwork 1.0 // en"

Http://www.opensymphony.com/xwork/xwork-1.0.dtd>
<Xwork>
<Include file = "webwork-default.xml"/>
<PackageName = "default"Extends= "Webwork-Default">
...
</Package>
</Xwork>

Webwork-default.xml this file is in the webwork-2.0-beta.jar.
2.2 modify the Web. xml file
Modify the Web. xml file to include webwork settings. For more information, see
$ Webwork/src/resource/web/WEB-INF/Web. xml file.
2.3 taglib Selection
If you do not want to use taglib, velocity, or XSLT, you only need to not include the content in Web. xml.
2.4 Log Settings
Webwork uses log4j. If your app server does not install log4j, you need to add log4j and 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 it to the path of your web application. Note that if the appserver also applies to log4j, you must be careful about version conflicts.
2.5 decompress jfreechart-0.9.15.zip
Jfreechart-0.9.15 and LIB/jfreechart-0.9.15.jar under the jcommon-0.9.0.jar directory, lib/gnujaxp. jar are the development and operation of the files required, put these three files to the/webapp/WEB-INF/lib directory.

3. Example of jfreechart's Application in webwork
After performing the above steps, we will proceed with the specific development process.
Step: first define result-types in xwork. xml
<Result-types>
<Result-type name = "chart"Class= "Pawpaw. Test. chartresult"/>
</Result-types>

Then define the action in xwork. xml.
<Action name = "viewmoderationchart"Class= "Pawpaw. Test. viewmoderationchartaction">
<Result name = "success" type = "chart">
<Param name = "width"> 400 </param>
<Param name = "height"> 300 </param> </result>
</Action>

The length and width of the chart are defined in Param.
Add the following content to the Web. xml file:
<Servlet>
<Servlet-Name> displaychart </servlet-Name>
<Servlet-Class> Org. jfree. Chart. servlet. displaychart </servlet-Class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> displaychart </servlet-Name>
<URL-pattern>/servlet/displaychart </url-pattern>
</Servlet-mapping>

Some source code is as follows:
PackagePawpaw. test;

ImportCom. opensymphony. webwork. servletactioncontext;
ImportCom. opensymphony. xwork. actioninvocation;
ImportCom. opensymphony. xwork. result;
ImportOrg. jfree. Chart. chartutilities;
ImportOrg. jfree. Chart. jfreechart;
ImportJava. Io. outputstream;
ImportJavax. servlet. http. httpservletresponse;
/*
* <P> Description: Change the chart file stream to httpservletresponse.
* The output stream obtained by the object is output in the browser </P>
* Author: pawpaw
* @ Version 1.0 12/15/2003
*/

Public ClassChartresultImplementsResult {
Jfreechart chart;
BooleanChartset =False;
Private IntHeight;
Private IntWidth;

Public VoidSetchart (jfreechart chart ){
This. Chart = chart;
Chartset =True;
}
// Set the image Length
Public VoidSetheight (IntHeight ){
This. Height = height;
}
// Set the Image Width
Public VoidSetwidth (IntWidth ){
This. Width = width;
}

Public VoidExecute (actioninvocation Invocation)ThrowsException {
Jfreechart chart = NULL;

If(Chartset ){
Chart =This. Chart;
}Else{
Chart = (jfreechart) Invocation. getstack (). findvalue ("chart ");
}

If(Chart = NULL ){
Throw NewNullpointerexception ("no chart found ");
}

// Replace the file stream with the output stream obtained through the httpservletresponse object.
Httpservletresponse response = servletactioncontext. getresponse ();
Outputstream OS = response. getoutputstream ();
Chartutilities. writechartaspng (OS, chart, width, height );
OS. Flush ();
}
}

Create an action class for jfreechart.
PackagePawpaw. test;

ImportJava. AWT. insets;
ImportJava. AWT. Font;
ImportJava. Io. printwriter;
ImportJavax. servlet. http. httpsession;
ImportOrg. jfree. Data .*;
ImportOrg. jfree. Chart .*;
ImportOrg. jfree. Chart. Plot .*;
ImportOrg. jfree. Chart. entity .*;
ImportOrg. jfree. Chart. URLs .*;
ImportOrg. jfree. Chart. servlet .*;
ImportOrg. jfree. Chart. Labels. standardpietooltipgenerator;
ImportOrg. jfree. util. rotation;
ImportCom. opensymphony. xwork. actionsupport;
/*
* <P> Description: outputs a diagonal line chart </P>
* Author: pawpaw
* @ Version 1.0 12/15/2003
*/

Public ClassViewmoderationchartactionExtendsActionsupport {
PrivateJfreechart chart;
PublicString execute ()ThrowsException {
// Create a chart file Dataset
Xyseries dataseries =NewXyseries (null );
For(IntI = 0; I <= 100; I ++ ){
Dataseries. Add (I, randomutils. nextint ());
}
Xyseriescollection xydataset =NewXyseriescollection (dataseries );
Valueaxis xaxis =NewNumberaxis ("raw marks ");// X axis Coordinate
Valueaxis yaxis =NewNumberaxis ("moderated marks ");// Y axis
// Set the chart Style
Chart =NewJfreechart (
"Moderation function ",// Chart title
Jfreechart. default_title_font,
NewXyplot (
Xydataset,
Xaxis,
Yaxis,
New
Standardxyitemrenderer (standardxyitemrenderer. Lines )),// Dataset
False// Generate URL link
);
Chart. setbackgroundpaint (Java. AWT. color. White );// Set the background color of the image
Font font =NewFont ("", Font. center_baseline, 20 );// Set the font and size of the image title
Texttitle _ Title =NewTexttitle (title );
_ Title. setfont (font );
Chart. settitle (_ title );
Return Super. Success;
}
PublicJfreechart getchart (){
ReturnChart;
}
}

Summary: This is just a simple example. For more information, see the webwork documentation and the jfreechart website.
References:
Http://www.jfree.org/jfreechart/index.html
Webwork documentation

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.