com.objectplanet.chart
Class BarChart

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

public class BarChart
extends Chart

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

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

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

See here for a list of parameters to set for the bar 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 SIDE_BY_SIDE_BARS
          Use side-by-side bars in multiple series charts; setBarType()
static int STACKED_BARS
          Use stacked bars in multiple series charts; setBarType()
 
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
BarChart()
          Creates a bar chart with 1 series, 1 sample and a range of 100.
BarChart(int count)
          Creates a bar chart with the given number of samples and default range.
BarChart(int count, double range)
          Creates a bar chart with the given number of bars and range.
BarChart(int sampleCount, double range, double lowerRange)
          Creates a bar chart with the given number of bars and range.
BarChart(int seriesCount, int sampleCount, double range, double lowerRange)
          Creates a bar chart with the given number of series and bars and range.
 
Method Summary
protected  void calculateChartData(java.awt.Rectangle grid, java.awt.Rectangle dataBounds)
          Calculates the position and size of each bar in the chart.
 com.objectplanet.chart.ChartSample checkSelection(java.awt.Point point)
          This method is called when the user releases the mouse and checks if the given point is inside a bar or a legend label.
 java.lang.String[] getBarLabels()
          Gets the bar labels.
 int getBarLabelStyle()
          Gets the current bar label style.
 int getBarType()
          Gets the current bar type in multiple series charts.
 double getBarWidth()
          Gets the relative width of each bar.
 java.awt.Rectangle getGraphBounds()
          Calculates the bounding rectangle of the graph itself, based on which features are on and how they are configured.
 int getgridAlignment()
          Gets the bar alignment.
 java.lang.String[] getLegendLabels()
          Gets the legend labels.
 int getValueLabelStyle()
          Gets the current value label style.
 int getValuePosition(double value)
          Gets the position in the chart of the specified value.
 boolean isAutoLabelSpacingOn()
          Gets the state of the automatic label spacing.
 boolean isBarLabelsOn()
          Checks if the bar labels are turned on.
 boolean isBarOutlineOn()
          Checks if the bar outline is turned on or off.
 boolean isMultiColorOn()
          Checks if the multicolor mode is turned on.
 boolean isMultiSeriesOn()
          Checks if the chart is in multi series mode.
 void render(java.awt.Graphics g)
          Renders the bar chart according to the current chart attributes and which charts features are turned on or off.
protected  void renderData(java.awt.Graphics g, java.awt.Rectangle grid, java.awt.Rectangle dataBounds)
          Renders the bars and the value labels.
 void reset()
          Resets the chart data and features.
 void setAutoLabelSpacingOn(boolean on)
          Turns on or off automatic bar label spacing.
 void setBarAlignment(int alignment)
          Sets the bar alignment.
 void setBarLabels(java.lang.String[] labels)
          Sets the bar labels.
 void setBarLabelsOn(boolean on)
          Turns on or off the bar labels.
 void setBarLabelStyle(int style)
          Sets the bar label style.
 void setBarOutlineColor(java.awt.Color color)
          Sets the color used for the bar outline.
 void setBarOutlineOn(boolean on)
          Turns on or off the bar outline.
 void setBarType(int type)
          Sets the bar type in multiple series charts.
 void setBarWidth(double width)
          Sets the relative width of each bar.
 void setMultiColorOn(boolean on)
          Turns on or off multi-color bar display.
 void setMultiSeriesOn(boolean state)
          Makes the chart behave like it does when it has multiple data series, even if it has only one.
 void setValueLabelStyle(int style)
          Sets the value label style.
 
