程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 學習JFreeChart(一)

學習JFreeChart(一)

編輯:關於JAVA

最近由於項目需要,開始學習JFreeChart和iText,在網上找了一下相關的資料不是很多,特別是JFreeChart在官方文檔上沒有像iText那樣詳盡的說明及例子,而且官方的demo只有一個jar文件,裡面也比較亂,不知道從什麼地方下手,在網上找了幾個例子,開始循序漸進的學吧!

第一個例子是個台灣人寫的,很多的名詞都是用的台灣說法,讀過侯捷先生翻譯的書的朋友應該有很深的體會!學習任何一個開源的項目第一個任務就是配環境,

JFreeChart 首頁:http://www.jfree.org/jfreechart/

JFreeChart API:http://www.jfree.org/jfreechart/Javadoc/

目前的版本:jfreechart-1.0.0具體的安裝和classpath的配置就不多說了

上面就是該例子要生成的柱狀圖!代碼如下:

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 = "First"; 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, // tooltips? false // URLs? ); return chart; }

private JFreeChart customizeChart(final JFreeChart chart){ return chart; }}

要建立一個JFreeChart的圖形主要有三個步驟

建立一個擁有資料的DataSet 用DataSet創造JFreeChart 對JFreeChart作一些自訂的設計 顯示JFreeChart

第一步:建立DataSet

BarChart使用的DataSet接口org.jfree.data.CategoryDataset的DataSet。有兩種方式來建立CategoryDataSet

使用CategoryDataSet的子類org.jfree.data.DefaultCategoryDataset,再用addValue()把資料加入DataSet中 建立包含數值的二維陣列,再使用org.jfree.data.DatasetUtilitIEs的createCategoryDataset()

使用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

參照前面的createDataset方法!

使用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)public static CategoryDataset createCategoryDataset(String rowKey, KeyedValues rowData)

rowKeyPrefix - the row key prefix.columnKeyPrefix - the column key prefix.rowKeys - the row keys.columnKeys - the column keys.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); }

第二步:創造JFreeChart

要用DataSet創造出一個JFreeChart類別,我們並不直接實體化出一個JFreeChart實體,而是使用ChartFactory類別裡面的方法。

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.VERTICAL).legend - a flag specifying whether or not a legend is required.tooltips - configure chart to generate tool tips?urls - configure chart to generate URLs?

private JFreeChart createChart(final CategoryDataset dataset){ JFreeChart chart = ChartFactory.createBarChart( "Hello Bar Chart", // 題目

"Category", //行名稱

"Value", // 列名稱

dataset, // 數據

PlotOrIEntation.VERTICAL, // 橫向,縱向

true, // 圖例

true, // 柱狀說明 false // URLs? ); return chart; }

第三步:修飾JFreeChart

這個範例中並沒有對JFreeChart作修飾。

private JFreeChart customizeChart(final JFreeChart chart){ return chart; }

第四步:顯示JFreeChart

ChartPanel是一個繼承JPanel的類別,負責把JFreeChart在應用程式中顯示出來。我們只要把JFreeChart當成參數傳給ChartPanel的建構子。造出ChartPanel的實體後,設定大小,再當成一般的Panel放入ContentPane中就可以了。

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 an off-screen buffer is used.propertIEs - a flag indicating whether or not the chart property editor should be 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 available via the popup menu.zoom - a flag indicating whether or not zoom options should be added to the popup menu.tooltips - a flag indicating whether or not tooltips should be enabled for the chart.width - the preferred width of the panel.height - the preferred height of the panel.minimumDrawWidth - the minimum drawing width.minimumDrawHeight - the minimum drawing 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); }

至此第一個例子已經學習完畢,基本了解了JFreeChart的一些基本技術!下面再學習一個柱狀圖的例子

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved