com.objectplanet.chart
Class PieChart

java.lang.Object
  |
  +--java.awt.Component
        |
        +--com.objectplanet.chart.Chart
              |
              +--com.objectplanet.chart.PieChart
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.ItemSelectable, java.awt.MenuContainer, java.lang.Runnable, java.io.Serializable

public class PieChart
extends Chart

This pie chart component enables you to add a pie chart to your java applications with just a few lines of code.

A simple pie chart can be created with the following code:

 PieChart chart = new PieChart(5);
 long[] values = new long[] {100, 200, 300, 400, 500};
 chart.setSampleValues(values);
A chart with the legend turned on and displayed in 3D can be created with the following code:
 PieChart chart = new PieChart(5);
 long[] values = new long[] {100, 200, 300, 400, 500};
 chart.setSampleValues(values);
 chart.setLegendOn(true);
 chart.set3DModeOn(true);

See here for a list of parameters to set for the pie chart.

Author:
Bjorn J. Kvande.
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
static int SELECTION_STYLE_CIRCLE
          Used to mark a selected pie segment with a indented circle
static int SELECTION_STYLE_DETACHED
          Used to mark a selected pie segment as detached from the pie
static int SELECTION_STYLE_TRIANGLE
          Used to mark a selected pie segment with a triangle
 
Fields inherited from class com.objectplanet.chart.Chart
BELOW, BELOW_AND_FLOATING, BOTTOM, chartData, FLOATING, HORIZONTAL, INSIDE, LEFT, OUTSIDE, overlayCharts, RIGHT, STATIC, TARGET_LINE_ID_AND_VALUE_LABEL, TARGET_LINE_ID_LABEL, TARGET_LINE_NO_LABEL, TARGET_LINE_VALUE_LABEL, TOP, VERTICAL, visibleSamples
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
PieChart()
          Creates a pie chart with 1 sample.
PieChart(int sampleCount)
          Creates a pie chart with the given number of pie segments.
PieChart(int seriesCount, int sampleCount)
          Creates a pie chart with the given number of series and samples.
 
Method Summary
protected  void calculateChartData(java.awt.Rectangle grid, java.awt.Rectangle dataBounds)
          Calculates the chart data.
protected  void checkDataIntegrity()
          Makes sure the linechart specific data has the same number of series and samples as the chart data has.
 com.objectplanet.chart.ChartSample checkSelection(java.awt.Point point)
          Checks if a pie segment or legend label was selected.
 int getAngle()
          Gets the angle of the pie chart.
 float getDepth()
          Gets the depth of the pie.
 double getDetachedDistance()
          Gets the default detach distance.
 double getDetachedSlice(int serie, int index)
          Gets the distance a pie slice is detached from the given pie.
 java.awt.Rectangle getGraphBounds()
          Calculates the bounding rectangle of the graph itself, based on which features are on and how they are configured.
 java.awt.Color getInsideLabelColor(int index)
          Gets the color of the inside labels of the specified pie.
 int getPercentDecimalCount()
          Gets the number of decimals used with the percent values.
 int getPercentLabelStyle()
          Gets the style of the percent labels.
 double getPercentValue(int serie, int index)
          Gets the value of the given pie segment as a percentage value (out of 100).
 int getSampleLabelStyle()
          Gets the style of the sample labels.
 int getSelectionStyle()
          Gets the current selection style.
 int getSeriesLabelStyle()
          Gets the style of the series labels.
 java.awt.Color getSliceSeperatorColor()
          Gets the current pie slice seperator line color.
 int getValueLabelStyle()
          Gets the style of the value labels.
 boolean isPercentLabelsOn()
          Checks if the percent labels are turned on.
 boolean isPieLabelsOn()
          Checks if the pie lablels are turned on.
 boolean isSampleLabelsOn()
          Checks if the sample label is turned on.
 boolean isSeriesLabelsOn()
          Checks if the series label is turned on.
 boolean isSliceSeperatorOn()
          Checks if the pie slice seperators are on.
 void render(java.awt.Graphics g)
          Renders the pie chart according to the current chart attributes and GUI state values.
