论坛首页 入门讨论版 Java

JFreeChart图表详解(一)

浏览 1348 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2007-09-14
java 代码
 
  1. package HelloJChart;  
  2. import java.awt.Dimension;  
  3. import javax.swing.JFrame;  
  4. import org.jfree.chart.ChartFactory;  
  5. import org.jfree.chart.ChartPanel;  
  6. import org.jfree.chart.JFreeChart;  
  7. import org.jfree.chart.plot.PlotOrientation;  
  8. import org.jfree.data.category.CategoryDataset;  
  9. import org.jfree.data.category.DefaultCategoryDataset;  
  10. public class HelloBarChart extends JFrame{  
  11.    public HelloBarChart(){  
  12.       CategoryDataset dataset = createDataset();  
  13.       JFreeChart chart = createChart(dataset);  
  14.       chart = customizeChart(chart);  
  15.       ChartPanel chartPanel = new ChartPanel(chart);  
  16.       chartPanel.setPreferredSize(new Dimension(500270));  
  17.       getContentPane().add(chartPanel);  
  18.       pack();  
  19.       setVisible(true);  
  20.       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  21.    }  
  22.    public static void main(String[] args){  
  23.       new HelloBarChart();  
  24.    }  
  25.    private CategoryDataset createDataset(){  
  26.       // row keys...  
  27.       String series1 = "First";  
  28.       String series2 = "Second";  
  29.       String series3 = "Third";  
  30.       // column keys...  
  31.       String category1 = "Category 1";  
  32.       String category2 = "Category 2";  
  33.       String category3 = "Category 3";  
  34.       String category4 = "Category 4";  
  35.       String category5 = "Category 5";  
  36.       // create the dataset...  
  37.       DefaultCategoryDataset dataset = new DefaultCategoryDataset();  
  38.       dataset.addValue(1.5, series1, category1);  
  39.       dataset.addValue(4.2, series1, category2);  
  40.       dataset.addValue(3.0, series1, category3);  
  41.       dataset.addValue(5.0, series1, category4);  
  42.       dataset.addValue(5.0, series1, category5);  
  43.       dataset.addValue(5.5, series2, category1);  
  44.       dataset.addValue(7.8, series2, category2);  
  45.       dataset.addValue(6.0, series2, category3);  
  46.       dataset.addValue(8.0, series2, category4);  
  47.       dataset.addValue(4.0, series2, category5);  
  48.       dataset.addValue(4.0, series3, category1);  
  49.       dataset.addValue(3.0, series3, category2);  
  50.       dataset.addValue(2.0, series3, category3);  
  51.       dataset.addValue(3.0, series3, category4);  
  52.       dataset.addValue(6.0, series3, category5);  
  53.       return dataset;  
  54.    }  
  55.    private JFreeChart createChart(final CategoryDataset dataset){  
  56.       JFreeChart chart = ChartFactory.createBarChart(  
  57.          "Hello Bar Chart"// chart title  
  58.          "Category"// domain axis label  
  59.          "Value"// range axis label  
  60.          dataset, // data  
  61.          PlotOrientation.VERTICAL, // orientation  
  62.          true// include legend  
  63.          true// tooltips  
  64.          false // URLs  
  65.          );  
  66.       return chart;  
  67.    }  
  68.    private JFreeChart customizeChart(final JFreeChart chart){  
  69.       return chart;  
  70.    }  
  71. }  

要建立一个JFreeChart的图形主要有三个步骤

  1. 建立一个拥有数据的DataSet
  2. DataSet创造JFreeChart
  3. JFreeChart作一些自订的设计
  4. 显示JFreeChart

第一步:建立DataSet

BarChart使用的DataSet接口org.jfree.data.CategoryDatasetDataSet
有两种方式来建立CategoryDataSet

  • 使用CategoryDataSet的子类org.jfree.data.DefaultCategoryDataset,再用addValue()把数据加入DataSet
  • 建立包含数值的二维数组,再使用org.jfree.data.DatasetUtilitiescreateCategoryDataset()

使用DefaultCategoryDataSet

DefaultCategoryDataSet class:

