Implementation of JFreeChart statistics chart (1)

zhaozj2021-02-16  59

In the chart statistics function, it is achieved by using open source project JFreeChart in developing web projects. Below is an analysis of the concept and implementation mechanism of JFreeChart. Finally, a chart statistics application instance is given.

1, JFreechart concept

JFreeChart is a free Java Chart Library and it can be used in applications, applets, servlets, and jsps. It is the complete source code, but if it is used in the business, it will be limited by this license. It is an open source graphical report engine middleware, mainly used to make a variety of charts, including pie chart, histogram (ordinary column chart, stack post chart), line map, regional map, distribution map, mix Figure, Gantt chart and some instrument panels, etc.

JFreeChart's development environment, because it is a web browser-based chart, therefore requires a servlet engine or a J2EE application server (such as WebSphere, Tomcat, etc.). And the construction of the web environment. Two files that must be in development are: JFreeChart and JCommon. The latest version of the company is: jfreechart

0.9.20

JCOMMON 0.9.4.

2. Create the basic knowledge and implementation mechanism of the web chart with JFreeChart

(1) JFreeChart is mainly composed of three classes:

Org.jfree.Chart.Servlet.Chartdeleter // Delete images in the temporary directory

Org.jfree.Chart.Servlet.displayChart // Display Image

Org.jfree.Chart.Servlet.ServletUtIlities // Processing Images

The following is a detailed description of the above classes:

Chartdeleter inherits from httpsessionBindingListener for implementation of image files in the temporary directory when session is closed.

ServletUtilities has a series of methods:

SaveChartas *; SaveChartas * is to store charts as images in different forms;

The SendTempFile method was overloaded many times and used to send the file stream to response;

DisplayChart inherits from httpservlets to process display images;

(2) In your application, the web.xml file must be configured in DISPLAYCHART;

Configuration forms such as:

DisplayChart

Org.jfree.Chart.Servlet.displayChart

DisplayChart

/ servlet / displaychart

3, create an implementation mechanism for interactive web chart

Many things are not only requiring a chart on the browser, but also the user can do interact directly on the chart, such as obtaining information prompts, clicking on a portion of the chart for more details. This graphic is required to have an interactive function for this purpose. In HTML requires an image with interactive function, you must define a MAP object to the image. To generate a corresponding MAP object according to an image, you need to have two parameters when you create a chart. These two parameters are the last two parameters in the ChartFactory.createBarchart3D method. The types of these two parameters are Boolean . They mean: whether to create a tooltip and whether the URL is generated. The Title attributes of an area in Map are respectively corresponding, respectively, and HREF attributes.

In jfreechart's functionality to implement the MAP object, you need to generate an MAP object. But it needs to introduce another object: ChartRenderingInfo. However, in JFreeChart does not have a direct way to generate MAP data directly, it requires an intermediate object to transition, this object is ChartrenderingInfo. Flowchart generating MAP data is shown in Figure 1.1:

Figure 1. Flowchart of 1 MAP data

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

First create a ChartRenderingInfo object and pass it as the last parameter when calling ChartUTILITIS WriteChartasjpeg. When calling this method, an image file and a ChartRenderingInfo object that fills MAP data will be generated. With this object or there is no way to obtain specific MAP data, we must also read the ChartRenderingInfo object with the WriteImageMap method of ChartUTILITIES.

The code segment for obtaining MAP data is as follows:

FileOutputStream fos_jpg = null; // Define a file output stream object fos_jpg

FileOutputStream Fos_Cri = null; // Define a file output stream object FOS_CRI

Try {

// Use different classes according to different types of charts, below is the operation of the pie chart

PIEPLOT PLOT = (PIEPLOT) Chart.getPlot (); // Define a pie chart

Plot.setURLGenerator (New Standardpieurlgenerator (URL));

// Set URL attribute

Plot.SetTooltipGenerator (New StandardPietooltipGenerator ());

// Set the tooltip

FOS_JPG = New FileOutputStream ("D: //example.jpg");

/ / Instantiate a 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");

/ / Instantiate a FOS_CRI output file stream D: //example.jpg

PrintWriter W = New PrintWriter (fos_Cri);

/ / Output instantiated file stream object

Chartutilities.WriteImageMap (W, MapName, Info);

// Write the image of the MAP to the PrintWriter object W

w.flush ();

}

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

New Post(0)