protected  void renderData(java.awt.Graphics g, java.awt.Rectangle grid, java.awt.Rectangle dataBounds)
          Renders the pies for the chart.
 void reset()
          Resets the chart data and features.
 void setAngle(int angle)
          Sets the angle of the pie chart.
 void setDepth(double depth)
          Sets the depth of the pie.
 void setDetachedDistance(double distance)
          Sets the default detach distance when detached selection style is set.
 void setDetachedSlice(int serie, int index, double distance)
          Detaches a pie slice.
 void setFont(java.lang.String label, java.awt.Font font)
          Sets the font for the title, legend, pie labels, sample labels, percent labels, or value labels.
 void setInsideLabelColor(int index, java.awt.Color color)
          Sets the color of the inside label for the specified pie.
 void setPercentDecimalCount(int count)
          Sets the number of decimals to use with the percent values.
 void setPercentLabelsOn(boolean on)
          Display percent labels when the mouse pointer is above the pie segment.
 void setPercentLabelStyle(int style)
          Sets the style of the percent labels.
 void setPieLabelsOn(boolean on)
          Use this to display the sample labels below each pie when multiple data series (multiple pies) are used.
 void setSampleLabelsOn(boolean on)
          Display the sample label when the mouse pointer is above the pie segment.
 void setSampleLabelStyle(int style)
          Sets the style of the sample labels.
 void setSelectionStyle(int style)
          Sets the selection style.
 void setSeriesLabelsOn(boolean on)
          Display the series label when the mouse pointer is above the pie segment.
 void setSeriesLabelStyle(int style)
          Sets the style of the series labels.
 void setSliceSeperatorColor(java.awt.Color color)
          Sets the pie slice seperator line color.
 void setSliceSeperatorOn(boolean state)
          Turns on or off the pie slice seperator lines.
 void setValueLabelStyle(int style)
          Sets the style of the value labels.
 
Methods inherited from class com.objectplanet.chart.Chart
addImage, addItemListener, addOverlayChart, appendSample, appendSampleLabel, appendSampleValue, autoRepaint, createImage, forceRepaint, formatNumber, get3DDepth, getChartBackground, getChartData, getChartForeground, getCurrentLowerRange, getCurrentRange, getDataBounds, getFont, getGraphInsets, getGridLine, getGridLineColor, getGridLineColors, getGridLines, getImage, getImage, getLabel, getLabelAngle, getLastSelectedSample, getLastSelectedSeries, getLegendColor, getLegendColors, getLegendImage, getLegendLabels, getLegendPosition, getLowerRange, getMaxValue, getMaxValueLineCount, getMinimumSize, getMinValue, getOverlayChart, getPreferredSize, getRange, getRangeAdjusted, getRangeAdjusterPosition, getRangeColor, getRangeDecimalCount, getRangePosition, getSample, getSample, getSampleColor, getSampleColors, getSampleCount, getSampleDecimalCount, getSampleLabel, getSampleLabelColor, getSampleLabels, getSampleLabelSelectionColor, getSamples, getSampleValue, getSampleValues, getSelectedObjects, getSeriesCount, getSeriesLabel, getSeriesLabelColor, getSeriesLabels, getSeriesRange, getSize, getTargetValueLine, getThousandsDelimeter, getTitle, getValueLinesColor, getVersion, is3DModeOn, isAutomaticRepaintOn, isDefaultGridLinesOn, isFloatingOnLegendOn, isGridAdjustmentOn, isLegendOn, isOverlayChartOn, isPrintAsBitmap, isRangeAdjusterOn, isRangeLabelsOn, isRangeOn, isSampleScrollerOn, isSelected, isServletModeOn, isTitleOn, isValueLabelsOn, isValueLabelsOn, isValueLinesOn, loadImage, main, paint, print, processEvent, removeExternalGraphics, removeItemListener, removeOverlayChart, run, set3DDepth, set3DModeOn, setAutomaticRepaintOn, setBackground, setChartBackground, setChartData, setChartForeground, setCurrentLowerRange, setCurrentRange, setDefaultGridLinesColor, setDefaultGridLinesOn, setDefaultGridLinesOn, setExternalGraphics, setFloatingOnLegendOn, setForeground, setGraphInsets, setGridAdjustmentOn, setGridLine, setGridLineColor, setGridLineColors, setGridLines, setGridLinesColor, setLabel, setLabelAngle, setLabelAngleCacheSize, setLegendColor, setLegendColors, setLegendImage, setLegendLabels, setLegendOn, setLegendPosition, setLowerRange, setLowerRelativeRange, setLowerRelativeRange, setLowerRelativeRange, setMaxValueLineCount, setOverlayChart, setOverlayChartOn, setPreferredSize, setPrintAsBitmap, setRange, setRangeAdjusted, setRangeAdjusterOn, setRangeAdjusterPosition, setRangeColor, setRangeDecimalCount, setRangeLabelsOn, setRangeOn, setRangePosition, setRelativeRange, setRelativeRange, setRelativeRange, setSample, setSampleAxisRange, setSampleColor, setSampleColors, setSampleCount, setSampleDecimalCount, setSampleLabel, setSampleLabelColor, setSampleLabels, setSampleLabelSelectionColor, setSamples, setSampleScrollerOn, setSampleValue, setSampleValues, setSelection, setSelection, setSeriesCount, setSeriesLabel, setSeriesLabelColor, setSeriesLabels, setSeriesRange, setServletModeOn, setTargetValueLine, setThousandsDelimeter, setTitle, setTitleOn, setValueLabelsOn, setValueLabelsOn, setValueLinesColor, setValueLinesOn, setVisibleSamples, toString, update
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SELECTION_STYLE_CIRCLE

