JFreeChart图表详解(一)
- 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 value
rowKey - the row key
columnKey - 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);
- }
- 浏览: 56332 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
我的相册
共 13 张
最近加入圈子
最新评论
-
Gwt-Ext学习笔记之基础篇
问题已经解决
-- by chinahcl -
Gwt-Ext学习笔记之基础篇
[ERROR] Unable to find 'com/google/gwt/u ...
-- by chinahcl -
Gwt-Ext学习笔记之基础篇
谢谢楼主,楼主厉害
-- by chinahcl -
Seam实例教程(环境配置)
不好意思,为什么照你这样做的,但是会报错, 10:30:39,562 INFO ...
-- by yuyanshan -
Seam实例教程(环境配置)
总算自己弄明白了
-- by afadgaeg






评论排行榜