jwo.utils.gui
Class JWGraph

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by jwo.utils.gui.JWGraph
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible
Direct Known Subclasses:
BlockGraph, Histogram, LineGraph, PolarGraph, Scatterplot

public class JWGraph
extends JPanel

Simple graph object. Originally based on the code given in the tutorial Chart your way to custom graph components, by Maria Winslow, 1997 (JavaWorld, July, 1997). See www.javaworld.com/javaworld/jw-07-1997/jw-07-step.html. Modified to use Swing and additional axis labelling.

Version:
2.3, 2nd June, 2007.
Author:
Jo Wood
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JPanel
JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
protected  int bottom
          Bottom position of graph.
protected  int centreX
          X coordinate of centre of graph.
protected  int centreY
          Y coordinate of centre of graph.
static int INTERVAL
          Measurement scale of data.
protected  Vector items
          Array of graphic items in graph.
protected  int left
          Left position of graph.
static int NOMINAL
          Measurement scale of data.
protected  Image offscreenImage
          Image for off-screen double buffering.
protected  Graphics og
          Off-screen double buffering context.
static int ORDINAL
          Measurement scale of data.
static int RATIO
          Measurement scale of data.
protected  int right
          Right position of graph.
protected  int top
          Top position of graph.
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
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
JWGraph()
          Minimal advance graph layout (axes only).
JWGraph(String title, float yMin, float yMax)
          Lays out the graph title, labels etc.
JWGraph(String title, float xMin, float xMax, float yMin, float yMax)
          Lays out the graph title, labels etc.
JWGraph(String title, float rMin, float rMax, float thetaMin, float thetaMax, boolean polar)
          Lays out the graph title, labels etc as a polar plot.
JWGraph(String title, float xMin, float xMax, float yMin, float yMax, String[] labels)
          Lays out the graph title, labels etc.
JWGraph(String title, float xMin, float xMax, float yMin, float yMax, String[] labels, int measurementScale)
          Lays out the graph title, labels etc.
JWGraph(String title, float xMin, float xMax, String[] labels, float yMin, float yMax)
          Lays out the graph title, labels etc.
JWGraph(String title, float xMin, float xMax, String[] labels, float yMin, float yMax, int measurementScale)
          Lays out the graph title, labels etc.
 
Method Summary
 void addItem(float xValue, float yValue)
          Adds a graphical element to the graph.
 void addItem(float xValue, float yValue, Color col)
          Adds a (coloured) graphical element to the graph.
 void erase()
          Clears the current graph area and redraws it.
 FontMetrics getFM()
          Reports the font metric used by the graph.
 Dimension getPreferredSize()
          Reports the preferred size of the graph
 float[][] getValues()
          Reports the numeric values of the items in the graph.
 float getXMax()
          Reports the maximum X value of the graph.
 int getXMeasurementScale()
          Reports on the measurement scale of the data on the X-axis.
 float getXMin()
          Reports the minimum X value of the graph.
 float getYMax()
          Reports the maximum Y value of the graph.
 int getYMeasurementScale()
          Reports on the measurement scale of the data on the Y-axis.
 float getYMin()
          Reports the minimum Y value of the graph.
 Color makeTransparent(Color inColour)
          Modifies a given colour by the currently set opacity level.
 void paintComponent(Graphics g)
          Overrides the paint method to draw the graph components.
 void removeItem(float xValue, float yValue)
          Removes graphical element(s) from the graph (identified by x,y values).
 void removeItems()
          Removes all graphical elements from the graph.
 void roundAxes()
          Rounds the axis labels to provide more convenient display.
 void roundXAxis()
          Rounds the x axis labels to provide more convenient display.
 void roundYAxis()
          Rounds the y axis labels to provide more convenient display.
 void setAspectRatio(float aspectRatio)
          Sets the aspect ratio for the graph.
 void setOpacity(int opacity)
          Sets the opacity level for the graph.
 void setTitle(String title)
          Sets the title of the graph and updates the display.
 void setXAxis(float min, float max)
          Sets the x-axis to the given range.
 void setXAxis(float min, float max, String[] xLabels, int scale)
          Sets the x-axis to the given range using the given measurement scale.
 void setXAxisInc(float inc)
          Sets the increment value between x axis labels.
 void setXMeasurementScale(int scale)
          Sets the measurement scale of the data on the X-axis.
 void setYAxis(float min, float max)
          Sets the y-axis to the given range.
 void setYAxis(float min, float max, int scale)
          Sets the y-axis to the given range using the given measurement scale.
 void setYAxisInc(float inc)
          Sets the increment value between y axis labels.
 void setYMeasurementScale(int scale)
          Sets the measurement scale of the data on the Y-axis.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NOMINAL

