com.jrefinery.chart
Class PiePlot

java.lang.Object
  |
  +--com.jrefinery.chart.Plot
        |
        +--com.jrefinery.chart.PiePlot
All Implemented Interfaces:
AxisChangeListener, AxisConstants, DatasetChangeListener, java.util.EventListener
Direct Known Subclasses:
Pie3DPlot

public class PiePlot
extends Plot

A plot that displays data in the form of a pie chart, using data from any class that implements the PieDataset interface.

Special notes:

  1. The default starting point is 12 o'clock and the pie sections proceed in a clockwise direction, but these settings can be changed
  2. negative values in the dataset are ignored
  3. there are utility methods for creating a PieDataset from a CategoryDataset

Author:
PB
See Also:
Plot, PieDataset

Field Summary
static int ANTICLOCKWISE
          A constant representing the anti-clockwise direction.
static int CLOCKWISE
          A constant indicating the clockwise direction.
static double DEFAULT_INTERIOR_GAP
          The default interior gap percent (currently 20%).
static double DEFAULT_RADIUS
          The default radius percent (currently 100%).
static java.awt.Font DEFAULT_SECTION_LABEL_FONT
          The default section label font.
static double DEFAULT_SECTION_LABEL_GAP
          The default section label gap (currently 10%).
static java.awt.Paint DEFAULT_SECTION_LABEL_PAINT
          The default section label paint.
static java.awt.Font DEFAULT_SERIES_LABEL_FONT
          The default series label font.
static java.awt.Paint DEFAULT_SERIES_LABEL_PAINT
          The default series label paint.
static boolean DEFAULT_SHOW_SERIES_LABELS
          The default for the show series labels flag
static double MAX_INTERIOR_GAP
          The maximum interior gap (currently 40%).
static double MAX_RADIUS
          The maximum radius (currently 100%).
static double MAX_SECTION_LABEL_GAP
          The maximum interior gap (currently 30%).
static int NAME_AND_PERCENT_LABELS
          Constant indicating percent labels on the pie sections.
static int NAME_AND_VALUE_LABELS
          Constant indicating percent labels on the pie sections.
static int NAME_LABELS
          Constant indicating name labels on the pie sections.
static int NO_LABELS
          Constant indicating no labels on the pie sections.
static int PERCENT_LABELS
          Constant indicating percent labels on the pie sections.
static int VALUE_AND_PERCENT_LABELS
          Constant indicating percent labels on the pie sections.
static int VALUE_LABELS
          Constant indicating value labels on the pie sections.
 
Fields inherited from class com.jrefinery.chart.Plot
backgroundAlpha, backgroundImage, backgroundPaint, dataset, DEFAULT_BACKGROUND_ALPHA, DEFAULT_BACKGROUND_PAINT, DEFAULT_FOREGROUND_ALPHA, DEFAULT_INSETS, DEFAULT_OUTLINE_PAINT, DEFAULT_OUTLINE_STROKE, foregroundAlpha, insets, listenerList, MINIMUM_HEIGHT_TO_DRAW, MINIMUM_WIDTH_TO_DRAW, noDataMessage, noDataMessageFont, outlinePaint, outlineStroke, seriesOutlinePaint, seriesOutlineStroke, seriesPaint, seriesStroke, shapeFactory, ZERO
 
Fields inherited from interface com.jrefinery.chart.AxisConstants
DEFAULT_AXIS_LABEL_FONT, DEFAULT_AXIS_LABEL_INSETS, DEFAULT_AXIS_LABEL_PAINT, DEFAULT_TICK_LABEL_FONT, DEFAULT_TICK_LABEL_INSETS, DEFAULT_TICK_LABEL_PAINT, DEFAULT_TICK_PAINT, DEFAULT_TICK_STROKE
 
Constructor Summary
PiePlot(CategoryDataset data)
          Constructs a new pie plot, using default attributes as required.
PiePlot(PieDataset data)
          Constructs a new pie plot, using default attributes as required.
