JSP空间欢迎您!专业JSP虚拟主机提供商!!!
返回首页
当前位置: 主页 > JSP教程 >

学习JFreeChart(一)

时间:2011-01-30 12:05来源:佚名 编辑:www.jspzg.com 点击: 次   字体大小:[ ]

当前阅读得是【JSP教程】之学习JFreeChart(一)!
【关键字信息】:学习,习J,JF,Fr,re,ee,eC,Ch,h

   最近由于项目需要,开始学习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的一些基本技术!下面再学习一个柱状图的例子
标签:
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
【郑重声明】JSP中国刊载此文不代表同意其说法或描述,仅为提供更多信息,也不构成任何投资或其他建议。本网站有部分文章是由网友自由上传。对于此类文章本站仅提供交流平台,不为其版权负责。如果您发现本网站上有侵犯您的知识产权的文章,请联系我们,我们将及时删除,谢谢!
推荐内容