public static final int NOMINAL
Measurement scale of data.

See Also:
Constant Field Values

ORDINAL

public static final int ORDINAL
Measurement scale of data.

See Also:
Constant Field Values

INTERVAL

public static final int INTERVAL
Measurement scale of data.

See Also:
Constant Field Values

RATIO

public static final int RATIO
Measurement scale of data.

See Also:
Constant Field Values

offscreenImage

protected Image offscreenImage
Image for off-screen double buffering.


og

protected Graphics og
Off-screen double buffering context.


top

protected int top
Top position of graph.


bottom

protected int bottom
Bottom position of graph.


left

protected int left
Left position of graph.


right

protected int right
Right position of graph.


centreX

protected int centreX
X coordinate of centre of graph.


centreY

protected int centreY
Y coordinate of centre of graph.


items

protected Vector items
Array of graphic items in graph.

Constructor Detail

JWGraph

public JWGraph()
Minimal advance graph layout (axes only).


JWGraph

public JWGraph(String title,
               float yMin,
               float yMax)
Lays out the graph title, labels etc. Assumes Y range is known in advance.

Parameters:
title - Title of the graph.
yMin - Minimum Y value.
yMax - Maximum Y value.

JWGraph

public JWGraph(String title,
               float xMin,
               float xMax,
               float yMin,
               float yMax)
Lays out the graph title, labels etc. Assumes both X and Y range are known in advance.

Parameters:
title - Title of the graph.
xMin - Minimum X value.
xMax - Maximum X value.
yMin - Minimum Y value.
yMax - Maximum Y value.

JWGraph

public JWGraph(String title,
               float xMin,
               float xMax,
               float yMin,
               float yMax,
               String[] labels)
Lays out the graph title, labels etc. Assumes both X and Y range are known in advance and Y axis contains nominal scale labels.

Parameters:
title - Title of the graph.
xMin - Minimum X value.
xMax - Maximum X value.
yMin - Minimum Y value.
yMax - Maximum Y value.
labels - Array holding labels for Y axis.

JWGraph

public JWGraph(String title,
               float xMin,
               float xMax,
               float yMin,
               float yMax,
               String[] labels,
               int measurementScale)
Lays out the graph title, labels etc. Assumes both X and Y range are known in advance and Y axis contains scale labels on the given measurement scale.

Parameters:
title - Title of the graph.
xMin - Minimum X value.
xMax - Maximum X value.
yMin - Minimum Y value.
yMax - Maximum Y value.
labels - Array holding labels for Y axis.
measurementScale - Measurement scale of the labelled axis.

JWGraph

public JWGraph(String title,
               float xMin,
               float xMax,
               String[] labels,
               float yMin,
               float yMax)
Lays out the graph title, labels etc. Assumes both X and Y range are known in advance and X axis contains nominal scale labels.

Parameters:
title - Title of the graph.
xMin - Minimum X value.
xMax - Maximum X value.
labels - Array holding labels of X axis.
yMin - Minimum Y value.
yMax - Maximum Y value.

JWGraph

public JWGraph(String title,
               float xMin,
               float xMax,
               String[] labels,
               float yMin,
               float yMax,
               int measurementScale)
Lays out the graph title, labels etc. Assumes both X and Y range are known in advance and X axis contains labels on the given measurement scale.

Parameters:
title - Title of the graph.
xMin - Minimum X value.
xMax - Maximum X value.
labels - Array holding labels of X axis.
yMin - Minimum Y value.
yMax - Maximum Y value.
measurementScale - Measurement scale of the labelled axis.

JWGraph

public JWGraph(String title,
               float rMin,
               float rMax,
               float thetaMin,
               float thetaMax,
               boolean polar)
Lays out the graph title, labels etc as a polar plot. Assumes both radius and angular range are known in advance.

Parameters:
title - Title of the graph.
rMin - Minimum X value.
rMax - Maximum X value.
thetaMin - Starting angular value.
thetaMax - Ending angular value.
polar - Indicates a polar plot.
Method Detail

setOpacity

public void setOpacity(int opacity)
Sets the opacity level for the graph.

Parameters:
opacity - Opacity level for items drawn on graph from 0 (invisible) to 255 (opaque).

erase

public void erase()
Clears the current graph area and redraws it.


makeTransparent

public Color makeTransparent(Color inColour)
Modifies a given colour by the currently set opacity level.

Parameters:
inColour - Colour to modify.
Returns:
Colour modified to show currently set opacity.

setAspectRatio

public void setAspectRatio(float aspectRatio)
Sets the aspect ratio for the graph. Values greater than 1 indicate larger width than height, less than 1 indicate greater height and 0 indicates no locked aspect ratio.