PiePlot(PieDataset data, java.awt.Insets insets, java.awt.Paint backgroundPaint, java.awt.Image backgroundImage, float backgroundAlpha, java.awt.Stroke outlineStroke, java.awt.Paint outlinePaint, float foregroundAlpha, double interiorGapPercent, boolean circular, double radiusPercent, int sectionLabelType, java.awt.Font sectionLabelFont, java.awt.Paint sectionLabelPaint, double sectionLabelGapPercent, java.lang.String valueFormatString, java.lang.String percentFormatString, PieToolTipGenerator toolTipGenerator, PieURLGenerator urlGenerator)
          Constructs a pie plot.
 
Method Summary
protected  java.awt.geom.Point2D calculateLabelLocation(java.awt.geom.Rectangle2D labelBounds, double ascent, java.awt.geom.Rectangle2D unexploded, java.awt.geom.Rectangle2D exploded, double startAngle, double extent, double explodePercent)
          Returns the location for a label, taking into account whether or not the pie section is exploded.
 void draw(java.awt.Graphics2D g2, java.awt.geom.Rectangle2D plotArea, ChartRenderingInfo info)
          Draws the plot on a Java 2D graphics device (such as the screen or a printer).
protected  void drawLabel(java.awt.Graphics2D g2, java.awt.geom.Rectangle2D pieArea, java.awt.geom.Rectangle2D explodedPieArea, PieDataset data, double value, int section, double startAngle, double extent)
          Draws the label for one pie section.
protected  java.awt.geom.Rectangle2D getArcBounds(java.awt.geom.Rectangle2D unexploded, java.awt.geom.Rectangle2D exploded, double startAngle, double extent, double explodePercent)
          Returns a rectangle that can be used to create a pie section (taking into account the amount by which the pie section is 'exploded').
 java.util.Collection getCategories()
          Returns a collection of the categories in the dataset.
 int getDirection()
          Returns the direction in which the pie sections are drawn (clockwise or anti-clockwise).
 double getExplodePercent(int section)
          Returns the amount that a section should be 'exploded'.
 double getInteriorGapPercent()
          Returns the interior gap, measures as a percentage of the available drawing space.
 java.util.List getLegendItemLabels()
          Deprecated. use getLegendItems().
 LegendItemCollection getLegendItems()
          Returns a collection of legend items for the pie chart.
 PieDataset getPieDataset()
          Returns the dataset for the plot, cast as a PieDataset.
 java.lang.String getPlotType()
          Returns a short string describing the type of plot.
 double getRadiusPercent()
          Returns the radius percentage.
 java.awt.Font getSectionLabelFont()
          Returns the section label font.
 double getSectionLabelGapPercent()
          Returns the section label gap, measured as a percentage of the radius.
 java.awt.Paint getSectionLabelPaint()
          Returns the section label paint.
 int getSectionLabelType()
          Returns the section label type.
 java.awt.Font getSeriesLabelFont()
          Returns the series label font.
 java.awt.Paint getSeriesLabelPaint()
          Returns the series label paint.
 boolean getShowSeriesLabels()
          Returns the show series labels flag.
 double getStartAngle()
          Returns the start angle for the first pie section.
 PieToolTipGenerator getToolTipGenerator()
          Returns the tooltip generator (possibly null).
 PieURLGenerator getURLGenerator()
          Returns the URL generator (possibly null).
 boolean isCircular()
          Returns a flag indicating whether the pie chart is circular, or stretched into an elliptical shape.
 void setCircular(boolean flag)
          A flag indicating whether the pie chart is circular, or stretched into an elliptical shape.