Methods inherited from class com.objectplanet.chart.Chart
addImage, addItemListener, addOverlayChart, appendSample, appendSampleLabel, appendSampleValue, autoRepaint, checkDataIntegrity, 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, getLegendPosition, getLowerRange, getMaxValue, getMaxValueLineCount, getMinimumSize, getMinValue, getOverlayChart, getPreferredSize, getRange, getRangeAdjusted, getRangeAdjusterPosition, getRangeColor, getRangeDecimalCount, getRangePosition, getSample, getSample, getSampleColor, getSampleColors, getSampleCount, getSampleDecimalCount, getSampleLabel, getSampleLabelColor, getSampleLabels, getSampleLabelSelectionColor, getSampleLabelStyle, getSamples, getSampleValue, getSampleValues, getSelectedObjects, getSeriesCount, getSeriesLabel, getSeriesLabelColor, getSeriesLabels, getSeriesLabelStyle, getSeriesRange, getSize, getTargetValueLine, getThousandsDelimeter, getTitle, getValueLinesColor, getVersion, is3DModeOn, isAutomaticRepaintOn, isDefaultGridLinesOn, isFloatingOnLegendOn, isGridAdjustmentOn, isLegendOn, isOverlayChartOn, isPrintAsBitmap, isRangeAdjusterOn, isRangeLabelsOn, isRangeOn, isSampleLabelsOn, isSampleScrollerOn, isSelected, isSeriesLabelsOn, 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, setFont, 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, setSampleLabelsOn, setSampleLabelStyle, setSamples, setSampleScrollerOn, setSampleValue, setSampleValues, setSelection, setSelection, setSeriesCount, setSeriesLabel, setSeriesLabelColor, setSeriesLabels, setSeriesLabelsOn, setSeriesLabelStyle, 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

SIDE_BY_SIDE_BARS

public static final int SIDE_BY_SIDE_BARS
Use side-by-side bars in multiple series charts; setBarType()

See Also:
Constant Field Values

STACKED_BARS

public static final int STACKED_BARS
Use stacked bars in multiple series charts; setBarType()

See Also:
Constant Field Values
Constructor Detail

BarChart

public BarChart()
Creates a bar chart with 1 series, 1 sample and a range of 100.


BarChart

public BarChart(int count)
Creates a bar chart with the given number of samples and default range. The default range is 100.

Parameters:
count - The number of bars for the chart.

BarChart

public BarChart(int count,
                double range)
Creates a bar chart with the given number of bars and range.

Parameters:
count - The number of bars for the chart.
range - The initial range of the chart.

BarChart

public BarChart(int sampleCount,
                double range,
                double lowerRange)
Creates a bar chart with the given number of bars and range.

Parameters:
sampleCount - The number of bars for the chart.
range - The initial upper range of the chart.
lowerRange - The initial lower range of the chart.

BarChart

public BarChart(int seriesCount,
                int sampleCount,
                double range,
                double lowerRange)
Creates a bar chart with the given number of series and bars and range.

Parameters:
seriesCount - The number of data series in the chart.
sampleCount - The number of sample in the chart.
range - The initial upper range of the chart.
lowerRange - The initial lower range of the chart.
Method Detail

calculateChartData

protected void calculateChartData(java.awt.Rectangle grid,
                                  java.awt.Rectangle dataBounds)
Calculates the position and size of each bar in the chart.

Specified by:
calculateChartData in class Chart
Parameters:
grid - The chart grid bounds.
dataBounds - The data bounds.

checkSelection

public com.objectplanet.chart.ChartSample checkSelection(java.awt.Point point)
This method is called when the user releases the mouse and checks if the given point is inside a bar or a legend label.

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

getBarLabels

public java.lang.String[] getBarLabels()
Gets the bar labels. The array returned is the original, so don't change anything in it if you don't know what you are doing.

Returns:
An array with the labels.

getBarLabelStyle

public int getBarLabelStyle()
Gets the current bar label style.

Returns:
BELOW, FLOATING, BELOW_AND_FLOATING.
See Also:
setBarLabelStyle(int)

getBarType

public int getBarType()
Gets the current bar type in multiple series charts.

Returns:
STACKED_BARS or SIDE_BY_SIDE_BARS.

getBarWidth

public double getBarWidth()
Gets the relative width of each bar.

Returns:
The width as a fraction between 0.0 and 1.0.
See Also:
setBarWidth(double)

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

getgridAlignment

public int getgridAlignment()
Gets the bar alignment.

Returns:
HORIZONTAL or VERTICAL.
See Also:
setBarAlignment(int)

getLegendLabels

public java.lang.String[] getLegendLabels()
Gets the legend labels. The array returned is the original, so don't change anything in it if you don't know what you are doing.

Overrides:
getLegendLabels in class Chart
Returns:
An array with the labels.
See Also:
Chart.setLegendLabels(java.lang.String[])

getValueLabelStyle

public int getValueLabelStyle()
Gets the current value label style.

Returns:
INSIDE, OUTSIDE, FLOATING.

getValuePosition

