Class JFreeChart
- All Implemented Interfaces:
Serializable,Cloneable,EventListener,LegendChangeListener,PlotChangeListener,TitleChangeListener,org.jfree.ui.Drawable
public class JFreeChart extends Object implements org.jfree.ui.Drawable, TitleChangeListener, LegendChangeListener, PlotChangeListener, Serializable, Cloneable
JFreeChart coordinates several objects to achieve its aim of being able to
draw a chart on a Java 2D graphics device: a list of Title objects, a
Legend, a Plot and a Dataset (the plot
in turn manages a horizontal axis and a vertical axis).
You should use a ChartPanel to display a chart in a GUI.
The ChartFactory class contains static methods for creating
'ready-made' charts.
- See Also:
ChartPanel,ChartFactory,Title,Legend,Plot, Serialized Form
-
Field Summary
Fields Modifier and Type Field Description static ImageDEFAULT_BACKGROUND_IMAGEThe default background image.static intDEFAULT_BACKGROUND_IMAGE_ALIGNMENTThe default background image alignment.static floatDEFAULT_BACKGROUND_IMAGE_ALPHAThe default background image alpha.static PaintDEFAULT_BACKGROUND_PAINTThe default background color.static FontDEFAULT_TITLE_FONTThe default font for titles.static org.jfree.ui.about.ProjectInfoINFOInformation about the project. -
Constructor Summary
Constructors Constructor Description JFreeChart(String title, Font titleFont, Plot plot, boolean createLegend)Constructs a chart.JFreeChart(String title, Plot plot)Creates a new chart.JFreeChart(Plot plot)Constructs a chart. -
Method Summary
Modifier and Type Method Description voidaddChangeListener(ChartChangeListener listener)Registers an object for notification of changes to the chart.voidaddProgressListener(ChartProgressListener listener)Registers an object for notification of progress events relating to the chart.voidaddSubtitle(Title subtitle)Adds a chart subtitle, and notifies registered listeners that the chart has been modified.Objectclone()Clones the object, and takes care of listeners.BufferedImagecreateBufferedImage(int width, int height)Creates and returns a buffered image into which the chart has been drawn.BufferedImagecreateBufferedImage(int imageWidth, int imageHeight, double drawWidth, double drawHeight, ChartRenderingInfo info)Creates and returns a buffered image into which the chart has been drawn.BufferedImagecreateBufferedImage(int width, int height, int imageType, ChartRenderingInfo info)Creates and returns a buffered image into which the chart has been drawn.BufferedImagecreateBufferedImage(int width, int height, ChartRenderingInfo info)Creates and returns a buffered image into which the chart has been drawn.voiddraw(Graphics2D g2, Rectangle2D area)Draws the chart on a Java 2D graphics device (such as the screen or a printer).voiddraw(Graphics2D g2, Rectangle2D chartArea, Point2D anchor, ChartRenderingInfo info)Draws the chart on a Java 2D graphics device (such as the screen or a printer).voiddraw(Graphics2D g2, Rectangle2D area, ChartRenderingInfo info)Draws the chart on a Java 2D graphics device (such as the screen or a printer).voiddrawTitle(Title title, Graphics2D g2, Rectangle2D area)Draws a title.booleanequals(Object obj)Tests this chart for equality with another object.voidfireChartChanged()Sends a defaultChartChangeEventto all registered listeners.booleangetAntiAlias()Returns a flag that indicates whether or not anti-aliasing is used when the chart is drawn.ImagegetBackgroundImage()Returns the background image for the chart, ornullif there is no image.intgetBackgroundImageAlignment()Returns the background image alignment.floatgetBackgroundImageAlpha()Returns the alpha-transparency for the chart's background image.PaintgetBackgroundPaint()Returns the paint used for the chart background.PaintgetBorderPaint()Returns the paint used to draw the chart border (if visible).StrokegetBorderStroke()Returns the stroke used to draw the chart border (if visible).CategoryPlotgetCategoryPlot()Returns the plot cast as aCategoryPlot.LegendgetLegend()Returns the chart legend.PlotgetPlot()Returns the plot for the chart.RenderingHintsgetRenderingHints()Returns the collection of rendering hints for the chart.TitlegetSubtitle(int index)Returns a chart subtitle.intgetSubtitleCount()Returns the number of titles for the chart.ListgetSubtitles()Returns the list of subtitles.booleangetSuppressChartChangeEvents()Deprecated.Use isNotify() instead.TextTitlegetTitle()Returns the chart title.XYPlotgetXYPlot()Returns the plot cast as anXYPlot.voidhandleClick(int x, int y, ChartRenderingInfo info)Handles a 'click' on the chart.booleanisBorderVisible()Returns a flag that controls whether or not a border is drawn around the outside of the chart.booleanisNotify()Returns a flag that controls whether or not change events are sent to registered listeners.voidlegendChanged(LegendChangeEvent event)Receives notification that the chart legend has changed, and passes this on to registered listeners.static voidmain(String[] args)Prints information about JFreeChart to standard output.voidplotChanged(PlotChangeEvent event)Receives notification that the plot has changed, and passes this on to registered listeners.voidremoveChangeListener(ChartChangeListener listener)Deregisters an object for notification of changes to the chart.voidremoveProgressListener(ChartProgressListener listener)Deregisters an object for notification of changes to the chart.voidsetAntiAlias(boolean flag)Sets a flag that indicates whether or not anti-aliasing is used when the chart is drawn.voidsetBackgroundImage(Image image)Sets the background image for the chart and sends aChartChangeEventto all registered listeners.voidsetBackgroundImageAlignment(int alignment)Sets the background alignment.voidsetBackgroundImageAlpha(float alpha)Sets the alpha-transparency for the chart's background image.voidsetBackgroundPaint(Paint paint)Sets the paint used to fill the chart background and sends aChartChangeEventto all registered listeners.voidsetBorderPaint(Paint paint)Sets the paint used to draw the chart border (if visible).voidsetBorderStroke(Stroke stroke)Sets the stroke used to draw the chart border (if visible).voidsetBorderVisible(boolean visible)Sets a flag that controls whether or not a border is drawn around the outside of the chart.voidsetHidePlot(boolean hidePlot)voidsetLegend(Legend legend)Sets the chart legend.voidsetNotify(boolean notify)Sets a flag that controls whether or not listeners receiveChartChangeEventnotifications.voidsetRenderingHints(RenderingHints renderingHints)Sets the rendering hints for the chart.voidsetSubtitles(List subtitles)Sets the title list for the chart (completely replaces any existing titles).voidsetSuppressChartChangeEvents(boolean flag)Deprecated.Use setNotify(boolean) instead.voidsetTitle(String title)Sets the chart title.voidsetTitle(TextTitle title)Sets the title for the chart.voidtitleChanged(TitleChangeEvent event)Receives notification that a chart title has changed, and passes this on to registered listeners.
-
Field Details
-
INFO
public static final org.jfree.ui.about.ProjectInfo INFOInformation about the project. -
DEFAULT_TITLE_FONT
The default font for titles. -
DEFAULT_BACKGROUND_PAINT
The default background color. -
DEFAULT_BACKGROUND_IMAGE
The default background image. -
DEFAULT_BACKGROUND_IMAGE_ALIGNMENT
public static final int DEFAULT_BACKGROUND_IMAGE_ALIGNMENTThe default background image alignment.- See Also:
- Constant Field Values
-
DEFAULT_BACKGROUND_IMAGE_ALPHA
public static float DEFAULT_BACKGROUND_IMAGE_ALPHAThe default background image alpha.
-
-
Constructor Details
-
JFreeChart
Constructs a chart.Note that the
ChartFactoryclass contains static methods that will return a ready-made chart.- Parameters:
plot- controller of the visual representation of the data (nullnot permitted).
-
JFreeChart
Creates a new chart.- Parameters:
title- the chart title (nullpermitted).plot- the plot (nullnot permitted).
-
JFreeChart
Constructs a chart.Note that the ChartFactory class contains static methods that will return a ready-made chart.
- Parameters:
title- the chart title (nullpermitted).titleFont- the font for displaying the chart title (nullpermitted).plot- controller of the visual representation of the data (nullnot permitted).createLegend- a flag indicating whether or not a legend should be created for the chart.
-
-
Method Details
-
getRenderingHints
Returns the collection of rendering hints for the chart.- Returns:
- The rendering hints for the chart, never null.
-
setRenderingHints
Sets the rendering hints for the chart. These will be added (using the Graphics2D addRenderingHints(...) method) near the start of the JFreeChart.draw(...) method.- Parameters:
renderingHints- the rendering hints (nullnot permitted).
-
isBorderVisible
public boolean isBorderVisible()Returns a flag that controls whether or not a border is drawn around the outside of the chart.- Returns:
- A boolean.
-
setBorderVisible
public void setBorderVisible(boolean visible)Sets a flag that controls whether or not a border is drawn around the outside of the chart.- Parameters:
visible- the flag.
-
getBorderStroke
Returns the stroke used to draw the chart border (if visible).- Returns:
- The border stroke.
-
setBorderStroke
Sets the stroke used to draw the chart border (if visible).- Parameters:
stroke- the stroke.
-
getBorderPaint
Returns the paint used to draw the chart border (if visible).- Returns:
- The border paint.
-
setBorderPaint
Sets the paint used to draw the chart border (if visible).- Parameters:
paint- the paint.
-
getTitle
Returns the chart title.- Returns:
- the chart title (possibly
null).
-
setTitle
Sets the title for the chart. If you do not want a title for the chart, set it tonull.- Parameters:
title- the title (nullpermitted).
-
setTitle
Sets the chart title. This is a convenience method that ends up calling thesetTitle(TextTitle)method.- Parameters:
title- the new title (nullpermitted).
-
getSubtitles
Returns the list of subtitles.- Returns:
- the subtitle list.
-
setSubtitles
Sets the title list for the chart (completely replaces any existing titles).- Parameters:
subtitles- the new list of subtitles.
-
getSubtitleCount
public int getSubtitleCount()Returns the number of titles for the chart.- Returns:
- the number of titles for the chart.
-
getSubtitle
Returns a chart subtitle.- Parameters:
index- the index of the chart subtitle (zero based).- Returns:
- a chart subtitle.
-
addSubtitle
Adds a chart subtitle, and notifies registered listeners that the chart has been modified.- Parameters:
subtitle- the subtitle.
-
getLegend
Returns the chart legend.- Returns:
- the chart legend (possibly
null).
-
setLegend
Sets the chart legend. Registered listeners are notified that the chart has been modified. The legends chart reference is updated.- Parameters:
legend- the new chart legend (null permitted).
-
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
Returns the plot cast as aCategoryPlot.NOTE: if the plot is not an instance of
CategoryPlot, then aClassCastExceptionis thrown.- Returns:
- the plot.
-
getXYPlot
Returns the plot cast as anXYPlot.NOTE: if the plot is not an instance of
XYPlot, then aClassCastExceptionis 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 usually improves the appearance of charts, but is slower.
- Parameters:
flag- the new value of the flag.
-
getBackgroundPaint
Returns the paint used for the chart background.- Returns:
- The paint (possibly
null).
-
setBackgroundPaint
Sets the paint used to fill the chart background and sends aChartChangeEventto all registered listeners.- Parameters:
paint- the paint (nullpermitted).
-
getBackgroundImage
Returns the background image for the chart, ornullif there is no image.- Returns:
- The image (possibly
null).
-
setBackgroundImage
Sets the background image for the chart and sends aChartChangeEventto all registered listeners.- Parameters:
image- the image (nullpermitted).
-
getBackgroundImageAlignment
public int getBackgroundImageAlignment()Returns the background image alignment. Alignment constants are defined in thecom.jrefinery.ui.Alignclass in the JCommon class library.- Returns:
- The alignment.
-
setBackgroundImageAlignment
public void setBackgroundImageAlignment(int alignment)Sets the background alignment.Alignment options are defined by the
Alignclass.- Parameters:
alignment- the alignment.
-
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.
-
isNotify
public boolean isNotify()Returns a flag that controls whether or not change events are sent to registered listeners.- Returns:
trueorfalse.
-
setNotify
public void setNotify(boolean notify)Sets a flag that controls whether or not listeners receiveChartChangeEventnotifications.- Parameters:
notify- a boolean.
-
draw
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.
- Specified by:
drawin interfaceorg.jfree.ui.Drawable- Parameters:
g2- the graphics device.area- the area within which the chart should be drawn.
-
draw
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.area- the area within which the chart should be drawn.info- records info about the drawing (null means collect no info).
-
draw
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.anchor- the anchor point (in Java2D space) for the chart (nullpermitted).info- records info about the drawing (null means collect no info).
-
drawTitle
Draws a title. The title should be drawn at the top, bottom, left or right of the specified area, and the area should be updated to reflect the amount of space used by the title.- Parameters:
title- the title (nullnot permitted).g2- the graphics device (nullnot permitted).area- the chart area, excluding any existing titles (nullnot permitted).
-
createBufferedImage
Creates and returns a buffered image into which the chart has been drawn.- Parameters:
width- the width.height- the height.- Returns:
- a buffered image.
-
createBufferedImage
Creates and returns a buffered image into which the chart has been drawn.- Parameters:
width- the width.height- the height.info- carries back chart state information (nullpermitted).- Returns:
- a buffered image.
-
createBufferedImage
public BufferedImage createBufferedImage(int width, int height, int imageType, ChartRenderingInfo info)Creates and returns a buffered image into which the chart has been drawn.- Parameters:
width- the width.height- the height.imageType- the image type.info- carries back chart state information (nullpermitted).- Returns:
- a buffered image.
-
createBufferedImage
public BufferedImage createBufferedImage(int imageWidth, int imageHeight, double drawWidth, double drawHeight, ChartRenderingInfo info)Creates and returns a buffered image into which the chart has been drawn.- Parameters:
imageWidth- the image width.imageHeight- the image height.drawWidth- the width for drawing the chart (will be scaled to fit image).drawHeight- the height for drawing the chart (will be scaled to fit image).info- optional object for collection chart dimension and entity information.- Returns:
- a buffered image.
-
handleClick
Handles a 'click' on the chart.JFreeChart is not a UI component, so some other object (e.g. ChartPanel) 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).
- Parameters:
x- x-coordinate of the click (in Java2D space).y- y-coordinate of the click (in Java2D space).info- contains chart dimension and entity information.
-
addChangeListener
Registers an object for notification of changes to the chart.- Parameters:
listener- the object being registered.
-
removeChangeListener
Deregisters an object for notification of changes to the chart.- Parameters:
listener- the object being deregistered.
-
fireChartChanged
public void fireChartChanged()Sends a defaultChartChangeEventto all registered listeners.This method is for convenience only.
-
addProgressListener
Registers an object for notification of progress events relating to the chart.- Parameters:
listener- the object being registered.
-
removeProgressListener
Deregisters an object for notification of changes to the chart.- Parameters:
listener- the object being deregistered.
-
titleChanged
Receives notification that a chart title has changed, and passes this on to registered listeners.- Specified by:
titleChangedin interfaceTitleChangeListener- Parameters:
event- information about the chart title change.
-
legendChanged
Receives notification that the chart legend has changed, and passes this on to registered listeners.- Specified by:
legendChangedin interfaceLegendChangeListener- Parameters:
event- information about the chart legend change.
-
plotChanged
Receives notification that the plot has changed, and passes this on to registered listeners.- Specified by:
plotChangedin interfacePlotChangeListener- Parameters:
event- information about the plot change.
-
equals
Tests this chart for equality with another object. -
main
Prints information about JFreeChart to standard output.- Parameters:
args- no arguments are honored.
-
getSuppressChartChangeEvents
public boolean getSuppressChartChangeEvents()Deprecated.Use isNotify() instead.Returns the flag that controls whether notification of chart change events is suppressed.- Returns:
- The flag.
-
setSuppressChartChangeEvents
public void setSuppressChartChangeEvents(boolean flag)Deprecated.Use setNotify(boolean) instead.Sets a flag that is used to suppress notification of chart change events.- Parameters:
flag- the flag.
-
clone
Clones the object, and takes care of listeners. Note: caller shall register its own listeners on cloned graph.- Returns:
- A clone.
- Throws:
CloneNotSupportedException- if the chart is not cloneable.
-
setHidePlot
public void setHidePlot(boolean hidePlot)
-