JSP jfreechart experiences and examples

Source: Internet
Author: User

The latest version of jfreechartis jfreechart-1.0.11.zip. You can find it at http://www.jfree.org/jfreechart/index.html.
Ii. basic use of jfreechart
Regardless of the created graph, jfreechart follows these steps:
1. Create a dataset. All data is stored in dataset. (Create a data source (Dataset) to include the data to be displayed in the graph)
2. Create a jfreechart. Import data from dataset to jfreechart. (Create a jfreechart object to represent the image to be displayed)
3. Set the display attribute of jfreechart. This step can be omitted. The default jfreechart display attribute is used.
3. Render charts. Generates an image.
4. Page image display.
Important classes and interfaces:
Org. jfree. Data. General. dataset interfaces to be implemented by all data source classes
Org. jfree. Chart. chartfactory is used to generate a jfreechart object.
Org. jfree. Chart. jfreechart all the adjustments to the image are made through it. Oh !!
Org. jfree. Chart. Plot. Plot obtains it through the jfreechart object, and then adjusts the external part of the image (for example, axis) through it.
Note: It has many sub-classes, generally involving its sub-classes!
Org. jfree. Chart. Renderer. abstractrenderer obtains it through the jfreechart object, and then adjusts the internal part of the graph (for example, the type of the line) through it. Similarly, for different types of report charts, it has different subclass implementations! In this example, we call it Renderer.

