Proficient in the implementation of Jfreechart statistical charts (i)

Source: Internet
Author: User

In the chart statistics feature, use the Open source project Jfreechart The simple and easy-to-use advantages of developing Web projects. The following is an analysis of the concept and implementation mechanism of Jfreechart. Finally, a practical example of chart statistics is given. <?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>

1 , Jfreechart the concept

Jfreechart is a free Java Chart Library, and it can be used in applications, Applets,servlets, and JSPs. It is open to full source code, but will be subject to the license limit if used in business. It is an Open-source graphical report engine middleware that is mainly used for a variety of charts, including pie, histogram (plain bar and Stack bar), line, area, distribution, blending, Gantt Chart, and some dashboards.

The Jfreechart development environment, because it is based on a Web browser chart presentation, requires a servlet engine or a Java EE application server (such as WEBSPHERE,TOMCAT, etc.). And the construction of the Web environment. The two packages that are required at development time are: Jfreechart and Jcommon. At present, the latest matching version is: jfreechart <?xml:namespace prefix = st1 ns = "Urn:schemas-microsoft-com:office:smarttags"/>0.9.20 Jcommon 0.9.4.

2 , with Jfreechart Create WEB basic knowledge and implementation mechanism of graphs

(1) The Jfreechart is composed mainly of three classes:

Org.jfree.chart.servlet.ChartDeleter//delete images in temp directory

Org.jfree.chart.servlet.DisplayChart//Display image

org.jfree.chart.servlet.servletutilities//processing Images

The following is a detailed description of each of the above classes:

Chartdeleter inherits from Httpsessionbindinglistener, which is used to delete an image file from a temporary directory when the session is closed.

Servletutilities has a series of methods:

Savechartas*;savechartas* is to store the chart in different forms as an image;

The Sendtempfile method is overloaded many times to send the file stream response;

Displaychart inherits from HttpServlet for processing display images;

(2) in your application Web.xml files must be configured with Displaychart;

Configuration forms such as:

Displaychart

Org.jfree.chart.servlet.DisplayChart

Displaychart

/servlet/displaychart

3 , Create an interactive WEB the implementation mechanism of the chart

Many situations require more than just a chart to be displayed on a browser, but a user can do interactive work directly on the chart, such as getting an informational cue, clicking on a part of the chart to show more detailed information, and so on. For this reason, the graphic is required to have interactive operation function. A map object must be defined for an image in HTML that requires an interactive function. To generate the corresponding map object based on an image, you need to create a chart with two parameters, which are the last two parameters in the Chartfactory.createbarchart3d method, both of which are of a Boolean value. They mean: whether to create ToolTips (tooltip) and whether to generate URLs. Corresponds to the title attribute of an area in map and the href attribute respectively.

To implement the function of a map object in Jfreechart, you need to generate a map object. But it needs to introduce another object: Chartrenderinginfo. But in Jfreechart there is no direct way to generate map data directly from a graph object, it requires an intermediate object to transition, and this object is Chartrenderinginfo.

The flowchart for generating the map data is shown in Figure 1.1:

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml"/>

Figure 1.1 map Data Flow chart

As shown in the figure above, the Chartutilities class is the core of the entire process, with objects around it such as data objects or files. This process is described briefly as follows:

First, a Chartrenderinginfo object is created and passed in as the last argument when the writechartasjpeg of the chartutilities is invoked. When this method is called, an image file and a Chartrenderinginfo object filled with the map data are generated, and there is no way to get the specific map data. We must also use the Chartutilities Writeimagemap method to read the Chartrenderinginfo object.

The code fragment that gets the map data is as follows:

FileOutputStream fos_jpg = null;//An object that defines a file output stream fos_jpg

FileOutputStream FOS_CRI = null; Defines an object for a file output stream FOS_CRI

try{

Use different classes based on different types of charts, and here's how to do the pie chart

Pieplot plot = (Pieplot) chart.getplot ();//define a pie chart

Plot.seturlgenerator (new Standardpieurlgenerator (URL));

Set URL Properties

Plot.settooltipgenerator (New Standardpietooltipgenerator ());

Set ToolTips

fos_jpg = new FileOutputStream ("d://example.jpg");

An object that instantiates an fos_jpg output file stream d://example.jpg

Chartutilities.writechartasjpeg (Fos_jpg,100,chart,400,300,info);

Save Fos_jpg File

FOS_CRI = new FileOutputStream ("d://example.map");

An object that instantiates an FOS_CRI output file stream d://example.jpg

PrintWriter w = new PrintWriter (FOS_CRI);

Output-instantiated file stream object

Chartutilities.writeimagemap (W, mapname, info);

Writes the map of an image to the PrintWriter object W

W.flush ();

}

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.