public static final int SELECTION_STYLE_CIRCLE
Used to mark a selected pie segment with a indented circle

See Also:
Constant Field Values

SELECTION_STYLE_DETACHED

public static final int SELECTION_STYLE_DETACHED
Used to mark a selected pie segment as detached from the pie

See Also:
Constant Field Values

SELECTION_STYLE_TRIANGLE

public static final int SELECTION_STYLE_TRIANGLE
Used to mark a selected pie segment with a triangle

See Also:
Constant Field Values
Constructor Detail

PieChart

public PieChart()
Creates a pie chart with 1 sample.


PieChart

public PieChart(int sampleCount)
Creates a pie chart with the given number of pie segments.

Parameters:
sampleCount - The number of pie segments for the chart.

PieChart

public PieChart(int seriesCount,
                int sampleCount)
Creates a pie chart with the given number of series and samples.

Parameters:
seriesCount - The number of pies.
sampleCount - The number of segments in each pie.
Method Detail

calculateChartData

protected void calculateChartData(java.awt.Rectangle grid,
                                  java.awt.Rectangle dataBounds)
Calculates the chart data.

Specified by:
calculateChartData in class Chart
Parameters:
grid - The chart grid bounds to paint data within.
dataBounds - The scrolled bounds to calculate the data within.

checkDataIntegrity

protected void checkDataIntegrity()
Makes sure the linechart specific data has the same number of series and samples as the chart data has.

Overrides:
checkDataIntegrity in class Chart

checkSelection

public com.objectplanet.chart.ChartSample checkSelection(java.awt.Point point)
Checks if a pie segment or legend label was selected.

Overrides:
checkSelection in class Chart
Parameters:
point - The point clicked on.
Returns:
The pie segment selected, or null if no segment was selected.

getAngle

public int getAngle()
Gets the angle of the pie chart.

Returns:
The angle in degrees (0-90).

getDepth

public float getDepth()
Gets the depth of the pie.

Returns:
a float in the range 0.0-1.0.

getDetachedDistance

public double getDetachedDistance()
Gets the default detach distance.

See Also:
setDetachedDistance(double)

getDetachedSlice

public double getDetachedSlice(int serie,
                               int index)
Gets the distance a pie slice is detached from the given pie.

Parameters:
serie - The series index of the slice.
index - The sample index of the slice.
Returns:
The distance as a factor of the pie size, 0 if not detached.

getGraphBounds

public java.awt.Rectangle getGraphBounds()
Calculates the bounding rectangle of the graph itself, based on which features are on and how they are configured.

Specified by:
getGraphBounds in class Chart

getInsideLabelColor

public java.awt.Color getInsideLabelColor(int index)
Gets the color of the inside labels of the specified pie. Use -1 as parameter to get default inside label color.

Parameters:
index - The index of the pie (0-based).
Returns:
The color, or null if no color is set.
Throws:
java.lang.IllegalArgumentException - if the pie index is invalid.

