Jfreechart introduction and classic getting started

Source: Internet
Author: User
1. Obtain jfreechart.
Jfreechart is a project of jfreechart on the open source website sourceforge.net. Its main products include:
1. jfreereport: Report solution Tool
2. jfreechart: Java graphics solution (Application/applet/servlet/jsp)
3. jcommon: public class libraries of jfreereport and jfreechart
4. jfreedesigner: jfreereport report design tool

We can get the latest version and related materials from the jfree official website (but jfree document takes US $40 ),
Get address: http://www.jfree.org/jfreechart/index.html (for a brief introduction)
The latest version: jfreechart_0.9.21.zip is used as an example.

Ii. jfreechart configuration and installation
1、decompress jfreechart_0.9.21.zip to the specified location, where source is the source code of jfreechart, jfreechart-0.9.21-demo.jar
Is the example Program (this part is left for everyone to study)
2, in order to configure successfully, we need to pay attention to the following three files: jfreechart-0.9.21.jar, lib/jcommon-0.9.6.jar,
LIB/gnujaxp. Jar
3. For application development, copy the preceding three files to % java_home %/lib and add them to the environment variable classpath.
For web development, take a WEB Project Test in Tomcat as an example:
Copy the above three files to test/WEB-INF/lib, then modify the test/WEB-INF/Web. xml file, add the following code in it:
<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>
Now the configuration of jfreechart is complete. Now we can develop jfreechart. The jfreechart class is worth mentioning here.
The compatibility before and after structure design is not very good. The structure of the class library in different versions of jfreechart may be different, and sometimes the source code may need to be checked. If
The source code font may need to be changed based on the perception when displaying Chinese characters, but I personally think this version is better than the previous version.

Iii. Introduction to jfreechart Functions
Jfreechart is currently the best Java graphics solution, which can basically meet the current graphics needs, mainly including the following aspects:
Pie charts (2D and 3D): Pie Chart (plane and three-dimensional)
Bar Charts (regular and stacked, with an optional 3D effect): bar chart
Line and area charts: Graph
Scatter plots and bubble charts
Time series, high/low/open/close charts and candle stick charts: Time Sequence Diagram
Combination charts: composite graph
Rochelle charts
Gantt charts: Gantt Chart
Wind plots, meter charts and symbol charts
Wafer map charts
(State chart, pie chart (2D and 3D), bar chart (horizontal and vertical), line chart, point chart, time change chart, Gantt Chart, stock market chart, hybrid chart, thermometer chart, scale charts and other commonly used commercial charts)
Images can be exported to PNG and JPEG formats, and can be associated with PDF and Excel files.

Jfreechart core class library introduction:
The source code of jfreechart is mainly composed of two major packages: org. jfree. Chart and org. jfree. Data. The former is mainly used with graphics
The latter is related to the data displayed in the image. If you are interested in specific research, you can study it yourself. I will tell you how to do it later.
Research source code.
Core categories include:
Org. jfree. Chart. jfreechart: a chart object. The final form of any type of chart is customized on this object. The jfreechart engine provides a factory class for creating chart objects of different types.
Org. jfree. Data. Category. xxxdataset: A DataSet object that provides the data used to display charts. Different types of charts correspond to many types of DataSet object classes.
Org. jfree. Chart. Plot. xxxplot: The chart area object. Basically, this object determines the chart type. When creating this object, axis, Renderer, and DataSet object are required.
Org. jfree. Chart. axis. xxxaxis: used to process two axes of a chart: vertical axis and horizontal axis.
Org. jfree. Chart. Render. xxxrender: displays a chart object.
Org. jfree. Chart. URLs. xxxurlgenerator: used to generate a mouse click link for each item in the Web chart
Xxxxxtooltipgenerator: used to generate image help prompts. Different types of charts correspond to different types of tooltip classes.

4. jfreechart Development (Application/applet)
1. Pie charts. The Code is as follows:
/**
* Description: This application is the first jfreechart
* Datetime: 20058-02-11
*/
Package demo;

Import org. jfree. Chart. jfreechart;
Import org. jfree. Chart. chartfactory;
Import org. jfree. Chart. chartframe;
Import org. jfree. Data. General. defaultpiedataset;

Public class firstjfreechart {
Public firstjfreechart (){
}

Public static void main (string [] ARGs ){
Defaultpiedataset DPD = new defaultpiedataset ();
DPD. setvalue ("Administrator", 25 );
DPD. setvalue ("market personnel", 25 );
DPD. setvalue ("Developer", 45 );
DPD. setvalue ("other personnel", 5 );

// Create jfreechart object
// View the source code for parameters
Jfreechart piechart = chartfactory. createpiechart ("cityinfoport organization architecture", DPD, true, true, false );
Chartframe pieframe = new chartframe ("cityinfoport Organization Structure", piechart );
Pieframe. Pack ();
Pieframe. setvisible (true );
}
}

The above example can be further improved as follows:
/**
* Description: This application is the first jfreechart
* Datetime: 20058-02-11
*/
Package com. cityinforport. Demo;

Import org. jfree. Chart. jfreechart;
Import org. jfree. Chart. chartpanel;
Import org. jfree. Chart. chartfactory;
Import org. jfree. Chart. chartframe;
Import org. jfree. Data. General. defaultpiedataset;
Import org. jfree. Chart. Plot. pieplot;
Import org. jfree. Data. General. piedataset;
Import org. jfree. UI. applicationframe;
Import org. jfree. UI. refineryutilities;
Import java. AWT. Font;
Import javax. Swing .*;

Public class firstjfreechart extends applicationframe {
// Constructor
Public firstjfreechart (string s ){
Super (s );
Setcontentpane (createdemopanel ());
}

Public static void main (string [] ARGs ){
Firstjfreechart FJC = new firstjfreechart ("cityinfoport organization structure ");
FJC. Pack ();
Refineryutilities. centerframeonscreen (FJC );
FJC. setvisible (true );
}

// Generate a pie chart DataSet object
Public static piedataset createdataset (){
Defaultpiedataset defapipiedataset = new defaultpiedataset ();
Defaultpiedataset. setvalue ("Administrator", 10.02d );
Defaultpiedataset. setvalue ("market personnel", 42423d );
Defaultpiedataset. setvalue ("Developer", 601_2d );
Defaultpiedataset. setvalue ("OEM", 10.02d );
Defaultpiedataset. setvalue ("other personnel", 5.11d );

Return defaultpiedataset;
}

// Generate the main chart object jfreechart
Public static jfreechart createchart (piedataset ){
// Define the chart object
Jfreechart = chartfactory. createpiechart ("cityinfoport organization architecture", piedataset, true, true, false );
// Obtain the Chart Display object
Pieplot = (pieplot) jfreechart. getplot ();
// Set the label font of the chart.
Pieplot. setlabelfont (new font ("sansserif", Font. Bold, 12 ));
Pieplot. setnodatamessage ("No data available ");
Pieplot. setcircular (true );
Pieplot. setlabelgap (0.01d); // spacing

Return jfreechart;
}

// Generate a panel for displaying charts
Public static jpanel createdemopanel (){
Jfreechart = createchart (createdataset ());
Return new chartpanel (jfreechart );
}

}

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.