分类: 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:
|
#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); } }
|
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:
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的宽度会变大。 |