getPercentDecimalCount

public int getPercentDecimalCount()
Gets the number of decimals used with the percent values.


getPercentLabelStyle

public int getPercentLabelStyle()
Gets the style of the percent labels.

Returns:
FLOATING or INSIDE.
See Also:
setPercentLabelStyle(int)

getPercentValue

public double getPercentValue(int serie,
                              int index)
Gets the value of the given pie segment as a percentage value (out of 100).

Parameters:
serie - The index of the serie (0-based).
index - The index of the pie segment (0-based).
Returns:
The number of percent the specified segment occupies.
Throws:
java.lang.IllegalArgumentException - If the index or series is invalid.

getSampleLabelStyle

public int getSampleLabelStyle()
Gets the style of the sample labels.

Overrides:
getSampleLabelStyle in class Chart
Returns:
FLOATING or INSIDE.
See Also:
setSampleLabelStyle(int)

getSelectionStyle

public int getSelectionStyle()
Gets the current selection style.

Returns:
SELECTION_STYLE_CIRCLE, SELECTION_STYLE_TRIANGLE, SELECTION_STYLE_DETACHED.
See Also:
setSelectionStyle(int)

getSeriesLabelStyle

public int getSeriesLabelStyle()
Gets the style of the series labels.

Overrides:
getSeriesLabelStyle in class Chart
Returns:
FLOATING or INSIDE.
See Also:
setSeriesLabelStyle(int)

getSliceSeperatorColor

public java.awt.Color getSliceSeperatorColor()
Gets the current pie slice seperator line color.

Returns:
The color, or null if no color is set.

getValueLabelStyle

public int getValueLabelStyle()
Gets the style of the value labels.

Returns:
FLOATING or INSIDE.
See Also:
setValueLabelStyle(int)

isPercentLabelsOn

public boolean isPercentLabelsOn()
Checks if the percent labels are turned on.

Returns:
True if on, false if off.

isPieLabelsOn

public boolean isPieLabelsOn()
Checks if the pie lablels are turned on.

Returns:
True for on, false for off.
See Also:
setPieLabelsOn(boolean)

isSampleLabelsOn

public boolean isSampleLabelsOn()
Checks if the sample label is turned on.

Overrides:
isSampleLabelsOn in class Chart
Returns:
True if on, false if off.

isSeriesLabelsOn

public boolean isSeriesLabelsOn()
Checks if the series label is turned on.

Overrides:
isSeriesLabelsOn in class Chart
Returns:
True if on, false if off.

isSliceSeperatorOn

public boolean isSliceSeperatorOn()
Checks if the pie slice seperators are on.

Returns:
True for on, false for off.
See Also:
setSliceSeperatorOn(boolean)

render

public void render(java.awt.Graphics g)
Renders the pie chart according to the current chart attributes and GUI state values.

Specified by:
render in class Chart
Parameters:
g - The graphics context used to paint with.
See Also:
Chart.paint(java.awt.Graphics)

renderData

protected void renderData(java.awt.Graphics g,
                          java.awt.Rectangle grid,
                          java.awt.Rectangle dataBounds)
Renders the pies for the chart.

Specified by:
renderData in class Chart
Parameters:
g - The graphics context used to paint with.
grid - The chart grid bounds to paint data within.
dataBounds - The scrolled bounds to calculate the data within.

reset

public void reset()
Resets the chart data and features.

Overrides:
reset in class Chart

setAngle

public void setAngle(int angle)
Sets the angle of the pie chart. The default angle is 20.

Parameters:
angle - (0-80).
See Also:
setDepth(double)

setDepth

public void setDepth(double depth)
Sets the depth of the pie. The depth is a fraction of the width of the pie. If the depth is set to 1.0, the depth is the same as the width. The default depth is set to 0.4.

Parameters:
depth - The depth (0.0-1.0).
See Also:
setAngle(int)

setDetachedDistance

public void setDetachedDistance(double distance)
Sets the default detach distance when detached selection style is set. The distance is relative to the radius of the pie. A distance of 0.5 will detach the slice halfway out the pie.

Parameters:
distance - The distance to use.
See Also:
setDetachedSlice(int, int, double)

setDetachedSlice

public void setDetachedSlice(int serie,
                             int index,
                             double distance)
