• Nie Znaleziono Wyników

JFreeChart

W dokumencie Version 0.9.18 (Stron 144-147)

Test

21.14 JFreeChart

21.14.1 Overview

The JFreeChart class coordinates the entire process of drawing charts. One method:

public void draw(Graphics2D g2, Rectangle2D area);

...instructs the JFreeChartobject to draw a chart onto a specific area on some graphics device.

Java supports several graphics devices—including the screen, the printer, and buffered images—via different implementations of the abstract class java.awt.-Graphics2D. Thanks to this abstraction, JFreeChart can generate charts on any of these target devices, as well as others implemented by third parties (for example, the SVG Generator implemented by the Batik Project).

In broad terms, theJFreeChartclass sets up a context for drawing aPlot. The plot obtains data from a Dataset, and may delegate the drawing of individual data items to a CategoryItemRendereror anXYItemRenderer, depending on the plot type (not all plot types use renderers).

TheJFreeChartclass can work with many differentPlotsubclasses. Depending on the type of plot, a specific dataset will be required. The following table summarises the combinations that are currently available:

Dataset: Compatible Plot Types:

MeterDataset CompassPlot,MeterPlotandThermometerPlot.

PieDataset PiePlot.

CategoryDataset CategoryPlotsubclasses with various renderers.

XYDataset XYPlotwith various renderers.

IntervalXYDataset XYPlotwith aXYBarRenderer.

HighLowDataset XYPlotwith aHighLowRenderer.

HighLowDataset XYPlotwith aCandlestickRenderer.

21.14.2 Constructors

All constructors require you to supply a Plot instance (the Plot maintains a reference to the dataset used for the chart).

The simplest constructor is:

public JFreeChart(Plot plot);

Creates a new JFreeChart instance. The chart will have no title, and no legend.

For greater control, a more complete constructor is available:

public JFreeChart(Plot plot, String title, Font titleFont, boolean createLegend);

Creates a new JFreeChart instance. This constructor allows you to spec-ify a single title (you can add additional titles, later, if necessary).

TheChartFactoryclass provides some utility methods that can make the process of constructing charts simpler.

21.14.3 Attributes

The attributes maintained by the JFreeChart class are listed in Table21.1.

Attribute: Description:

title The chart title (an instance ofTextTitle).

sub-titles A list of subtitles.

legend The chart legend.

plot The plot.

antialias A flag that indicates whether or not the chart should be drawn with anti-aliasing.

background-paint The background paint for the chart.

background-image An optional background image for the chart.

background-image-alignment The alignment of the background image (if there is one).

background-image-alpha The alpha transparency for the background image.

Table 21.1: Attributes for the JFreeChart class

21.14.4 Methods

The most important method for a chart is thedraw() method:

public void draw(Graphics2D g2, Rectangle2D chartArea);

Draws the chart on the Graphics2D device, within the specified area.

The chart does not retain any information about the location or dimensions of the items it draws. Callers that require such information should use the alternative method:

public void draw(Graphics2D g2, Rectangle2D chartArea, ChartRenderingInfo info);

Draws the chart on the Graphics2D device, within the specified area. If info is not null, it will be populated with information about the items drawn within the chart (to be returned to the caller).

To set the title for a chart:

public void setTitle(String title);

Sets the title for a chart and sends aChartChangeEventto all registered listeners.

An alternative method for setting the chart title is:

public void setTitle(TextTitle title);

Sets the title for a chart and sends aChartChangeEventto all registered listeners.

Although a chart can have only one title, it can have any number of subtitles:

public void addSubtitle(Title title);

Adds a title to the chart.

The legend shows the names of the series (or sometimes categories) in a chart, next to a small color indicator. To set the legend for a chart:

public void setLegend(Legend legend);

Sets the legend for a chart.

You can control whether or not the chart is drawn with anti-aliasing (switching anti-aliasing on can improve the on-screen appearance of charts):

public void setAntiAlias(boolean flag);

Sets a flag controlling whether or not anti-aliasing is used when drawing the chart.

To set the background paint for the chart:

public void setBackgroundPaint(Paint paint);

Sets the background paint for the chart and sends aChartChangeEvent to all registered listeners. If this is set to null, the chart background will be transparent.

You can set an optional background image for the chart:1

public void setBackgroundImage(Image image);

Sets the background image for the chart (null permitted) and sends a ChartChangeEventto all registered listeners.

You need to ensure that the image supplied to the above method is fully loaded, see this link for more details:

http://java.sun.com/docs/books/tutorial/uiswing/painting/loadingImages.html

To receive notification of any change to a chart, a listener object should register via this method:

public void addChangeListener(ChartChangeListener listener);

Register to receive chart change events.

To stop receiving change notifications, a listener object should deregister via this method:

public void removeChangeListener(ChartChangeListener listener);

Deregister to stop receiving chart change events.

1As an alternative to this method, note that you can set a background image for the chart’s Plot. This will be positioned within the plot area only rather than the entire chart area.

21.14.5 Creating Images

TheJFreeChartclass includes utility methods for creating aBufferedImage con-taining the chart:

public BufferedImage createBufferedImage(int width, int height);

Creates a buffered image containing the chart. The size of the image is specified by the width and height arguments.

public BufferedImage createBufferedImage(int width, int height, ChartRenderingInfo info);

Creates a buffered image containing the chart. The size of the image is specified by the width and height arguments. The info argument is used to collect information about the chart as it is being drawn (required if you want to create an HTML image map for the image).

One other variation draws the chart at one size then scales it (up or down) to fit a different image size:

public BufferedImage createBufferedImage(int imageWidth, int imageHeight, double drawWidth, double drawHeight, ChartRenderingInfo info)

Creates an image containing a chart that has been drawn at one size then scaled (up or down) to fit the image size.

21.14.6 Notes

Some points to note:

• the ChartFactory class provides a large number of methods for creating

“ready-made” charts.

• the Java2D API is used throughout JFreeChart, so JFreeChart does not work with JDK1.1 (a common question from applet developers, although hopefully less of an issue as browser support for Java 2 improves).

W dokumencie Version 0.9.18 (Stron 144-147)

Powiązane dokumenty