protected  void setCircularAttribute(boolean circular)
          Sets the circular attribute, with no side effects.
 void setDirection(int direction)
          Sets the direction (use the constants CLOCKWISE or ANTICLOCKWISE).
 void setExplodePercent(int section, double percent)
          Sets the amount that a pie section should be exploded.
 void setInteriorGapPercent(double percent)
          Sets the interior gap percent.
 void setPercentFormat(java.text.NumberFormat format)
          Sets the format for the value labels.
 void setPercentFormatString(java.lang.String format)
          Sets the format string for the percent labels.
 void setRadiusPercent(double percent)
          Sets the radius percentage.
 void setSectionLabelFont(java.awt.Font font)
          Sets the section label font.
 void setSectionLabelGapPercent(double percent)
          Sets the section label gap percent.
 void setSectionLabelPaint(java.awt.Paint paint)
          Sets the section label paint.
 void setSectionLabelType(int type)
          Sets the section label type.
 void setSeriesLabelFont(java.awt.Font font)
          Sets the series label font.
 void setSeriesLabelPaint(java.awt.Paint paint)
          Sets the series label paint.
 void setShowSeriesLabels(boolean flag)
          Sets the show series labels flag.
 void setStartAngle(double angle)
          Sets the starting angle.
 void setToolTipGenerator(PieToolTipGenerator generator)
          Sets the tool tip generator.
 void setURLGenerator(PieURLGenerator generator)
          Sets the URL generator.
 void setValueFormat(java.text.NumberFormat format)
          Sets the format for the value labels.
 void setValueFormatString(java.lang.String format)
          Sets the format string for the value labels.
 void zoom(double percent)
          A zoom method that does nothing.
 
Methods inherited from class com.jrefinery.chart.Plot
addChangeListener, axisChanged, datasetChanged, drawNoDataMessage, drawOutlineAndBackground, getBackgroundAlpha, getBackgroundPaint, getDataset, getDatasetGroup, getForegroundAlpha, getInsets, getNoDataMessage, getNoDataMessageFont, getOutlinePaint, getOutlineStroke, getSeriesOutlinePaint, getSeriesOutlineStroke, getSeriesPaint, getSeriesStroke, getShape, getShape, getShapeFactory, handleClick, isSubplot, notifyListeners, removeChangeListener, setBackgroundAlpha, setBackgroundImage, setBackgroundPaint, setDataset, setDatasetGroup, setForegroundAlpha, setInsets, setNoDataMessage, setNoDataMessageFont, setOutlinePaint, setOutlineStroke, setSeriesOutlinePaint, setSeriesOutlineStroke, setSeriesOutlineStroke, setSeriesPaint, setSeriesPaint, setSeriesStroke, setSeriesStroke, setShapeFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLOCKWISE

public static final int CLOCKWISE
A constant indicating the clockwise direction.

ANTICLOCKWISE

public static final int ANTICLOCKWISE
A constant representing the anti-clockwise direction.

DEFAULT_INTERIOR_GAP

public static final double DEFAULT_INTERIOR_GAP
The default interior gap percent (currently 20%).

MAX_INTERIOR_GAP

public static final double MAX_INTERIOR_GAP
The maximum interior gap (currently 40%).

DEFAULT_RADIUS

public static final double DEFAULT_RADIUS
The default radius percent (currently 100%).

MAX_RADIUS

public static final double MAX_RADIUS
The maximum radius (currently 100%).

DEFAULT_SECTION_LABEL_FONT

public static final java.awt.Font DEFAULT_SECTION_LABEL_FONT
The default section label font.

DEFAULT_SECTION_LABEL_PAINT

public static final java.awt.Paint DEFAULT_SECTION_LABEL_PAINT
The default section label paint.

DEFAULT_SECTION_LABEL_GAP

public static final double DEFAULT_SECTION_LABEL_GAP
The default section label gap (currently 10%).

MAX_SECTION_LABEL_GAP

public static final double MAX_SECTION_LABEL_GAP
The maximum interior gap (currently 30%).

DEFAULT_SERIES_LABEL_FONT

public static final java.awt.Font DEFAULT_SERIES_LABEL_FONT
The default series label font.

DEFAULT_SERIES_LABEL_PAINT

public static final java.awt.Paint DEFAULT_SERIES_LABEL_PAINT
The default series label paint.

DEFAULT_SHOW_SERIES_LABELS

public static final boolean DEFAULT_SHOW_SERIES_LABELS
The default for the show series labels flag

NO_LABELS

public static final int NO_LABELS
Constant indicating no labels on the pie sections.

NAME_LABELS

public static final int NAME_LABELS
Constant indicating name labels on the pie sections.

VALUE_LABELS

public static final int VALUE_LABELS
Constant indicating value labels on the pie sections.

