Several core object classes in JFreeChart

xiaoxiao2021-03-06  50

Source:

http://blog.9cbs.net/sjl_jaly/archive/2004/12/10/21166.aspx

Several core object classes in JFreechart:

JFreechart

Several core object classes in JFreechart:

The role of the class name class and the simple description of the JFreeChart chart object, and the final expression of any type of chart is customized in this object. The JFreeChart engine itself provides a factory class for creating different types of chart object XXXXDataSet dataset objects for providing data used in the chart. According to different types of charts, there should be many types of data set object classes. Commonly used data set objects include: Column Data Set Object DefaultCategoryDataSet, Pie Data Set Object Defauldpiedataset and Curve Data Set Object DEFAULTTABLEDATASETXXXXXPLOTTABLEDATASETXXXXPLT TOYDATASETXXXXPLOT Chart Area object, basically this object determines what styles, when you create this object, you need AXIS, Renderer and support for data set objects. Common PLOT objects are: histogram categoryplot, pie chart PIEPLOT and graph xyplot. XXXXXAXIS is used to process two axes of the chart: longitudinal axis and horizontal axis. Commonly used Numberaxis data axes, Dateaxis date axis. XXXXXRENDERER is responsible for how to display a chart object. Common renderer has a defaultcategoryItemRenderer histogram display StandardxyItemRenderer graph display. The renderexxxxxurlgenerator of the pie chart is used to generate the mouse of each item in the web chart. Click on the link. Common Urlcenerator has StandardPieRlGenerator StandardPieRlGenerator StandardxyurlGeneratorXxxxTooltipGenerator to generate a help prompt for image, and different types of charts correspond to different types of toolkeeping classes. Commonly used: StandardxyTooltipGeneratorStandardcategoryTooltipGeneratorStandardpieItemLabelgenerator

The general creation of the chart is:

1. Read data from the database

2. Save data to the dataset object corresponding to the chart

3. Create a coordinate axis object

4. Create a rendrer object

5. Create a Plot object

6. Create a JFreeChart object

7. Generate a picture file (or binary flow)

Examples of the web application provided by JFreeChart illustrates the above steps:

One. Column.

Public Static String Generatebarchart (Date Hitdate, HttpSession Session, PrintWriter PW) {

String filename = NULL;

Try {

// Retrieve List of Webhits

WebhitDataSet whitataset = new webhitdata ();

ArrayList List = whdataset.getdataBysection (Hitdate); // Get the data set

// throw a Custom NodataException if there is no data

IF (list.size () == 0) {

System.out.println ("No Data Has Been Found";

Throw new nodataException ();

}

// Create and Populate a categoryDataSetiterator it = list.listiterator ();

DefaultcategoryDataSet DataSet = New Default ();

While (ore.hasnext ()) {

Webhit wh = (webhit) iter.next ();

Dataset.addValue (New Long (Wh.Gethitcount ()), "Hits", Wh.GetSection ());

} // Save the data set to

DataSet object

// Create the Chart Object

CategoryAxis categoryaxis = new categoryaxis ("");

Valueaxis valueaxis = new numberaxis ("); // Create a coordinate axis

Barrenderer RENDERER = New barrenderer (); // Create renderer

Renderer.SetItemURLGENERATOR ("XY_CHART.JSP", "Series", "section"); // Create a URLGENERATOR. No connection is required to set the first parameter to "###".

Renderer.SetTooltipGenerator (New StandardCategoryTooltipGenerator ()); // Create a prompt tab

Plot plot = new categoryplot (Dataset, CategoryAxis, Valueaxis, Render); // Create a chart area object

JFreeChart Chart = new jfreechart ("", jfreechart.default_title_font, plot, false;

Chart.SetBackgroundPaint (java.awt.color.white); // Create a chart

// Write the Chart Image to the Temporary Directory

ChartRenderingInfo Info = New ChartRenderingInfo (New StandardentityCollection ()); // This tool class is not described above. When the mouse is moved to the picture, the prompt information is implemented with MAP, which is generated by this class.

FileName = servletutilities.savechartaspng (Chart, 500, 300, info, session); // Save Chart as a file

// write the image map to the printwriter

ChartUTILITIES.WRITEIMAGEMAP (PW, FILENAME, INFO); // Generate

MAP information. This information is written

In the output stream of the PW, the output flow here is

RESPONSE.OUT. That is, directly output to the page.

Pw.flush ();

} catch (nodataexception e) {

System.out.println (E.TOString ());

FILENAME = "public_nodata_500x300.png";

} catch (exception e) {

System.out.println ("Exception -" E.TOString ());

E.PrintStackTrace (System.out);

FILENAME = "public_error_500x300.png";

Return FileName;

}

two. Chart shows:

<%

......

String filename = WebHitchart.generateBarchart (DDATE, SESSION, New PrintWriter); // Get the file name of the picture. Note that the statement is written at the beginning of the JSP file (bar_chart.jsp), and the package class of OUT is used as a parameter. After accessing the page, you can see the content output of the generated MAP hotspot and then the beginning of the page. That is to say, it is best to write these sentences before the page is output.

String graphURL = Request.getContextPath () "/ servlet / displaychart? Filename =" filename; // Displays the serm full path name, the other file name is the parameter of the servlet

......

%>

......

SRC specifies the full path name of the servlet, and usermap specifies the hotspot used.

The servlet must be configured in the web-inf / web.xml file:

displaychart

org.jfree.chart.servlet.displaychart

displaychart

/ servlet / displaychart

The role of the servlet is to write the binary flow of the image file to the RESPONSE output stream, and it is displayed in the client.

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

New Post(0)