Use of jfreechart

Source: Internet
Author: User

Jfreechart is a free Java chart library. jfreechart supports pie charts (2D and 3D), bar charts (horizontal and vertical, neatly stacked), line charts, scatter plots, sequence charts,

High and low open closed chart, candle holder chart, Gantt Chart, combined with plot, thermometer, dial and more. Jfreechart can be used in applications, such as Applets, Servlets, and JSP.

Jfreechart can generate many images. Here, we will briefly list three images (pie chart, bar chart, and graph) as an entry to learn how to integrate Struts into JSP.

1. Set up struts

First, we need to build a Struts framework. On this basis, we can develop and use jfreechart,

The detailed struts2 construction reference http://blog.csdn.net/itmyhome1990/article/details/36186059

Note: of course, no struts is required. You can run jfreechart by writing a Java Script in JSP, but we recommend that you use struts for standardization.


Ii. Download jfreechart

Jfreechart is open-source free software, (including the source code in this example) http://download.csdn.net/detail/itmyhome/7598033

The jfreechart version used in this article is a jfreechart-1.0.17


Iii. Environment Configuration

Myeclipse new web project, will download the jfreechart-1.0.17 under the lib directory under the jcommon-1.0.21.jar, jfreechart-1.0.17.jar

Import to the project. Add the following configuration in 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>/DisplayChart</url-pattern></servlet-mapping>

4. generate various charts★Pie Chart

Defaultpiedataset DPD = new defaultpiedataset (); DPD. setvalue ("jdk1.4", 60); DPD. setvalue ("jdk1.5", 15); DPD. setvalue ("jdk1.6", 25); DPD. setvalue ("jdk1.7", 25); DPD. setvalue ("jdk1.8", 25); jfreechart chart = chartfactory. createpiechart ("JDK version distribution is currently used by developers", DPD, true, false, false); string filename = ""; try {filename = servletutilities. savechartaspng (chart, 600,500, session);} catch (ioexception e) {e. printst Acktrace ();} // urlpiecharturl = request. getcontextpath () + "/displaychart? Filename = "+ filename;
In JSP, shows the pie chart (the same below)

★Bar Chart

Defaultcategorydataset dataset = new defaultcategorydataset (); dataset. addvalue (42, "language", "Java"); dataset. addvalue (17, "language", "C #"); dataset. addvalue (14, "language", "C ++"); dataset. addvalue (12, "language", "C"); dataset. addvalue (3, "language", "php"); dataset. addvalue (2, "language", "javsscript"); dataset. addvalue (1, "language", "objective-c"); dataset. addvalue (3, "language", "Python"); dataset. addvalue (4, "language", "other"); jfreechart chart = char Tfactory. createbarchart3d ("developers' first programming language distribution", "development language", "percentage", dataset, plotorientation. vertical, false); string filename = ""; try {filename = servletutilities. savechartaspng (chart, 700,500, null, session);} catch (ioexception e) {e. printstacktrace ();} barcharturl = request. getcontextpath () + "/displaychart? Filename = "+ filename;

★Graph

Timeseries = new Timeseries ("traffic trends for the last 30 days", day. class); timeseriescollection linedataset = new timeseriescollection (); Timeseries. add (New Day (01, 06,201 4), 10200); Timeseries. add (New Day (05, 06,201 4), 11200); Timeseries. add (New Day (10, 06,201 4), 12000); Timeseries. add (New Day (15, 06,201 4), 13200); Timeseries. add (New Day (20, 06,201 4), 11600); Timeseries. add (New Day (25, 06,201 4), 13200); time Series. add (New Day (30, 06,201 4), 12500); linedataset. addseries (Timeseries); jfreechart chart = chartfactory. createtimeserieschart ("traffic statistics timeline", "month", "Traffic", linedataset, true); xyplot plot = chart. getxyplot (); // set the grid background color. setbackgroundpaint (color. white); // set the grid vertical line color (PLOT. setdomaingridlinepaint (color. light_gray); // set the grid horizontal line color (PLOT. setrangegridlinepaint (color. light_gray); // set the main title chart. sett Itle (New texttitle ("traffic trends in the last 30 days"); chart. setantialias (true); string filename = ""; try {filename = servletutilities. savechartaspng (chart, 600,500, null, session);} catch (ioexception e) {e. printstacktrace ();} graphcharturl = request. getcontextpath () + "/displaychart? Filename = "+ filename;

V. garbled Problem

Garbled characters may occur during chart generation. The solution is as follows:

Standardcharttheme = new standardcharttheme ("cn"); standardcharttheme. setextralargefont (new font ("", Font. bold, 20); standardcharttheme. setregularfont (new font ("", Font. plain, 15); standardcharttheme. setlargefont (new font ("", Font. plain, 15); chartfactory. setcharttheme (standardcharttheme );
If the following error is reported during use:

Org. Apache. struts2.dispatcher. Dispatcher warn warning: cocould not find action or result:/jfreechart/displaychart? Filename=jfreechart-4239939260100283845.png there is no action mapped for namespace [/] and action name [displaychart] associated with context path [/jfreechart].-[unknown location]
The reason for the failure to display images in JSP is that the struts configuration is incorrect.

This is because the request. getcontextpath () + "/displaychart? Filename = "+ filename;

When we use chorme to view the image path, we can also see

The Servlet Path is intercepted by struts (because the interception configured by struts is<URL-pattern>/* </url-pattern>) So the corresponding action or result cannot be found.


Modify web. xml

<Filter-mapping> <filter-Name> struts2 </filter-Name> <URL-pattern>/admin/* </url-pattern> </filter-mapping> <! -- Or <filter-mapping> <filter-Name> struts2 </filter-Name> <URL-pattern> *. action </url-pattern> </filter-mapping> -->

Now the basic use of jfreechart is complete. This is just a simple chart display. For more complex functions, see other articles.

Project source code download: http://download.csdn.net/detail/itmyhome/7598033

Import the project to myeclipse and start the Tomcat browser. Input http: // localhost: 8088/jfreechart/index. jsp to demonstrate the program.


Welcome to the Java technology exchange group: 74955800


Reprinted please indicate the source: http://blog.csdn.net/itmyhome1990/article/details/36898497



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.