java 代码
 
  1. public void addValue(double value, java.lang.Comparable rowKey, java.lang.Comparable columnKey)  
  2. 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

参照前面的createDataset方法!

使用org.jfree.data.DatasetUtilities

org.jfree.data.DatasetUtilities class:

java 代码
 
  1. public static CategoryDataset createCategoryDataset(String rowKeyPrefix, String columnKeyPrefix, java.lang.Number[][] data)  
  2. public static CategoryDataset createCategoryDataset(String[] rowKeys, String[] columnKeys, double[][] data)  
  3. 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.

java 代码
 
  1. private CategoryDataset createDataset(){  
  2.    double[][] data = new double[][]{{1.043.035.058.054.077.071.089.0}  
  3.       , {54.075.063.083.043.046.027.013.0}  
  4.       , {41.033.022.034.062.032.042.034.0}  
  5.    };  
  6.    return DatasetUtilities.createCategoryDataset("Series ""Factor ", data);  
  7. }  

第二步:创造JFreeChart

要用DataSet创造出一个JFreeChart类别,我们并不直接实体化出一个JFreeChart实体,而是使用ChartFactory类别里面的方法。

ChartFactory class:

java 代码
 
  1. public static JFreeChart createBarChart(java.lang.String title,  
  2.                                         java.lang.String categoryAxisLabel,  
  3.                                         java.lang.String valueAxisLabel,  
  4.                                         CategoryDataset data,  
  5.                                         PlotOrientation orientation,  
  6.                                         boolean legend,  
  7.                                         boolean tooltips,  
  8.                                         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?

java 代码
 
  1.   private JFreeChart createChart(final CategoryDataset dataset){  
  2.      JFreeChart chart = ChartFactory.createBarChart(  
  3.         "Hello Bar Chart"// 题目  
  4.         "Category",   //行名称         
  5. "Value"// 列名称           
  6. dataset, // 数据           
  7. PlotOrientation.VERTICAL, // 横向,纵向           
  8. true// 图例           
  9. true// 柱状说明  
  10.         false // URLs  
  11.         );  
  12.      return chart;  
  13.   }  

第三步:修饰JFreeChart

这个范例中并没有对JFreeChart作修饰。

java 代码
 
  1. private JFreeChart customizeChart(final JFreeChart chart){  
  2.    return chart;  
  3. }  

第四步:显示JFreeChart

ChartPanel是一个继承JPanel的类别,负责把JFreeChart在应用程序中显示出来。
我们只要把JFreeChart当成参数传给ChartPanel的建构子。造出ChartPanel的实体后,设定大小,再当成一般的Panel放入ContentPane中就可以了。

ChartPanel class:

java 代码
 
  1. public ChartPanel(JFreeChart chart)  
  2. public ChartPanel(JFreeChart chart, boolean useBuffer)  
  3. public ChartPanel(JFreeChart chart,  
  4.                   boolean properties,  
  5.                   boolean save,  
  6.                   boolean print,  
  7.                   boolean zoom,  
  8.                   boolean tooltips)  
  9. public ChartPanel(JFreeChart chart,  
  10.                   int width,  
  11.                   int height,  
  12.                   int minimumDrawWidth,  
  13.                   int minimumDrawHeight,  
  14.                   int maximumDrawWidth,  
  15.                   int maximumDrawHeight,  
  16.                   boolean useBuffer,  
  17.                   boolean properties,  
  18.                   boolean save,  
  19.                   boolean print,  
  20.                   boolean zoom,  
  21.                   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.

java 代码
 
  1. public HelloBarChart(){  
  2.    CategoryDataset dataset = createDataset();  
  3.    JFreeChart chart = createChart(dataset);  
  4.    chart = customizeChart(chart);  
  5.    ChartPanel chartPanel = new ChartPanel(chart);  
  6.    chartPanel.setPreferredSize(new Dimension(500270));  
  7.    getContentPane().add(chartPanel);  
  8.   
  9.    pack();  
  10.    setVisible(true);  
  11.    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  12. }  
   
最后更新时间:2007-12-20
收下啦
   
0 请登录后投票
论坛首页 入门讨论版 Java

跳转论坛:
JavaEye推荐