Learning Jfreechart (i)

Source: Internet
Author: User
Tags comparable final pack return
Chart
Recently because of the project needs, began to learn Jfreechart and itext, on the Internet to find a relevant information is not a lot, especially jfreechart in the official document did not like Itext as detailed description and examples, and the official demo only a jar file, which is also more chaotic , do not know where to start, on the Internet to find a few examples, began to learn it step-by-step!

The first example is written by Taiwanese, and many nouns are used in Taiwanese parlance, and friends who have read the books of Mr. Houtie should have a deep understanding of them! Learning any open source project the first task is to match the environment,

Jfreechart First: http://www.jfree.org/jfreechart/

Jfreechart api:http://www.jfree.org/jfreechart/javadoc/

Current version: jfreechart-1.0.0 specific installation and classpath configuration is not much to say



The above is the example of the histogram to be generated! The code is as follows:

Package Hellojchart;import java.awt.Dimension;

Import Javax.swing.JFrame;

Import Org.jfree.chart.chartfactory;import Org.jfree.chart.chartpanel;import Org.jfree.chart.jfreechart;import Org.jfree.chart.plot.plotorientation;import Org.jfree.data.category.categorydataset;import Org.jfree.data.category.DefaultCategoryDataset;

public class Hellobarchart extends jframe{public Hellobarchart () {Categorydataset DataSet = CreateDataSet ();      Jfreechart chart = Createchart (DataSet);      Chart = Customizechart (chart);      Chartpanel Chartpanel = new Chartpanel (chart);      Chartpanel.setpreferredsize (New Dimension (500, 270)); Getcontentpane (). Add (Chartpanel);

Pack ();      SetVisible (TRUE);   Setdefaultcloseoperation (Jframe.exit_on_close); }

public static void Main (string[] args) {new Hellobarchart (); }

Private Categorydataset CreateDataSet () {//row keys ...      String series1 = "a";      String Series2 = "Second"; String Series3 = "Third";

Column keys ...      String category1 = "Category 1";      String Category2 = "Category 2";      String category3 = "Category 3";      String Category4 = "Category 4"; String category5 = "Category 5";

Create the DataSet ... Defaultcategorydataset DataSet = new Defaultcategorydataset ();

Dataset.addvalue (1.5, series1, category1);      Dataset.addvalue (4.2, series1, Category2);      Dataset.addvalue (3.0, series1, category3);      Dataset.addvalue (5.0, series1, Category4); Dataset.addvalue (5.0, series1, category5);

Dataset.addvalue (5.5, Series2, category1);      Dataset.addvalue (7.8, Series2, Category2);      Dataset.addvalue (6.0, Series2, Category3);      Dataset.addvalue (8.0, Series2, category4); Dataset.addvalue (4.0, Series2, category5);

Dataset.addvalue (4.0, Series3, category1);      Dataset.addvalue (3.0, Series3, Category2);      Dataset.addvalue (2.0, Series3, Category3);      Dataset.addvalue (3.0, Series3, Category4); Dataset.addvalue (6.0, Series3, Category5);

return dataset; }

Private Jfreechart Createchart (final categorydataset DataSet) {Jfreechart chart = Chartfactory.createbarchart (         "Hello Bar Chart",//Chart title "Category",//Domain axis label "Value"//Range axis label DataSet,//Data plotorientation.vertical,//orientation true,//include legend True,//ToolTip         S?         False//URLs?      );   return chart; }

Private Jfreechart Customizechart (final Jfreechart chart) {return chart; }}

There are three main steps to creating a Jfreechart image
Create a DataSet with a dataset to create jfreechart for Jfreechart make some custom designs Jfreechart
The first step: setting Up a DataSet

Barchart uses the DataSet interface to org.jfree.data.CategoryDataset the dataset. There are two ways to build a categorydataset
Using the Categorydataset subclass Org.jfree.data.DefaultCategoryDataset, and then using AddValue () to add the data to the dataset to create a two array containing the numbers, Then use the Org.jfree.data.DatasetUtilities createcategorydataset ()
Using Defaultcategorydataset

Defaultcategorydataset class:

public void AddValue (double value, java.lang.Comparable Rowkey, java.lang.Comparable columnkey) public void AddValue ( Java.lang.Number value, java.lang.Comparable rowkey, java.lang.Comparable Columnkey)

Value-the valuerowkey-the row keycolumnkey-the column key

Refer to the previous CreateDataSet Method!

Using Org.jfree.data.DatasetUtilities

Org.jfree.data.DatasetUtilities class:

public static Categorydataset Createcategorydataset (String rowkeyprefix, String columnkeyprefix, java.lang.number[][] Data) public static Categorydataset Createcategorydataset (string[] Rowkeys, string[] Columnkeys, double[][] Data Static Categorydataset Createcategorydataset (String rowkey, Keyedvalues rowdata)

Rowkeyprefix-the row key prefix.columnkeyprefix-the column key prefix.rowkeys-the row keys.columnkeys-the column K Eys.data-the data.



Private Categorydataset CreateDataSet () {double[][] data = new double[][]{{1.0, 43.0, 35.0, 58.0, 54.0, 77.0, 71.0, 89.0} , {54.0, 75.0, 63.0, 83.0, 43.0, 46.0, 27.0, 13.0}, {41.0, 33.0, 22.0, 34.0, 62.0, 32.0, 42.0, 34.0}}; Return Datasetutilities.createcategorydataset ("Series", "Factor", data);}

Step Two: Create Jfreechart

To create a jfreechart by using a dataset, we do not directly manifest a jfreechart reality, but rather use chartfactory methods.

Chartfactory class:

public static Jfreechart Createbarchart (java.lang.String title, java.lang.String Categoryaxislabel, java.lang.String Valueaxislabel, categorydataset data, plotorientation orientation, Boolean legend, Boolean ToolTips, Boolean URLs)

Title-the Chart title.categoryaxislabel-the label for the category Axis.valueaxislabel-the label for the value axis Data-the DataSet for the chart.orientation-the plot orientation (plotorientation.horizontal or Plotorientation.vertica L). legend-a flag specifying whether or not a legend was Required.tooltips-configure chart to generate tool- Configure chart to generate URLs?



Private Jfreechart Createchart (final categorydataset DataSet) {Jfreechart chart = Chartfactory.createbarchart ("Hello Bar Chart ",//Topic

"Category",//Line name

' Value ',//column name

DataSet,//Data

Plotorientation.vertical,//Landscape, longitudinal

True,//Legend

True,//Columnar description false//URLs? ); return chart; }

Step Three: Trim Jfreechart

In this case, there is no decoration for jfreechart.

Private Jfreechart Customizechart (final Jfreechart chart) {return chart;}

Fourth Step: Display Jfreechart

Chartpanel is a kind of inheritance jpanel, which is responsible for showing the Jfreechart in the application. We simply pass the Jfreechart as a reference to Chartpanel's construction. After making the Chartpanel, set the size, and then add the ContentPane to the General panel.

Chartpanel class:

Public Chartpanel (Jfreechart chart) public Chartpanel (Jfreechart chart, Boolean Usebuffer) public Chartpanel (Jfreechart Chart, Boolean properties, Boolean Save, Boolean print, Boolean zoom, Boolean tooltips) public Chartpanel (Jfreechart chart, int width, int height, int minimumdrawwidth, int minimumdrawheight, int maximumdrawwidth, int maximumdrawheight, Boolean Usebuffer, Boolean properties, Boolean Save, Boolean print, Boolean zoom, Boolean tooltips)

Chart-the chart.usebuffer-a flag Controlling whether or not a off-screen buffer is USED.PROPERTIES-A flag Indicatin G whether or not the Chart property editor should is available via the popup Menu.save-a flag indicating whether or not Save options should be available via the popup Menu.print-a flag indicating whether or not the "Print option should be AV Ailable via the popup menu.zoom-a flag indicating whether or not zoom options should is added to the popup menu.tooltips -a flag indicating whether or not ToolTips should is enabled for the Chart.width-the preferred width of the Panel.heig Ht-the preferred height of the panel.minimumdrawwidth-the minimum drawing width.minimumdrawheight-the minimum Drawin G height.maximumdrawwidth-the Maximum drawing width.maximumdrawheight-the maximum drawing height.



Public Hellobarchart () {Categorydataset DataSet = CreateDataSet (); Jfreechart chart = Createchart (DataSet); Chart = Customizechart (chart); Chartpanel Chartpanel = new Chartpanel (chart); Chartpanel.setpreferredsize (New Dimension (500, 270)); Getcontentpane (). Add (Chartpanel); Pack (); SetVisible (TRUE); Setdefaultcloseoperation (Jframe.exit_on_close); }
So far the first example has been completed, the basic understanding of Jfreechart some of the basic technology! Here's another example of a columnar chart.

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.