Iii. Examples of jfreechart
Examples in Web Applications
Package com. lzk. Bean;
Import java. AWT. color;
Import java. AWT. Font;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import java. Text. numberformat;
Import java. Text. simpledateformat;
Import javax. servlet. http. httpsession;
Import org. jfree. Chart. chartfactory;
Import org. jfree. Chart. chartrenderinginfo;
Import org. jfree. Chart. chartutilities;
Import org. jfree. Chart. jfreechart;
Import org. jfree. Chart. axis. dateaxis;
Import org. jfree. Chart. axis. numberaxis;
Import org. jfree. Chart. entity. standardentitycollection;
Import org. jfree. Chart. Labels. standardxytooltipgenerator;
Import org. jfree. Chart. Plot. xyplot;
Import org. jfree. Chart. Renderer. XY. xyitemrenderer;
Import org. jfree. Chart. Renderer. XY. xylineandshaperenderer;
Import org. jfree. Chart. servlet. servletutilities;
Import org. jfree. Chart. Title. texttitle;
Import org. jfree. Data. Time. Day;
Import org. jfree. Data. Time. Timeseries;
Import org. jfree. Data. Time. timeseriescollection;
Import org. jfree. Data. XY. xydataset;
Import org. jfree. UI. rectangleinsets;
/**
* Graph Creation
*/
Public class linexychart
{
/**
* Returns the file name of the generated image.
* @ Param session
* @ Param pw
* @ Return: generate the image file name
*/
Public String getlinexychart (httpsession session, printwriter PW)
{
Xydataset dataset = This. createdateset (); // create a dataset
String filename = NULL;
// Create a jfreechart
Jfreechart chart = chartfactory. createtimeserieschart (
"Jfreechart time curve sequence diagram", // Title
"Date", // X-axis label
"Price", // y-axis label
Dataset, // data
True, // create legend?
True, // generate tooltips?
False // generate URLs?
);
// Set the display attribute of jfreechart to adjust the external part of the image.
Chart. setbackgroundpaint (color. Red); // you can specify the background color of a curve.
// Set the font size and shape
Font font = new font ("", Font. Bold, 16 );
Texttitle Title = new texttitle ("jfreechart time curve sequence diagram", font );
Chart. settitle (title );
// Subtitle
Texttitle subtitle =
New texttitle ("subtitle", new font ("", Font. Bold, 12 ));
Chart. addsubtitle (Subtitle );
Chart. settitle (title); // Title

// Set the title character of the graph.
// Timeseries S1 = new Timeseries ("historical curve", Day. Class); Chinese Character
Legendtitle legengtitle = chart. getlegend ();
Legengtitle. setitemfont (font );

Xyplot plot = (xyplot) Chart. getplot (); // obtain the image canvas
Plot. setbackgroundpaint (color. lightgray); // sets the grid background color.
Plot. setdomaingridlinepaint (color. Green); // you can specify the domain axis color.
Plot. setrangegridlinepaint (color. White); // you can specify the grid horizontal line color.
Plot. setaxisoffset (New rectangleinsets (5.0, 5.0, 5.0, 5.0); // set the distance between the curve and the XY axis
Plot. setdomaincrosshairvisible (true );
Plot. setrangecrosshairvisible (true );
Xyitemrenderer r = plot. getrenderer ();
If (r instanceof xylineandshaperenderer)
{
Xylineandshaperenderer Renderer = (xylineandshaperenderer) R;
Renderer. setbaseshapesvisible (true );
Renderer. setbaseshapesfilled (true );
Renderer. setshapesvisible (true); // you can specify whether the curve displays data points.
}
// Set the Y axis
Numberaxis numaxis = (numberaxis) plot. getrangeaxis ();
Numberformat numformater = numberformat. getnumberinstance ();
Numformater. setminimumfractiondigits (2 );
Numaxis. setnumberformatoverride (numformater );
// Set the prompt information
Standardxytooltipgenerator tipgenerator = new standardxytooltipgenerator (
"Historical information {1}, {2})", new simpledateformat ("mm-dd"), numformater );
R. settooltipgenerator (tipgenerator );
// Set the X axis (date axis)
Dateaxis axis = (dateaxis) plot. getdomainaxis ();
Axis. setdateformatoverride (New simpledateformat ("mm-dd "));
Chartrenderinginfo info = new chartrenderinginfo (
New standardentitycollection ());
Try
{
Filename = servletutilities. savechartaspng (chart, 500,300, info,
Session); // generate an image
// Write the image map to the printwriter
Chartutilities. writeimagemap (PW, filename, info, false );
}
Catch (ioexception E)
{
E. printstacktrace ();
}
PW. Flush ();
Return filename; // return the file name of the generated image
}
/**
* Create a dataset required to generate a graph.
* @ Return returns the dataset.
*/
Private xydataset createdateset ()
{
Timeseriescollection dataset = new timeseriescollection (); // time curve data set
Timeseries S1 = new Timeseries ("historical curve", Day. Class); // create a time data source. Each // Timeseries is a curve on the graph.
// S1.add (New Day (day, month, year), value), add data point information
S1.add (New Day (1, 2, 2006), 123.51 );
S1.add (New Day (2, 2, 2006), 122.1 );
S1.add (New Day (3, 2, 2006), 120.86 );
S1.add (New Day (4, 2, 2006), 122.50 );
S1.add (New Day (5, 2, 2006), 123.12 );
S1.add (New Day (6, 2, 2006), 123.9 );
S1.add (New Day (7, 2, 2006), 124.47 );
S1.add (New Day (8, 2, 2006), 124.08 );
S1.add (New Day (9, 2, 2006), 123.55 );
S1.add (New Day (10, 2, 2006), 122.53 );
Dataset. addseries (S1 );
Dataset. setdomainispointsintime (true );
Return dataset;
}
}

Display images in JSP files
First in the Web ApplicationProgramAdd the following to the deployment file web. xml:Code:

<! -- Image display: Use a dedicated servlet for display. This completes path search and ing. -->
<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>
Then display the image in JSP
Complete JSP file:
<! -- File name: timeline. jsp -->
<% @ Page contenttype = "text/html; charset = gb2312" pageencoding = "gb2312" %>
<% @ Page import = "com. Hong. Bean. linexychart" %>
<% @ Page import = "Java. Io. printwriter" %>
<%
Linexychart xychart = new linexychart ();
String filename = xychart. getlinexychart (Session, New printwriter (out ));
String graphurl = request. getcontextpath () + "/servlet/displaychart? Filename = "+ filename;
%>
<HTML>
</Head>
<Title> jfreechart example </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>

</Body>
</Html>

Related Article

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.