Chinaunix首页 | 论坛 | 博客
  • 博客访问: 206066
  • 博文数量: 73
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 750
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 18:32
文章分类

全部博文(73)

文章存档

2009年(1)

2008年(72)

我的朋友

分类: Java

2008-03-29 10:23:40

 
概要
  This chapter provides an introduction to creating bar charts with JFreeChart. We begin with a very simple bar chart, then go on to describe some of the many options that JFreeChart provides for customising the charts. After covering the standard bar chart and its options, we’ll move on to some more complex bar chart variants:
• stacked bar charts;
• bar charts for time series data;
• histograms.

  Bar charts are used to provide a visual representation of tabular data. For example, consider the
following table, which contains a simple set of data in two rows and three columns:
        Column 1: Column 2: Column 3:
Row 1:     1.0      5.0        3.0
Row 2:     2.0      3.0        2.0
  In JFreeChart, this table is referred to as a dataset, each column heading is a category, and each row in the table is a series. Each row heading is a series name (or series key).

#The complete program 1
import java.awt.Dimension;
/**
* A simple demonstration application showing how to create a bar chart.
*/
public class BarExample1 extends ApplicationFrame {
/**
* Creates a new demo instance.
*
* @param title the frame title.
*/
  public BarExample1(String title) {
    super(title);
    DefaultCategoryDataset dataset =
      new DefaultCategoryDataset();

    dataset.addValue(1.0, "Row 1", "Column 1");
    dataset.addValue(5.0, "Row 1", "Column 2");
    dataset.addValue(3.0, "Row 1", "Column 3");
    dataset.addValue(2.0, "Row 2", "Column 1");
    dataset.addValue(3.0, "Row 2", "Column 2");
    dataset.addValue(2.0, "Row 2", "Column 3");
    JFreeChart chart = ChartFactory.createBarChart(
     "Bar Chart Demo", // chart title
     "Category", // domain axis label
     "Value", // range axis label
     dataset, // data
     PlotOrientation.VERTICAL, // orientation
     true, // include legend
     true, // tooltips?
     false // URLs?
   );
   ChartPanel chartPanel = new ChartPanel(chart, false);
   chartPanel.setPreferredSize(new Dimension(500, 270));
   setContentPane(chartPanel);
   }
  /**
  * Starting point for the demonstration application.
  *
  * @param args ignored.
  */
   public static void main(String[] args) {
     BarExample1 demo = new BarExample1("Bar Demo 1");
     demo.pack();
     RefineryUtilities.centerFrameOnScreen(demo);
     demo.setVisible(true);
  }
}

#The complete program 2
package com.biaoflying;

import java.awt.Dimension;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.ChartFrame;

public class BarExample1{
   
    public static void main(String[] args) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(1.0, "Row 1", "Column 1");
        dataset.addValue(5.0, "Row 1", "Column 2");
        dataset.addValue(3.0, "Row 1", "Column 3");
        dataset.addValue(2.0, "Row 2", "Column 1");
        dataset.addValue(3.0, "Row 2", "Column 2");
        dataset.addValue(2.0, "Row 2", "Column 3");
        JFreeChart chart = ChartFactory.createBarChart(
                "Bar Chart Demo", // chart title
                "Category", // domain axis label
                "Value", // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                true, // include legend
                true, // tooltips?
                false // URLs?
                );
        ChartFrame frame=new ChartFrame("Bar",chart);
        frame.pack();
        frame.setVisible(true);
        }
    }

Most of the argument to thecreateBarChart() method are obvious, but a few of them demand
further explanation:
    •the plot orientation can be either horizontal or vertical (for bar charts, this corresponds to the way the bars are drawn, horizontally or vertically);
    •the tooltips flag controls whether or not a tool tip generator is added to the chart—in this example, we’ve set this flag to true so that we’ll see tool tips when we display the chart in a Swing application;
    • the URLs flag is only relevant when creating drill-down reports using HTML image maps, so we set it to cffalse here.

The ChartFactory Class
  In our first example, the ChartFactory class is used to piece together a JFreeChart instance that renders a bar chart. Here we take a closer look at how this is done, so we can see a little more of the underlying structure of our bar chart. Understanding this structure is key to being able customise the appearance of the chart.
  Here are the important parts of the code from the createBarChart() method in the ChartFactory class:
1 CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
2 ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
3 BarRenderer renderer = new BarRenderer();[snip...]
4 CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis,renderer);
5 plot.setOrientation(orientation);
6 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT TITLE FONT,plot, legend);
  #以上code的说明
  Here’s what this code is doing:

  • Our bar chart has two axes, one that displays categories from the dataset (a CategoryAxis), and another that provides the numerical scale against which the data values are plotted (a NumberAxis). You can see these axes being constructed in lines 1 and 2 above, using the axis
labels that we passed to the createBarChart() method.
  • At line 3, a BarRenderer is created—this is the class that is used to draw the bar for each data item. The renderer handles most of the drawing work, and you’ll see later that you can substitute another type of renderer to change the overall appearance of the chart.
  • The dataset, axes and renderer are all managed by a CategoryPlot, which coordinates most of the interaction between these components. When you customise charts, you’ll often need to get a reference to the chart’s plot, which in turn can give you access to the axes, renderer
and dataset. At line 4, the plot is created, and the other components are assigned to it.
  • Finally, the plot is wrapped in a JFreeChart instance, with the specified title. The JFreeChart class provides the top-level access to a chart, but most of the “guts” of a chart is defined at the plot level (or in the objects managed by the plot, such as the axes, dataset(s) and renderer(s)).

  6.4 简单的定制Chart
  Some simple modifications to the appearance of a bar chart can be made by calling methods in the JFreeChart and CategoryPlot classes. For example, to change the background colours for the chart and plot:
  #可以调用JFreeChart或者Plot的相关方法进行chart appearance定制:
  chart.setBackgroundPaint(Color.white);
  CategoryPlot plot = (CategoryPlot) chart.getPlot();
  plot.setBackgroundPaint(Color.lightGray);
  #设置纵坐标标尺出分割线的颜色:
  plot.setRangeGridlinePaint(Color.white);


  6.5 定制Renderer
  To change the colours used for each series in the chart:
  #改变series所在柱的颜色
  BarRenderer renderer = (BarRenderer)plot.getRenderer();
  renderer.setSeriesPaint(0, Color.gray);
  renderer.setSeriesPaint(1, Color.orange);
  renderer.setDrawBarOutline(false);
 
  Note that the setSeriesPaint() method is defined in
the AbstractRenderer base class—you can use this for all types of renderer.

  #设置Bars之间的间距
  renderer.setItemMargin(0.0);
  这时每一个bar的宽度会变大。













阅读(2316) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~