Bar Chart

HelloBarChart

原始檔案
HelloBarChart.java

HelloBarChart解說

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

  1. 建立一個擁有資料的DataSet
  2. 用DataSet創造JFreeChart
  3. JFreeChart作一些自訂的設計
  4. 顯示JFreeChart

建立DataSet

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

使用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 value
rowKey - the row key
columnKey - the column key


   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;

   }

使用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", // chart title

         "Category", // domain axis label

         "Value", // range axis label

         dataset, // data

         PlotOrientation.VERTICAL, // orientation

         true, // include legend

         true, // tooltips?

         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);

   }