public int getValuePosition(double value)
Gets the position in the chart of the specified value. If the alignment of the bars is VERTICAL the position represents the y-axis. If the alignment of the bars is HORIZONTAL the position represents the x-axis.

Parameters:
value - The value to find the position for.
Returns:
The pixel position.

isAutoLabelSpacingOn

public boolean isAutoLabelSpacingOn()
Gets the state of the automatic label spacing.

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

isBarLabelsOn

public boolean isBarLabelsOn()
Checks if the bar labels are turned on.

Returns:
True for on, false for off.

isBarOutlineOn

public boolean isBarOutlineOn()
Checks if the bar outline is turned on or off. The bar outline is the frame drawn around each bar.

Returns:
True if on, false for off.

isMultiColorOn

public boolean isMultiColorOn()
Checks if the multicolor mode is turned on.

Returns:
True if multicolor, false otherwise.

isMultiSeriesOn

public boolean isMultiSeriesOn()
Checks if the chart is in multi series mode.

Returns:
True for multi series, false otherwise.
See Also:
setMultiSeriesOn(boolean)

render

public void render(java.awt.Graphics g)
Renders the bar chart according to the current chart attributes and which charts features are turned on or off.

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 bars and the value labels.

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

reset

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

Overrides:
reset in class Chart

setAutoLabelSpacingOn

public void setAutoLabelSpacingOn(boolean on)
Turns on or off automatic bar label spacing. If this is on, only bar labels there is space for will be painted. If turned off, all labels will be painted, even if they overlap. It is turned off by default.

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

setBarAlignment

public void setBarAlignment(int alignment)
Sets the bar alignment. The default alignment is vertical.

Parameters:
alignment - HORIZONTAL or VERTICAL.
Throws:
java.lang.IllegalArgumentException - If the alignment is not valid.

setBarLabels

public void setBarLabels(java.lang.String[] labels)
Sets the bar labels. By default, the bar labels are read from the sampleLabels. Setting this, overrides the sampleLabels.

Parameters:
labels - The labels, or null for default behavior.
See Also:
setBarLabelsOn(boolean), Chart.setSampleLabels(java.lang.String[])

setBarLabelsOn

public void setBarLabelsOn(boolean on)
Turns on or off the bar labels. The bar labels are the same as the labels in the legend, but are displayed at the base of the bar itself.

Parameters:
on - True if the bar labels should be displayed.
See Also:
Chart.setSampleLabel(int, java.lang.String)

setBarLabelStyle

public void setBarLabelStyle(int style)
Sets the bar label style. The bar labels can either be painted below the bar itself, or as a floating label over the bar when the mouse pointer moves over it.

Parameters:
style - BELOW, FLOATING, BELOW_AND_FLOATING.

setBarOutlineColor

public void setBarOutlineColor(java.awt.Color color)
Sets the color used for the bar outline. If color is null, chart foreground is used

Parameters:
color - The color to set.

setBarOutlineOn

public void setBarOutlineOn(boolean on)
Turns on or off the bar outline. The bar outline is the frame drawn around each bar. The bar outline is turned on by default.

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

setBarType

public void setBarType(int type)
Sets the bar type in multiple series charts.

Parameters:
type - STACKED_BARS or SIDE_BY_SIDE_BARS.
Throws:
java.lang.IllegalArgumentException - If the type is not valid.

setBarWidth

public void setBarWidth(double width)
Sets the relative width of each bar. If the width is set to 1.0, there will be no space between the bars. If the width is set to 0.5, the space between each bar will be the same as the width of the bar. If you have side-by-side bars in the chart, it is the combined width of a sample that is set.

Parameters:
width - The width fraction to use (0.0 to 1.0).

setMultiColorOn

public void setMultiColorOn(boolean on)
Turns on or off multi-color bar display.

Parameters:
on - True for multi-color, false for single-color.

setMultiSeriesOn

public void setMultiSeriesOn(boolean state)
Makes the chart behave like it does when it has multiple data series, even if it has only one. The labels in the chart will then always be the series labels, and not change to the sample labels when the number of data series changes to one.

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

setValueLabelStyle

public void setValueLabelStyle(int style)
Sets the value label style.

Parameters:
style - INSIDE, OUTSIDE, FLOATING.
Throws:
java.lang.IllegalArgumentException - If the style is invalid.