PERCENT_LABELS

public static final int PERCENT_LABELS
Constant indicating percent labels on the pie sections.

NAME_AND_VALUE_LABELS

public static final int NAME_AND_VALUE_LABELS
Constant indicating percent labels on the pie sections.

NAME_AND_PERCENT_LABELS

public static final int NAME_AND_PERCENT_LABELS
Constant indicating percent labels on the pie sections.

VALUE_AND_PERCENT_LABELS

public static final int VALUE_AND_PERCENT_LABELS
Constant indicating percent labels on the pie sections.
Constructor Detail

PiePlot

public PiePlot(PieDataset data)
Constructs a new pie plot, using default attributes as required.
Parameters:
data - the data.

PiePlot

public PiePlot(CategoryDataset data)
Constructs a new pie plot, using default attributes as required.
Parameters:
data - the data.

PiePlot

public PiePlot(PieDataset data,
               java.awt.Insets insets,
               java.awt.Paint backgroundPaint,
               java.awt.Image backgroundImage,
               float backgroundAlpha,
               java.awt.Stroke outlineStroke,
               java.awt.Paint outlinePaint,
               float foregroundAlpha,
               double interiorGapPercent,
               boolean circular,
               double radiusPercent,
               int sectionLabelType,
               java.awt.Font sectionLabelFont,
               java.awt.Paint sectionLabelPaint,
               double sectionLabelGapPercent,
               java.lang.String valueFormatString,
               java.lang.String percentFormatString,
               PieToolTipGenerator toolTipGenerator,
               PieURLGenerator urlGenerator)
Constructs a pie plot.
Parameters:
data - the data.
insets - amount of blank space around the plot area.
backgroundPaint - an optional color for the plot's background.
backgroundImage - an optional image for the plot's background.
backgroundAlpha - alpha-transparency for the plot's background.
outlineStroke - the Stroke used to draw an outline around the plot.
outlinePaint - the color used to draw an outline around the plot.
foregroundAlpha - the alpha-transparency for the plot.
interiorGapPercent - the interior gap (space for labels) as a percentage of the available space.
circular - flag indicating whether the pie chart is circular or elliptical.
radiusPercent - the radius of the pie chart, as a percentage of the available space (after accounting for interior gap).
sectionLabelType - the type of labels for the pie sections.
sectionLabelFont - the font for the section labels.
sectionLabelPaint - the color for the section labels.
sectionLabelGapPercent - the space between the pie sections and the labels.
valueFormatString - the value format string.
percentFormatString - the percent format string.
urlGenerator - the URL generator.
toolTipGenerator - the tooltip generator.
Method Detail

getStartAngle

public double getStartAngle()
Returns the start angle for the first pie section.

This is measured in degrees starting from 3 o'clock and measuring anti-clockwise.

Returns:
the start angle.

setStartAngle

public void setStartAngle(double angle)
Sets the starting angle.

The initial default value is 90 degrees, which corresponds to 12 o'clock. A value of zero corresponds to 3 o'clock... this is the encoding used by Java's Arc2D class.

Parameters:
angle - the angle (in degrees).

getDirection

public int getDirection()
Returns the direction in which the pie sections are drawn (clockwise or anti-clockwise).
Returns:
the direction.

setDirection

public void setDirection(int direction)
Sets the direction (use the constants CLOCKWISE or ANTICLOCKWISE).
Parameters:
direction - the new direction.

getInteriorGapPercent

public double getInteriorGapPercent()
Returns the interior gap, measures as a percentage of the available drawing space.
Returns:
the interior gap percentage.

setInteriorGapPercent

public void setInteriorGapPercent(double percent)
Sets the interior gap percent.
Parameters:
percent - the percentage.

isCircular

public boolean isCircular()
Returns a flag indicating whether the pie chart is circular, or stretched into an elliptical shape.
Returns:
a flag indicating whether the pie chart is circular.

setCircular

public void setCircular(boolean flag)
A flag indicating whether the pie chart is circular, or stretched into an elliptical shape.
Parameters:
flag - the new value.

setCircularAttribute