Parameters:
aspectRatio - Ratio of width over height or 0 for no fixed ratio.

addItem

public void addItem(float xValue,
                    float yValue)
Adds a graphical element to the graph.

Parameters:
xValue - x-value associated with item.
yValue - y-value associated with item.

addItem

public void addItem(float xValue,
                    float yValue,
                    Color col)
Adds a (coloured) graphical element to the graph.

Parameters:
xValue - x-value associated with item.
yValue - y-value associated with item.
col - Colour of item.

getValues

public float[][] getValues()
Reports the numeric values of the items in the graph.

Returns:
Array of numeric values of the items represented in the graph. First column contains x values, second column y values.
See Also:
JWGraphItem.getXValue(), JWGraphItem.getYValue()

removeItem

public void removeItem(float xValue,
                       float yValue)
Removes graphical element(s) from the graph (identified by x,y values).

Parameters:
xValue - x-value associated with item.
yValue - y-value associated with item.

removeItems

public void removeItems()
Removes all graphical elements from the graph.


getYMin

public float getYMin()
Reports the minimum Y value of the graph.

Returns:
Minimum Y value.

getYMax

public float getYMax()
Reports the maximum Y value of the graph.

Returns:
Maximum Y value.

getXMin

public float getXMin()
Reports the minimum X value of the graph.

Returns:
Minimum X value.

getXMax

public float getXMax()
Reports the maximum X value of the graph.

Returns:
Maximum X value.

getFM

public FontMetrics getFM()
Reports the font metric used by the graph.

Returns:
Font metric.

getXMeasurementScale

public int getXMeasurementScale()
Reports on the measurement scale of the data on the X-axis.

Returns:
Measurement scale (NOMINAL, ORDINAL, INTERVAL or RATIO).

getYMeasurementScale

public int getYMeasurementScale()
Reports on the measurement scale of the data on the Y-axis.

Returns:
Measurement scale (NOMINAL, ORDINAL, INTERVAL or RATIO).

setXMeasurementScale

public void setXMeasurementScale(int scale)
Sets the measurement scale of the data on the X-axis.

Parameters:
scale - Measurement scale (NOMINAL, ORDINAL, INTERVAL or RATIO).

setYMeasurementScale

public void setYMeasurementScale(int scale)
Sets the measurement scale of the data on the Y-axis.

Parameters:
scale - Measurement scale (NOMINAL, ORDINAL, INTERVAL or RATIO).

setXAxis

public void setXAxis(float min,
                     float max)
Sets the x-axis to the given range.

Parameters:
min - Minimum axis value.
max - Maximum axis value.

setXAxis

public void setXAxis(float min,
                     float max,
                     String[] xLabels,
                     int scale)
Sets the x-axis to the given range using the given measurement scale.

Parameters:
min - Minimum axis value.
max - Maximum axis value.
xLabels - Labels for x axis.
scale - Axis measurement scale.

setYAxis

public void setYAxis(float min,
                     float max)
Sets the y-axis to the given range.

Parameters:
min - Minimum axis value.
max - Maximum axis value.

setYAxis

public void setYAxis(float min,
                     float max,
                     int scale)
Sets the y-axis to the given range using the given measurement scale.

Parameters:
min - Minimum axis value.
max - Maximum axis value.
scale - Axis measurement scale.

setTitle

public void setTitle(String title)
Sets the title of the graph and updates the display.

Parameters:
title - New title to display.

roundAxes

public void roundAxes()
Rounds the axis labels to provide more convenient display. The degree of rounding will depend on the existing range of values represented by the axes.


roundXAxis

public void roundXAxis()
Rounds the x axis labels to provide more convenient display. The degree of rounding will depend on the existing range of values represented by the axis.


roundYAxis

public void roundYAxis()
Rounds the y axis labels to provide more convenient display. The degree of rounding will depend on the existing range of values represented by the axis.


setYAxisInc

public void setYAxisInc(float inc)
Sets the increment value between y axis labels. Uses the existing y minimum and maximum values to define the range.

Parameters:
inc - Increment between axis labels.

setXAxisInc

public void setXAxisInc(float inc)
Sets the increment value between x axis labels. Uses the existing x minimum and maximum values to define the range.

Parameters:
inc - Increment between axis labels.

paintComponent

public void paintComponent(Graphics g)
Overrides the paint method to draw the graph components.

Overrides:
paintComponent in class JComponent
Parameters:
g - Graphics context to paint to.

getPreferredSize

public Dimension getPreferredSize()
Reports the preferred size of the graph

Overrides:
getPreferredSize in class JComponent
Returns:
The graph's preferred size.


Copyright Jo Wood, 1996-2009, last modified, 17th April, 2009