com.jrefinery.chart
Class JFreeChart

java.lang.Object
  extended by com.jrefinery.chart.JFreeChart
All Implemented Interfaces:
com.jrefinery.chart.event.LegendChangeListener, com.jrefinery.chart.event.PlotChangeListener, com.jrefinery.chart.event.TitleChangeListener, JFreeChartConstants, JFreeChartInfo

public class JFreeChart
extends java.lang.Object
implements JFreeChartInfo, JFreeChartConstants, com.jrefinery.chart.event.TitleChangeListener, com.jrefinery.chart.event.LegendChangeListener, com.jrefinery.chart.event.PlotChangeListener

A chart class implemented using the Java 2D APIs. The current version supports bar charts, line charts, pie charts and xy plots (including time series data).

JFreeChart coordinates several objects to achieve its aim of being able to draw a chart on a Java 2D graphics device: a Title, a Legend, a Plot and a Dataset (the Plot in turn manages a horizontal axis and a vertical axis).

You should use JFreeChartPanel to display a chart in a GUI.

The ChartFactory class contains static methods for creating 'ready-made' charts.

See Also:
JFreeChartPanel, ChartFactory, AbstractTitle, Legend, Plot

Field Summary
protected  boolean antialias
          Flag that determines whether or not the chart is drawn with anti-aliasing.
protected  java.awt.Image backgroundImage
          An optional background image for the chart.
protected  float backgroundImageAlpha
          The alpha transparency for the background image.
protected  java.awt.Paint backgroundPaint
          Paint used to draw the background of the chart.
protected  Legend legend
          The chart legend.
protected  java.util.List listeners
          Storage for registered change listeners.
protected  Plot plot
          Draws the visual representation of the data.
protected  java.util.List titles
          The chart title(s).
 
Fields inherited from interface com.jrefinery.chart.JFreeChartInfo
CONTRIBUTORS, COPYRIGHT, INFO, LIBRARIES, LICENCE, LICENCE_NAME, NAME, VERSION
 
Fields inherited from interface com.jrefinery.chart.JFreeChartConstants
DEFAULT_BACKGROUND_IMAGE, DEFAULT_BACKGROUND_IMAGE_ALPHA, DEFAULT_BACKGROUND_PAINT, DEFAULT_TITLE_FONT
 
Constructor Summary
JFreeChart(Plot plot)
          Constructs a chart.
JFreeChart(java.lang.String title, java.awt.Font titleFont, Plot plot, boolean createLegend)
          Constructs a chart.
 
Method Summary
 void addChangeListener(com.jrefinery.chart.event.ChartChangeListener listener)
          Registers an object for notification of changes to the chart.
 void addTitle(AbstractTitle title)
          Adds the chart title, and notifies registered listeners that the chart has been modified.
 java.awt.image.BufferedImage createBufferedImage(int width, int height)
          Creates and returns a buffered image into which the chart has been drawn.
 void draw(java.awt.Graphics2D g2, java.awt.geom.Rectangle2D chartArea)
          Draws the chart on a Java 2D graphics device (such as the screen or a printer).
 void draw(java.awt.Graphics2D g2, java.awt.geom.Rectangle2D chartArea, ChartRenderingInfo info)
          Draws the chart on a Java 2D graphics device (such as the screen or a printer).
 void fireChartChanged()
          Sends a default ChartChangeEvent to all registered listeners.
 boolean getAntiAlias()
          Returns a flag that indicates whether or not anti-aliasing is used when the chart is drawn.
 java.awt.Image getBackgroundImage()
          Returns the chart's background image (possibly null).
 float getBackgroundImageAlpha()
          Returns the alpha-transparency for the chart's background image.
 java.awt.Paint getBackgroundPaint()
          Returns the color/shade used to fill the chart background.
 CategoryPlot getCategoryPlot()
          Returns the plot cast as a CategoryPlot.
 Legend getLegend()
          Returns the chart legend.
 Plot getPlot()
          Returns the plot for the chart.
 AbstractTitle getTitle(int index)
          Returns a chart title.
 int getTitleCount()
          Returns the number of titles for the chart.
 java.util.List getTitles()
          Returns a reference to the list of titles.
 XYPlot getXYPlot()
          Returns the plot cast as an XYPlot.
 void handleClick(int x, int y, ChartRenderingInfo info)
          Handles a 'click' on the chart.
 void handleClick(int x, int y, java.awt.geom.Rectangle2D chartArea)
           
 void legendChanged(com.jrefinery.chart.event.LegendChangeEvent event)
          Receives notification that the chart legend has changed, and passes this on to registered listeners.