protected void setCircularAttribute(boolean circular)
Sets the circular attribute, with no side effects.
Parameters:
circular - the new value of the flag.

getRadiusPercent

public double getRadiusPercent()
Returns the radius percentage.
Returns:
the radius percentage.

setRadiusPercent

public void setRadiusPercent(double percent)
Sets the radius percentage.
Parameters:
percent - the new value.

getExplodePercent

public double getExplodePercent(int section)
Returns the amount that a section should be 'exploded'.
Parameters:
section - the section number.
Returns:
the amount that a section should be 'exploded'.

setExplodePercent

public void setExplodePercent(int section,
                              double percent)
Sets the amount that a pie section should be exploded.
Parameters:
section - the section index.
percent - the amount to explode the section as a percentage.

getSectionLabelType

public int getSectionLabelType()
Returns the section label type. Defined by the constants: NO_LABELS, NAME_LABELS, PERCENT_LABELS and NAME_AND_PERCENT_LABELS.
Returns:
the section label type.

setSectionLabelType

public void setSectionLabelType(int type)
Sets the section label type.

Valid types are defined by the following constants: NO_LABELS, NAME_LABELS, VALUE_LABELS, PERCENT_LABELS, NAME_AND_VALUE_LABELS, NAME_AND_PERCENT_LABELS, VALUE_AND_PERCENT_LABELS.

Parameters:
type - the type.

getSectionLabelFont

public java.awt.Font getSectionLabelFont()
Returns the section label font.
Returns:
the section label font.

setSectionLabelFont

public void setSectionLabelFont(java.awt.Font font)
Sets the section label font.

Notifies registered listeners that the plot has been changed.

Parameters:
font - the new section label font.

getSectionLabelPaint

public java.awt.Paint getSectionLabelPaint()
Returns the section label paint.
Returns:
the section label paint.

setSectionLabelPaint

public void setSectionLabelPaint(java.awt.Paint paint)
Sets the section label paint.

Notifies registered listeners that the plot has been changed.

Parameters:
paint - the new section label paint.

getSectionLabelGapPercent

public double getSectionLabelGapPercent()
Returns the section label gap, measured as a percentage of the radius.
Returns:
the section label gap, measured as a percentage of the radius.

setSectionLabelGapPercent

public void setSectionLabelGapPercent(double percent)
Sets the section label gap percent.
Parameters:
percent - the gap.

setValueFormatString

public void setValueFormatString(java.lang.String format)
Sets the format string for the value labels.
Parameters:
format - The format.

setValueFormat

public void setValueFormat(java.text.NumberFormat format)
Sets the format for the value labels.
Parameters:
format - the format.

setPercentFormatString

public void setPercentFormatString(java.lang.String format)
Sets the format string for the percent labels.
Parameters:
format - the format.

setPercentFormat

public void setPercentFormat(java.text.NumberFormat format)
Sets the format for the value labels.
Parameters:
format - the format.

getPieDataset

public PieDataset getPieDataset()
Returns the dataset for the plot, cast as a PieDataset.

Provided for convenience.

Returns:
the dataset for the plot, cast as a PieDataset.

getShowSeriesLabels

public boolean getShowSeriesLabels()
Returns the show series labels flag.
Returns:
the show series label flag.

setShowSeriesLabels

public void setShowSeriesLabels(boolean flag)
Sets the show series labels flag.

Notifies registered listeners that the plot has been changed.

Parameters:
flag - the new show series labels flag.

getSeriesLabelFont

public java.awt.Font getSeriesLabelFont()
Returns the series label font.
Returns:
the series label font.

setSeriesLabelFont

public void setSeriesLabelFont(java.awt.Font font)
Sets the series label font.

Notifies registered listeners that the plot has been changed.

Parameters:
font - the new series label font.

getSeriesLabelPaint

public java.awt.Paint getSeriesLabelPaint()
Returns the series label paint.
Returns:
the series label paint.

setSeriesLabelPaint

public void setSeriesLabelPaint(java.awt.Paint paint)
Sets the series label paint.

Notifies registered listeners that the plot has been changed.

Parameters:
paint - the new series label paint.      

getCategories

