- All Implemented Interfaces:
Serializable
,Cloneable
,EventListener
,PlotChangeListener
,TitleChangeListener
,Drawable
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
(which often includes the chart's legend), a Plot
and a
Dataset
(the plot in turn manages a
domain axis and a range 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:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Image
The default background image.static final int
The default background image alignment.static final float
The default background image alpha.static final Paint
The default background color.static final Font
The default font for titles.static final RenderingHints.Key
The key for a rendering hint that can suppress the generation of a shadow effect when drawing the chart. -
Constructor Summary
ConstructorDescriptionJFreeChart
(String title, Font titleFont, Plot plot, boolean createLegend) Creates a new chart with the given title and plot.JFreeChart
(String title, Plot plot) Creates a new chart with the given title and plot.JFreeChart
(Plot plot) Creates a new chart based on the supplied plot. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addChangeListener
(ChartChangeListener listener) Registers an object for notification of changes to the chart.void
addLegend
(LegendTitle legend) Adds a legend to the plot and sends aChartChangeEvent
to all registered listeners.void
addProgressListener
(ChartProgressListener listener) Registers an object for notification of progress events relating to the chart.void
addSubtitle
(int index, Title subtitle) Adds a subtitle at a particular position in the subtitle list, and sends aChartChangeEvent
to all registered listeners.void
addSubtitle
(Title subtitle) Adds a chart subtitle, and notifies registered listeners that the chart has been modified.void
Clears all subtitles from the chart and sends aChartChangeEvent
to all registered listeners.clone()
Clones the object, and takes care of listeners.createBufferedImage
(int width, int height) Creates and returns a buffered image into which the chart has been drawn.createBufferedImage
(int imageWidth, int imageHeight, double drawWidth, double drawHeight, ChartRenderingInfo info) Creates and returns a buffered image into which the chart has been drawn.createBufferedImage
(int width, int height, int imageType, ChartRenderingInfo info) Creates and returns a buffered image into which the chart has been drawn.createBufferedImage
(int width, int height, ChartRenderingInfo info) Creates and returns a buffered image into which the chart has been drawn.void
draw
(Graphics2D g2, Rectangle2D area) Draws the chart on a Java 2D graphics device (such as the screen or a printer).void
draw
(Graphics2D g2, Rectangle2D chartArea, Point2D anchor, ChartRenderingInfo info) Draws the chart on a Java 2D graphics device (such as the screen or a printer).void
draw
(Graphics2D g2, Rectangle2D area, ChartRenderingInfo info) Draws the chart on a Java 2D graphics device (such as the screen or a printer).protected EntityCollection
drawTitle
(Title t, Graphics2D g2, Rectangle2D area, boolean entities) Draws a title.boolean
Tests this chart for equality with another object.void
Sends a defaultChartChangeEvent
to all registered listeners.boolean
Returns a flag that indicates whether or not anti-aliasing is used when the chart is drawn.Returns the background image for the chart, ornull
if there is no image.int
Returns the background image alignment.float
Returns the alpha-transparency for the chart's background image.Returns the paint used for the chart background.Returns the paint used to draw the chart border (if visible).Returns the stroke used to draw the chart border (if visible).Returns the plot cast as aCategoryPlot
.boolean
Returns the flag that controls whether or not rendering hints (ChartHints.KEY_BEGIN_ELEMENT
andChartHints.KEY_END_ELEMENT
) that identify chart elements are added during rendering.getID()
Returns the ID for the chart.Returns the legend for the chart, if there is one.getLegend
(int index) Returns the nth legend for a chart, ornull
.Returns the padding between the chart border and the chart drawing area.getPlot()
Returns the plot for the chart.Returns the collection of rendering hints for the chart.getSubtitle
(int index) Returns a chart subtitle.int
Returns the number of titles for the chart.Returns the list of subtitles for the chart.Returns the current value stored in the rendering hints table forRenderingHints.KEY_TEXT_ANTIALIASING
.getTitle()
Returns the main chart title.Returns the plot cast as anXYPlot
.void
handleClick
(int x, int y, ChartRenderingInfo info) Handles a 'click' on the chart.boolean
Returns a flag that controls whether or not a border is drawn around the outside of the chart.boolean
isNotify()
Returns a flag that controls whether or not change events are sent to registered listeners.protected void
notifyListeners
(ChartChangeEvent event) Sends aChartChangeEvent
to all registered listeners.protected void
Sends aChartProgressEvent
to all registered listeners.void
plotChanged
(PlotChangeEvent event) Receives notification that the plot has changed, and passes this on to registered listeners.void
removeChangeListener
(ChartChangeListener listener) Deregisters an object for notification of changes to the chart.void
Removes the first legend in the chart and sends aChartChangeEvent
to all registered listeners.void
removeProgressListener
(ChartProgressListener listener) Deregisters an object for notification of changes to the chart.void
removeSubtitle
(Title title) Removes the specified subtitle and sends aChartChangeEvent
to all registered listeners.void
setAntiAlias
(boolean flag) Sets a flag that indicates whether or not anti-aliasing is used when the chart is drawn.void
setBackgroundImage
(Image image) Sets the background image for the chart and sends aChartChangeEvent
to all registered listeners.void
setBackgroundImageAlignment
(int alignment) Sets the background alignment.void
setBackgroundImageAlpha
(float alpha) Sets the alpha-transparency for the chart's background image.void
setBackgroundPaint
(Paint paint) Sets the paint used to fill the chart background and sends aChartChangeEvent
to all registered listeners.void
setBorderPaint
(Paint paint) Sets the paint used to draw the chart border (if visible).void
setBorderStroke
(Stroke stroke) Sets the stroke used to draw the chart border (if visible).void
setBorderVisible
(boolean visible) Sets a flag that controls whether or not a border is drawn around the outside of the chart.void
setElementHinting
(boolean hinting) Sets the flag that controls whether or not rendering hints (ChartHints.KEY_BEGIN_ELEMENT
andChartHints.KEY_END_ELEMENT
) that identify chart elements are added during rendering.void
Sets the ID for the chart.void
setNotify
(boolean notify) Sets a flag that controls whether or not listeners receiveChartChangeEvent
notifications.void
setPadding
(RectangleInsets padding) Sets the padding between the chart border and the chart drawing area, and sends aChartChangeEvent
to all registered listeners.void
setRenderingHints
(RenderingHints renderingHints) Sets the rendering hints for the chart.void
setSubtitles
(List subtitles) Sets the title list for the chart (completely replaces any existing titles) and sends aChartChangeEvent
to all registered listeners.void
setTextAntiAlias
(boolean flag) Sets the value in the rendering hints table forRenderingHints.KEY_TEXT_ANTIALIASING
to eitherRenderingHints.VALUE_TEXT_ANTIALIAS_ON
orRenderingHints.VALUE_TEXT_ANTIALIAS_OFF
, then sends aChartChangeEvent
to all registered listeners.void
setTextAntiAlias
(Object val) Sets the value in the rendering hints table forRenderingHints.KEY_TEXT_ANTIALIASING
and sends aChartChangeEvent
to all registered listeners.void
Sets the chart title and sends aChartChangeEvent
to all registered listeners.void
Sets the main title for the chart and sends aChartChangeEvent
to all registered listeners.void
titleChanged
(TitleChangeEvent event) Receives notification that a chart title has changed, and passes this on to registered listeners.
-
Field Details
-
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:
-
DEFAULT_BACKGROUND_IMAGE_ALPHA
public static final float DEFAULT_BACKGROUND_IMAGE_ALPHAThe default background image alpha.- See Also:
-
KEY_SUPPRESS_SHADOW_GENERATION
The key for a rendering hint that can suppress the generation of a shadow effect when drawing the chart. The hint value must be a Boolean.
-
-
Constructor Details
-
JFreeChart
Creates a new chart based on the supplied plot. The chart will have a legend added automatically, but no title (although you can easily add one later).
Note that theChartFactory
class contains a range of static methods that will return ready-made charts, and often this is a more convenient way to create charts than using this constructor.- Parameters:
plot
- the plot (null
not permitted).
-
JFreeChart
Creates a new chart with the given title and plot. A default font (DEFAULT_TITLE_FONT
) is used for the title, and the chart will have a legend added automatically.
Note that theChartFactory
class contains a range of static methods that will return ready-made charts, and often this is a more convenient way to create charts than using this constructor.- Parameters:
title
- the chart title (null
permitted).plot
- the plot (null
not permitted).
-
JFreeChart
Creates a new chart with the given title and plot. ThecreateLegend
argument specifies whether or not a legend should be added to the chart.
Note that theChartFactory
class contains a range of static methods that will return ready-made charts, and often this is a more convenient way to create charts than using this constructor.- Parameters:
title
- the chart title (null
permitted).titleFont
- the font for displaying the chart title (null
permitted).plot
- controller of the visual representation of the data (null
not permitted).createLegend
- a flag indicating whether or not a legend should be created for the chart.
-
-
Method Details
-
getID
Returns the ID for the chart.- Returns:
- The ID for the chart (possibly
null
).
-
setID
Sets the ID for the chart.- Parameters:
id
- the id (null
permitted).
-
getElementHinting
public boolean getElementHinting()Returns the flag that controls whether or not rendering hints (ChartHints.KEY_BEGIN_ELEMENT
andChartHints.KEY_END_ELEMENT
) that identify chart elements are added during rendering. The default value isfalse
.- Returns:
- A boolean.
- See Also:
-
setElementHinting
public void setElementHinting(boolean hinting) Sets the flag that controls whether or not rendering hints (ChartHints.KEY_BEGIN_ELEMENT
andChartHints.KEY_END_ELEMENT
) that identify chart elements are added during rendering.- Parameters:
hinting
- the new flag value.- See Also:
-
getRenderingHints
Returns the collection of rendering hints for the chart.- Returns:
- The rendering hints for the chart (never
null
). - See Also:
-
setRenderingHints
Sets the rendering hints for the chart. These will be added (using theGraphics2D.addRenderingHints()
method) near the start of theJFreeChart.draw()
method.- Parameters:
renderingHints
- the rendering hints (null
not permitted).- See Also:
-
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.
- See Also:
-
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.- See Also:
-
getBorderStroke
Returns the stroke used to draw the chart border (if visible).- Returns:
- The border stroke.
- See Also:
-
setBorderStroke
Sets the stroke used to draw the chart border (if visible).- Parameters:
stroke
- the stroke.- See Also:
-
getBorderPaint
Returns the paint used to draw the chart border (if visible).- Returns:
- The border paint.
- See Also:
-
setBorderPaint
Sets the paint used to draw the chart border (if visible).- Parameters:
paint
- the paint.- See Also:
-
getPadding
Returns the padding between the chart border and the chart drawing area.- Returns:
- The padding (never
null
). - See Also:
-
setPadding
Sets the padding between the chart border and the chart drawing area, and sends aChartChangeEvent
to all registered listeners.- Parameters:
padding
- the padding (null
not permitted).- See Also:
-
getTitle
Returns the main chart title. Very often a chart will have just one title, so we make this case simple by providing accessor methods for the main title. However, multiple titles are supported - see theaddSubtitle(Title)
method.- Returns:
- The chart title (possibly
null
). - See Also:
-
setTitle
Sets the main title for the chart and sends aChartChangeEvent
to all registered listeners. If you do not want a title for the chart, set it tonull
. If you want more than one title on a chart, use theaddSubtitle(Title)
method.- Parameters:
title
- the title (null
permitted).- See Also:
-
setTitle
Sets the chart title and sends aChartChangeEvent
to all registered listeners. This is a convenience method that ends up calling thesetTitle(TextTitle)
method. If there is an existing title, its text is updated, otherwise a new title using the default font is added to the chart. Iftext
isnull
the chart title is set tonull
.- Parameters:
text
- the title text (null
permitted).- See Also:
-
addLegend
Adds a legend to the plot and sends aChartChangeEvent
to all registered listeners.- Parameters:
legend
- the legend (null
not permitted).- See Also:
-
getLegend
Returns the legend for the chart, if there is one. Note that a chart can have more than one legend - this method returns the first.- Returns:
- The legend (possibly
null
). - See Also:
-
getLegend
Returns the nth legend for a chart, ornull
.- Parameters:
index
- the legend index (zero-based).- Returns:
- The legend (possibly
null
). - See Also:
-
removeLegend
public void removeLegend()Removes the first legend in the chart and sends aChartChangeEvent
to all registered listeners.- See Also:
-
getSubtitles
Returns the list of subtitles for the chart.- Returns:
- The subtitle list (possibly empty, but never
null
). - See Also:
-
setSubtitles
Sets the title list for the chart (completely replaces any existing titles) and sends aChartChangeEvent
to all registered listeners.- Parameters:
subtitles
- the new list of subtitles (null
not permitted).- See Also:
-
getSubtitleCount
public int getSubtitleCount()Returns the number of titles for the chart.- Returns:
- The number of titles for the chart.
- See Also:
-
getSubtitle
Returns a chart subtitle.- Parameters:
index
- the index of the chart subtitle (zero based).- Returns:
- A chart subtitle.
- See Also:
-
addSubtitle
Adds a chart subtitle, and notifies registered listeners that the chart has been modified.- Parameters:
subtitle
- the subtitle (null
not permitted).- See Also:
-
addSubtitle
Adds a subtitle at a particular position in the subtitle list, and sends aChartChangeEvent
to all registered listeners.- Parameters:
index
- the index (in the range 0 togetSubtitleCount()
).subtitle
- the subtitle to add (null
not permitted).
-
clearSubtitles
public void clearSubtitles()Clears all subtitles from the chart and sends aChartChangeEvent
to all registered listeners.- See Also:
-
removeSubtitle
Removes the specified subtitle and sends aChartChangeEvent
to all registered listeners.- Parameters:
title
- the title.- See Also:
-
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 aClassCastException
is thrown.- Returns:
- The plot.
- See Also:
-
getXYPlot
Returns the plot cast as anXYPlot
.NOTE: if the plot is not an instance of
XYPlot
, then aClassCastException
is thrown.- Returns:
- The plot.
- See Also:
-
getAntiAlias
public boolean getAntiAlias()Returns a flag that indicates whether or not anti-aliasing is used when the chart is drawn.- Returns:
- The flag.
- See Also:
-
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.- See Also:
-
getTextAntiAlias
Returns the current value stored in the rendering hints table forRenderingHints.KEY_TEXT_ANTIALIASING
.- Returns:
- The hint value (possibly
null
). - See Also:
-
setTextAntiAlias
public void setTextAntiAlias(boolean flag) Sets the value in the rendering hints table forRenderingHints.KEY_TEXT_ANTIALIASING
to eitherRenderingHints.VALUE_TEXT_ANTIALIAS_ON
orRenderingHints.VALUE_TEXT_ANTIALIAS_OFF
, then sends aChartChangeEvent
to all registered listeners.- Parameters:
flag
- the new value of the flag.- See Also:
-
setTextAntiAlias
Sets the value in the rendering hints table forRenderingHints.KEY_TEXT_ANTIALIASING
and sends aChartChangeEvent
to all registered listeners.- Parameters:
val
- the new value (null
permitted).- See Also:
-
getBackgroundPaint
Returns the paint used for the chart background.- Returns:
- The paint (possibly
null
). - See Also:
-
setBackgroundPaint
Sets the paint used to fill the chart background and sends aChartChangeEvent
to all registered listeners.- Parameters:
paint
- the paint (null
permitted).- See Also:
-
getBackgroundImage
Returns the background image for the chart, ornull
if there is no image.- Returns:
- The image (possibly
null
). - See Also:
-
setBackgroundImage
Sets the background image for the chart and sends aChartChangeEvent
to all registered listeners.- Parameters:
image
- the image (null
permitted).- See Also:
-
getBackgroundImageAlignment
public int getBackgroundImageAlignment()Returns the background image alignment. Alignment constants are defined in theAlign
class.- Returns:
- The alignment.
- See Also:
-
setBackgroundImageAlignment
public void setBackgroundImageAlignment(int alignment) Sets the background alignment. Alignment options are defined by theAlign
class.- Parameters:
alignment
- the alignment.- See Also:
-
getBackgroundImageAlpha
public float getBackgroundImageAlpha()Returns the alpha-transparency for the chart's background image.- Returns:
- The alpha-transparency.
- See Also:
-
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.- See Also:
-
isNotify
public boolean isNotify()Returns a flag that controls whether or not change events are sent to registered listeners.- Returns:
- A boolean.
- See Also:
-
setNotify
public void setNotify(boolean notify) Sets a flag that controls whether or not listeners receiveChartChangeEvent
notifications.- Parameters:
notify
- a boolean.- See Also:
-
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.
-
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 (null
permitted).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:
t
- the title (null
not permitted).g2
- the graphics device (null
not permitted).area
- the chart area, excluding any existing titles (null
not permitted).entities
- a flag that controls whether or not an entity collection is returned for the title.- Returns:
- An entity collection for the title (possibly
null
).
-
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 (null
permitted).- 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 (null
permitted).- 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 (for example,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.- 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 (null
not permitted).
-
addChangeListener
Registers an object for notification of changes to the chart.- Parameters:
listener
- the listener (null
not permitted).- See Also:
-
removeChangeListener
Deregisters an object for notification of changes to the chart.- Parameters:
listener
- the listener (null
not permitted)- See Also:
-
fireChartChanged
public void fireChartChanged()Sends a defaultChartChangeEvent
to all registered listeners.This method is for convenience only.
-
notifyListeners
Sends aChartChangeEvent
to all registered listeners.- Parameters:
event
- information about the event that triggered the notification.
-
addProgressListener
Registers an object for notification of progress events relating to the chart.- Parameters:
listener
- the object being registered.- See Also:
-
removeProgressListener
Deregisters an object for notification of changes to the chart.- Parameters:
listener
- the object being deregistered.- See Also:
-
notifyListeners
Sends aChartProgressEvent
to all registered listeners.- Parameters:
event
- information about the event that triggered the notification.
-
titleChanged
Receives notification that a chart title has changed, and passes this on to registered listeners.- Specified by:
titleChanged
in interfaceTitleChangeListener
- Parameters:
event
- information about the chart title change.
-
plotChanged
Receives notification that the plot has changed, and passes this on to registered listeners.- Specified by:
plotChanged
in interfacePlotChangeListener
- Parameters:
event
- information about the plot change.
-
equals
Tests this chart for equality with another object. -
clone
Clones the object, and takes care of listeners. Note: caller shall register its own listeners on cloned graph.- Overrides:
clone
in classObject
- Returns:
- A clone.
- Throws:
CloneNotSupportedException
- if the chart is not cloneable.
-