Detaches a pie slice. The distance parameter is a factor of the pie radius. If the distance is set to 0.5 the slice will be detached halfway out from the pie. To reattach a slice, set the distance to 0.

Parameters:
serie - The series index of the slice.
index - The sample index of the slice.
distance - The distance as a factor of the pie size.

setFont

public void setFont(java.lang.String label,
                    java.awt.Font font)
Sets the font for the title, legend, pie labels, sample labels, percent labels, or value labels.

Overrides:
setFont in class Chart
Parameters:
label - "titleFont", "legendFont", "pieLabelFont", "floatingLabelFont".
font - The font to set for the specified label.

setInsideLabelColor

public void setInsideLabelColor(int index,
                                java.awt.Color color)
Sets the color of the inside label for the specified pie. Tos set the color for all pies if index is -1.

Parameters:
index - The index of the pie (0-based).
color - The color to use, null for the default color.
Throws:
java.lang.IllegalArgumentException - if the pie index is invalid.

setPercentDecimalCount

public void setPercentDecimalCount(int count)
Sets the number of decimals to use with the percent values. The default is 0 decimals.

Parameters:
count - The number of decimals to use.

setPercentLabelsOn

public void setPercentLabelsOn(boolean on)
Display percent labels when the mouse pointer is above the pie segment. The label shows the percent the pie segment occupies of the whole pie. If the value labels are turned on both of them will be displayed.

Parameters:
on - True for on, false for off.
See Also:
Chart.setValueLabelsOn(boolean)

setPercentLabelStyle

public void setPercentLabelStyle(int style)
Sets the style of the percent labels. The percent labels can be displayed floating over the slice when the mouse is over it or statically inside each slice. The default style is floating.

Parameters:
style - FLOATING or INSIDE.

setPieLabelsOn

public void setPieLabelsOn(boolean on)
Use this to display the sample labels below each pie when multiple data series (multiple pies) are used.

Parameters:
on - True for on, false for off (default).

setSampleLabelsOn

public void setSampleLabelsOn(boolean on)
Display the sample label when the mouse pointer is above the pie segment.

Overrides:
setSampleLabelsOn in class Chart
Parameters:
on - True for on, false for off (default).

setSampleLabelStyle

public void setSampleLabelStyle(int style)
Sets the style of the sample labels. The sample labels can be displayed floating over the slice when the mouse is over it or statically inside each slice. The default style is floating.

Overrides:
setSampleLabelStyle in class Chart
Parameters:
style - FLOATING or INSIDE.

setSelectionStyle

public void setSelectionStyle(int style)
Sets the selection style. A selection marker is painted when the user selects a pie slice. The selection marker can either be a circle in the slice, a triangle in the slice, or the whole slice can be detached when selected.

Parameters:
style - SELECTION_STYLE_CIRCLE, SELECTION_STYLE_TRIANGLE, SELECTION_STYLE_DETACHED.

setSeriesLabelsOn

public void setSeriesLabelsOn(boolean on)
Display the series label when the mouse pointer is above the pie segment.

Overrides:
setSeriesLabelsOn in class Chart
Parameters:
on - True for on, false for off (default).

setSeriesLabelStyle

public void setSeriesLabelStyle(int style)
Sets the style of the series labels. The series labels can be displayed floating over the slice when the mouse is over it or statically inside each slice. The default style is floating.

Overrides:
setSeriesLabelStyle in class Chart
Parameters:
style - FLOATING or INSIDE.

setSliceSeperatorColor

public void setSliceSeperatorColor(java.awt.Color color)
Sets the pie slice seperator line color. If the color set is null, the color defaults to be a little darker than the pie segment itself.

Parameters:
color - The color to use.

setSliceSeperatorOn

public void setSliceSeperatorOn(boolean state)
Turns on or off the pie slice seperator lines. When this is on, a line will be painted between each slice to seperate them. By default the color of the slice seperator line is the same color as the slice, only darker. Use setSliceSeperatorColor(Color) to control the seperator line color.

Parameters:
state - True for on, false for off.

setValueLabelStyle

public void setValueLabelStyle(int style)
Sets the style of the value labels. The value labels can be displayed floating over the slice when the mouse is over it or inside each slice. The default style is floating.

Parameters:
style - FLOATING or INSIDE.