static void main(java.lang.String[] args)
          Prints information about JFreeChart to standard output.
protected  void notifyListeners(com.jrefinery.chart.event.ChartChangeEvent event)
          Sends a ChartChangeEvent to all registered listeners.
 void plotChanged(com.jrefinery.chart.event.PlotChangeEvent event)
          Receives notification that the plot has changed, and passes this on to registered listeners.
 void removeChangeListener(com.jrefinery.chart.event.ChartChangeListener listener)
          Deregisters an object for notification of changes to the chart.
 void setAntiAlias(boolean flag)
          Sets a flag that indicates whether or not anti-aliasing is used when the chart is drawn.
 void setBackgroundImage(java.awt.Image image)
          Sets the chart's background image (null permitted).
 void setBackgroundImageAlpha(float alpha)
          Sets the alpha-transparency for the chart's background image.
 void setBackgroundPaint(java.awt.Paint paint)
          Sets the color/shade used to fill the chart background.
 void setLegend(Legend legend)
          Sets the chart legend.
 void setTitles(java.util.List titles)
          Sets the title list for the chart (completely replaces any existing titles).
 void titleChanged(com.jrefinery.chart.event.TitleChangeEvent event)
          Receives notification that a chart title has changed, and passes this on to registered listeners.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

titles

protected java.util.List titles
The chart title(s).


legend

protected Legend legend
The chart legend.


plot

protected Plot plot
Draws the visual representation of the data.


antialias

protected boolean antialias
Flag that determines whether or not the chart is drawn with anti-aliasing.


backgroundPaint

protected java.awt.Paint backgroundPaint
Paint used to draw the background of the chart.


backgroundImage

protected java.awt.Image backgroundImage
An optional background image for the chart.


backgroundImageAlpha

protected float backgroundImageAlpha
The alpha transparency for the background image.


listeners

protected java.util.List listeners
Storage for registered change listeners.

Constructor Detail

JFreeChart

public JFreeChart(Plot plot)
Constructs a chart.

Note that the ChartFactory class contains static methods that will return a ready-made chart.

Parameters:
plot - Controller of the visual representation of the data.

JFreeChart

public JFreeChart(java.lang.String title,
                  java.awt.Font titleFont,
                  Plot plot,
                  boolean createLegend)
Constructs a chart.

Note that the ChartFactory class contains static methods that will return a ready-made chart.

Parameters:
title - The main chart title.
titleFont - The font for displaying the chart title.
plot - Controller of the visual representation of the data.
createLegend - Flag indicating whether or not a legend should be created for the chart.
Method Detail

getTitles

public java.util.List getTitles()
Returns a reference to the list of titles.

Returns:
A reference to the list of titles.

setTitles

public void setTitles(java.util.List titles)
Sets the title list for the chart (completely replaces any existing titles).

Parameters:
titles - The new list of titles.

getTitleCount

public int getTitleCount()
Returns the number of titles for the chart.

Returns:
The number of titles for the chart.

getTitle

public AbstractTitle getTitle(int index)
Returns a chart title.

Parameters:
The - index of the chart title (zero based).
Returns:
A chart title.

addTitle

public void addTitle(AbstractTitle title)
Adds the chart title, and notifies registered listeners that the chart has been modified.

Parameters:
title - The chart title.

getLegend

public Legend getLegend()
Returns the chart legend.

Returns:
The chart legend (possibly null).

setLegend

public void setLegend(Legend legend)
Sets the chart legend. Registered listeners are notified that the chart has been modified.

Parameters:
legend - The new chart legend (null permitted).

getPlot

public Plot getPlot()
Returns the plot for the chart. The plot is a class responsible for coordinating the visual representation of the data, including the axes (if any).

Returns:
The plot.

getCategoryPlot

public CategoryPlot getCategoryPlot()
Returns the plot cast as a CategoryPlot. If the plot is not an instance of CategoryPlot, then a ClassCastException is thrown.

Returns:
The plot.

getXYPlot

public XYPlot getXYPlot()
Returns the plot cast as an XYPlot. If the plot is not an instance of XYPlot, then a ClassCastException is thrown.

Returns:
The plot.

getAntiAlias

public boolean getAntiAlias()
Returns a flag that indicates whether or not anti-aliasing is used when the chart is drawn.

Returns:
The flag.

setAntiAlias

public void setAntiAlias(boolean flag)
Sets a flag that indicates whether or not anti-aliasing is used when the chart is drawn.

Anti-aliasing can improve the appearance of charts.

