學習JFreeChart(一)

來源:互聯網
上載者:User
chart
   最近由於項目需要,開始學習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的一些基本技術!下面再學習一個柱狀圖的例子

相關關鍵詞:
相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。