public java.util.Collection getCategories()
Returns a collection of the categories in the dataset.
Returns:
the categories.

getLegendItemLabels

public java.util.List getLegendItemLabels()
Deprecated. use getLegendItems().

Returns a list of labels for the legend.
Overrides:
getLegendItemLabels in class Plot
Returns:
a list of labels.

getLegendItems

public LegendItemCollection getLegendItems()
Returns a collection of legend items for the pie chart.
Overrides:
getLegendItems in class Plot
Returns:
the legend items.

getToolTipGenerator

public PieToolTipGenerator getToolTipGenerator()
Returns the tooltip generator (possibly null).
Returns:
the tool tip generator.

setToolTipGenerator

public void setToolTipGenerator(PieToolTipGenerator generator)
Sets the tool tip generator.

If you set the generator to null, no tool tips will be generated for the pie chart.

Parameters:
generator - the new tooltip generator (null permitted).

getURLGenerator

public PieURLGenerator getURLGenerator()
Returns the URL generator (possibly null).
Returns:
the URL generator.

setURLGenerator

public void setURLGenerator(PieURLGenerator generator)
Sets the URL generator.
Parameters:
generator - the new URL generator (null permitted).

draw

public void draw(java.awt.Graphics2D g2,
                 java.awt.geom.Rectangle2D plotArea,
                 ChartRenderingInfo info)
Draws the plot on a Java 2D graphics device (such as the screen or a printer).
Overrides:
draw in class Plot
Parameters:
g2 - the graphics device.
plotArea - the area within which the plot should be drawn.
info - collects info about the drawing.

drawLabel

protected void drawLabel(java.awt.Graphics2D g2,
                         java.awt.geom.Rectangle2D pieArea,
                         java.awt.geom.Rectangle2D explodedPieArea,
                         PieDataset data,
                         double value,
                         int section,
                         double startAngle,
                         double extent)
Draws the label for one pie section. You can set the plot up for different types of labels using the setSectionLabelType() method.
Parameters:
g2 - the graphics device.
pieArea - the area for the unexploded pie sections.
explodedPieArea - the area for the exploded pie section.
data - the data for the plot.
value - the value of the label.
section - the section (zero-based index).
startAngle - the starting angle.
extent - the extent of the arc.

getPlotType

public java.lang.String getPlotType()
Returns a short string describing the type of plot.
Overrides:
getPlotType in class Plot
Returns:
the plot type.

zoom

public void zoom(double percent)
A zoom method that does nothing.

Plots are required to support the zoom operation. In the case of a pie chart, it doesn't make sense to zoom in or out, so the method is empty.

Overrides:
zoom in class Plot
Parameters:
percent - the zoom percentage.

getArcBounds

protected java.awt.geom.Rectangle2D getArcBounds(java.awt.geom.Rectangle2D unexploded,
                                                 java.awt.geom.Rectangle2D exploded,
                                                 double startAngle,
                                                 double extent,
                                                 double explodePercent)
Returns a rectangle that can be used to create a pie section (taking into account the amount by which the pie section is 'exploded').
Parameters:
unexploded - the area inside which the unexploded pie sections are drawn.
exploded - the area inside which the exploded pie sections are drawn.
startAngle - the start angle.
extent - the extent of the arc.
explodePercent - the amount by which the pie section is exploded.
Returns:
a rectangle that can be used to create a pie section.

calculateLabelLocation

protected java.awt.geom.Point2D calculateLabelLocation(java.awt.geom.Rectangle2D labelBounds,
                                                       double ascent,
                                                       java.awt.geom.Rectangle2D unexploded,
                                                       java.awt.geom.Rectangle2D exploded,
                                                       double startAngle,
                                                       double extent,
                                                       double explodePercent)
Returns the location for a label, taking into account whether or not the pie section is exploded.
Parameters:
labelBounds - the label bounds.
ascent - the ascent.
unexploded - the area within which the unexploded pie sections are drawn.
exploded - the area within which the exploded pie sections are drawn.
startAngle - the start angle for the pie section.
extent - the extent of the arc.
explodePercent - the amount by which the pie section is exploded.
Returns:
the location for a label.