Parameters:
flag - The new value of the flag.

getBackgroundPaint

public java.awt.Paint getBackgroundPaint()
Returns the color/shade used to fill the chart background.

Returns:
The color/shade used to fill the chart background.

setBackgroundPaint

public void setBackgroundPaint(java.awt.Paint paint)
Sets the color/shade used to fill the chart background. All registered listeners are notified that the chart has been changed.

Parameters:
paint - The new background color/shade.

getBackgroundImage

public java.awt.Image getBackgroundImage()
Returns the chart's background image (possibly null).

Returns:
The image.

setBackgroundImage

public void setBackgroundImage(java.awt.Image image)
Sets the chart's background image (null permitted). Registered listeners are notified that the chart has been changed.

Parameters:
image - The image.

getBackgroundImageAlpha

public float getBackgroundImageAlpha()
Returns the alpha-transparency for the chart's background image.

Returns:
The alpha-transparency.

setBackgroundImageAlpha

public void setBackgroundImageAlpha(float alpha)
Sets the alpha-transparency for the chart's background image. Registered listeners are notified that the chart has been changed.

Parameters:
alpha - The alpha value.

draw

public void draw(java.awt.Graphics2D g2,
                 java.awt.geom.Rectangle2D chartArea)
Draws the chart on a Java 2D graphics device (such as the screen or a printer).

This method is the focus of the entire JFreeChart library.

Parameters:
g2 - The graphics device.
chartArea - The area within which the chart should be drawn.

draw

public void draw(java.awt.Graphics2D g2,
                 java.awt.geom.Rectangle2D chartArea,
                 ChartRenderingInfo info)
Draws the chart on a Java 2D graphics device (such as the screen or a printer).

This method is the focus of the entire JFreeChart library.

Parameters:
g2 - The graphics device.
chartArea - The area within which the chart should be drawn.
info - Records info about the drawing (null means collect no info).

createBufferedImage

public java.awt.image.BufferedImage createBufferedImage(int width,
                                                        int height)
Creates and returns a buffered image into which the chart has been drawn.

Parameters:
width - The width.
height - The height.

handleClick

public void handleClick(int x,
                        int y,
                        ChartRenderingInfo info)
Handles a 'click' on the chart.

JFreeChart is not a UI component, so some other object (e.g. JFreeChartPanel) needs to capture the click event and pass it onto the JFreeChart object. If you are not using JFreeChart in a client application, then this method is not required (and hopefully it doesn't get in the way).


handleClick

public void handleClick(int x,
                        int y,
                        java.awt.geom.Rectangle2D chartArea)

addChangeListener

public void addChangeListener(com.jrefinery.chart.event.ChartChangeListener listener)
Registers an object for notification of changes to the chart.

Parameters:
listener - The object being registered.

removeChangeListener

public void removeChangeListener(com.jrefinery.chart.event.ChartChangeListener listener)
Deregisters an object for notification of changes to the chart.

Parameters:
listener - The object being deregistered.

fireChartChanged

public void fireChartChanged()
Sends a default ChartChangeEvent to all registered listeners.

This method is for convenience only.


notifyListeners

protected void notifyListeners(com.jrefinery.chart.event.ChartChangeEvent event)
Sends a ChartChangeEvent to all registered listeners.

Parameters:
event - Information about the event that triggered the notification.

titleChanged

public void titleChanged(com.jrefinery.chart.event.TitleChangeEvent event)
Receives notification that a chart title has changed, and passes this on to registered listeners.

Specified by:
titleChanged in interface com.jrefinery.chart.event.TitleChangeListener
Parameters:
event - Information about the chart title change.

legendChanged

public void legendChanged(com.jrefinery.chart.event.LegendChangeEvent event)
Receives notification that the chart legend has changed, and passes this on to registered listeners.

Specified by:
legendChanged in interface com.jrefinery.chart.event.LegendChangeListener
Parameters:
event - Information about the chart legend change.

plotChanged

public void plotChanged(com.jrefinery.chart.event.PlotChangeEvent event)
Receives notification that the plot has changed, and passes this on to registered listeners.

Specified by:
plotChanged in interface com.jrefinery.chart.event.PlotChangeListener
Parameters:
event - Information about the plot change.

main

public static void main(java.lang.String[] args)
Prints information about JFreeChart to standard output.



Copyright © 2003 MBARI.

The Monterey Bay Aquarium Research Institute (MBARI) provides this documentation and code "as is", with no warranty, express or implied, of its quality or consistency. It is provided without support and without obligation on the part of MBARI to assist in its use, correction, modification, or enhancement.