Layout
Class PamAxis

java.lang.Object
  extended by Layout.PamAxis
Direct Known Subclasses:
ClassifierHistoryTimeAxis, RepeatedAxis

public class PamAxis
extends java.lang.Object

Draws an axis on a Java Component.

PamAxis contains all the information needed to draw an axis, the actual drawing is generally called from the paintComponent function in the containing window.

for a good example, see PamAxisPanel which will automaticall draw four axis around a central plot.

Author:
Doug Gillespie
See Also:
PamLabel, PamAxisPanel

Field Summary
static int ABOVE_LEFT
           
private  boolean allowScaleMultiples
           
(package private)  double axisAngle
           
(package private)  double axisScale
           
(package private)  double axisValue
           
private  java.util.ArrayList<java.lang.Double> axisValues
           
static int BELOW_RIGHT
           
static int BOTH_SIDES
           
private static double[] defaultIntervals
           
private static double[] defaultScaleEnds
           
private  boolean drawLine
           
(package private)  java.awt.FontMetrics fontMetrics
           
private  double forcedFirstVal
           
private  boolean forceFirstVal
           
private  java.lang.String format
           
private  boolean fractionalScale
          Always set the scale interval to be 1/2, 1/4, 1/8, etc of the maximum
private  boolean integerFormat
           
private  double interval
           
static int INTERVAL_AUTO
           
private  double[] intervals
           
private  boolean intervalStart
           
private  java.lang.String label
           
static int LABEL_NEAR_CENTRE
           
static int LABEL_NEAR_MAX
           
static int LABEL_NEAR_MIN
           
(package private)  double labelOffsetX
           
(package private)  double labelOffsetY
           
private  int labelPos
           
private  boolean logScale
           
private  boolean logTenthsScale
           
(package private)  int maxLabelHeight
           
(package private)  int maxLabelWidth
           
private  double maxVal
           
(package private) static int minPixels
           
private  double minVal
           
private  double[] scaleEnds
           
(package private)  double theInterval
           
(package private)  double tickAngle
           
private  int tickLength
           
(package private)  int[] tickLengthX
           
(package private)  int[] tickLengthY
           
private  int tickPosition
           
(package private) static int TICKS_AUTO
           
(package private) static int TICKS_ENDSONLY
           
(package private)  double tickStepX
           
(package private)  double tickStepY
           
(package private)  double totalPixs
           
private  int x1
           
private  int x2
           
private  int y1
           
private  int y2
           
 
Constructor Summary
PamAxis(int x1, int y1, int x2, int y2, double minVal, double maxVal, boolean aboveLeft, java.lang.String label, java.lang.String format)
           
PamAxis(int x1, int y1, int x2, int y2, double minVal, double maxVal, int tickPosition, java.lang.String label, int labelPos, java.lang.String format)
           
 
Method Summary
 void drawAxis(java.awt.Graphics g)
          Draw the axis in the Graphics Context g
 void drawAxis(java.awt.Graphics g, int x1, int y1, int x2, int y2)
          Draw the axis in the graphics context g at the given position
private  void drawAxisTitle(java.awt.Graphics2D g2d)
           
 void drawGrid(java.awt.Graphics g, java.awt.Dimension plotSize, java.awt.Insets insets, int minorGrid)
           
 void drawGrid(java.awt.Graphics g, java.awt.Dimension plotSize, int minorGrid)
          Draw a grid to go with the axis.
protected  void drawLinearAxis(java.awt.Graphics2D g2d)
          Draw a linear axis on the graphics context
private  void drawLinearMinorGrid(java.awt.Graphics g, java.util.ArrayList<java.awt.Point> axisPoints, int xExtent, int yExtent, int minorGrid)
           
protected  void drawLogAxis(java.awt.Graphics2D g2d)
          Draw a logarithmic axis on the graphics context
private  void drawLogMinorGrid(java.awt.Graphics g, java.util.ArrayList<java.awt.Point> axisPoints, int xExtent, int yExtent, int minorGrid)
           
private  void drawTickAndLabel(java.awt.Graphics2D g2d, double x, double y, double value)
          Draw a tick and a label at the given point
protected  void drawTickAndLabel(java.awt.Graphics2D g2d, java.awt.Point xy, double value)
          Draw a tick and a label at the given point.
protected  java.lang.String formatValue(double val)
          Format the text for the label.
 java.util.ArrayList<java.awt.Point> getAxisPoints(boolean extraOne)
          Work out the coordinates of all tick marks.
 java.util.ArrayList<java.lang.Double> getAxisValues()
           
 double getDataValue(double position)
          Converts a position on the plot into a data value based on the axis max, min and scale.
static double getDefaultScaleEnd(double val, double minVal)
           
 int getExtent(java.awt.Graphics g)
          Gets the dimension of the axis parallel to the direction of the tickmark - i.e.
 int getExtent(java.awt.Graphics g, java.lang.String typicalString)
          Gets the dimension of the axis parallel to the direction of the tickmark - i.e.
 int getExtent2(java.awt.Graphics g)
          Gets the dimension of the axis perpendicular to the direction of the tickmark - i.e.
 int getExtent2(java.awt.Graphics g, java.lang.String typicalString)
          Gets the dimension of the axis perpendicular to the direction of the tickmark - i.e.
 java.lang.String getFormat()
           
private  double getInterval(double range, int totalPixs, int minPixs)
           
 java.lang.String getLabel()
           
 int getLabelPos()
           
private  java.util.ArrayList<java.awt.Point> getLinearPoints(boolean extraOne)
          work out the coordinates of all tick marks using a linear scale
protected  java.util.ArrayList<java.awt.Point> getLogPoints(boolean extraOne)
          Get the coordinates of tick points for a logarithmic axis
 double getMaxVal()
           
 double getMinVal()
           
 double getPosition(double dataValue)
          The axis knows all about scale and can tell us the pixel value for any given data value.
 int getTickPosition()
           
 int getX1()
           
 int getX2()
           
 int getY1()
           
 int getY2()
           
 boolean isAllowScaleMultiples()
           
 boolean isDrawLine()
           
 boolean isFractionalScale()
           
 boolean isIntegerFormat()
           
 boolean isLogScale()
           
 boolean isLogTenthsScale()
           
 void setAllowScaleMultiples(boolean allowScaleMultiples)
           
 void setAngleScales(boolean angleScales)
          Easy way to make scales end at 45, 90, 180, etc and step in sensible 45, 90, etc.
 void setDrawLine(boolean drawLine)
           
 void setForceFirstVal(boolean forceFirstVal, double forcedFirstVal)
           
 void setFormat(java.lang.String format)
          Set the format string for writing out numbers on the axis.
 void setFractionalScale(boolean fractionalScale)
           
 void setInterval(double interval)
          Set the interval between ticks.
 void setLabel(java.lang.String label)
           
 void setLabelPos(int labelPos)
           
 void setLogScale(boolean logScale)
           
 void setLogTenthsScale(boolean logTenthsScale)
           
 void setMaxVal(double maxVal)
           
 void setMinVal(double minVal)
           
 void setPosition(int x1, int y1, int x2, int y2)
          Set the axis default coordinates
 void setRange(double minVal, double maxVal)
          Set the minimum and maximum values for the axis.
 void setTickPosition(int tickPosition)
           
 void setX1(int x1)
           
 void setX2(int x2)
           
 void setY1(int y1)
           
 void setY2(int y2)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TICKS_ENDSONLY

static final int TICKS_ENDSONLY
See Also:
Constant Field Values

TICKS_AUTO

static final int TICKS_AUTO
See Also:
Constant Field Values

defaultIntervals

private static final double[] defaultIntervals

intervals

private double[] intervals

defaultScaleEnds

private static final double[] defaultScaleEnds

scaleEnds

private double[] scaleEnds

allowScaleMultiples

private boolean allowScaleMultiples

minPixels

static final int minPixels
See Also:
Constant Field Values

INTERVAL_AUTO

public static final int INTERVAL_AUTO
See Also:
Constant Field Values

ABOVE_LEFT

public static final int ABOVE_LEFT
See Also:
Constant Field Values

BELOW_RIGHT

public static final int BELOW_RIGHT
See Also:
Constant Field Values

BOTH_SIDES

public static final int BOTH_SIDES
See Also:
Constant Field Values

LABEL_NEAR_MAX

public static final int LABEL_NEAR_MAX
See Also:
Constant Field Values

LABEL_NEAR_MIN

public static final int LABEL_NEAR_MIN
See Also:
Constant Field Values

LABEL_NEAR_CENTRE

public static final int LABEL_NEAR_CENTRE
See Also:
Constant Field Values

x1

private int x1

x2

private int x2

y1

private int y1

y2

private int y2

minVal

private double minVal

maxVal

private double maxVal

forceFirstVal

private boolean forceFirstVal

forcedFirstVal

private double forcedFirstVal

tickPosition

private int tickPosition

interval

private double interval

logTenthsScale

private boolean logTenthsScale

drawLine

private boolean drawLine

intervalStart

private boolean intervalStart

tickLength

private int tickLength

label

private java.lang.String label

format

private java.lang.String format

integerFormat

private boolean integerFormat

logScale

private boolean logScale

labelPos

private int labelPos

fractionalScale

private boolean fractionalScale
Always set the scale interval to be 1/2, 1/4, 1/8, etc of the maximum


fontMetrics

java.awt.FontMetrics fontMetrics

axisValue

double axisValue

axisScale

double axisScale

tickLengthX

int[] tickLengthX

tickLengthY

int[] tickLengthY

labelOffsetX

double labelOffsetX

labelOffsetY

double labelOffsetY

tickStepX

double tickStepX

tickStepY

double tickStepY

axisAngle

double axisAngle

theInterval

double theInterval

maxLabelWidth

int maxLabelWidth

maxLabelHeight

int maxLabelHeight

totalPixs

double totalPixs

tickAngle

double tickAngle

axisValues

private java.util.ArrayList<java.lang.Double> axisValues
Constructor Detail

PamAxis

public PamAxis(int x1,
               int y1,
               int x2,
               int y2,
               double minVal,
               double maxVal,
               int tickPosition,
               java.lang.String label,
               int labelPos,
               java.lang.String format)
Parameters:
x1 - first x coordinate of axis
y1 - first y coordinate of axis
x2 - second x coordinate of axis
y2 - second coordinate of axis
minVal - minimum axis value
maxVal - maximum axis value
tickPosition - tick position (ABOVE_LEFT or BELOW_RIGHT)
label - text for label (or null if no label)
labelPos - Position of axis label (LABEL_NEAR_MAX, LABEL_NEAR_MIN or LABEL_NEAR_CENTRE)
format - format for numbers printed on the display. This must be a standard format String such as "%d", "%f", "%3.1f", "%.2 seconds", etc.

PamAxis

public PamAxis(int x1,
               int y1,
               int x2,
               int y2,
               double minVal,
               double maxVal,
               boolean aboveLeft,
               java.lang.String label,
               java.lang.String format)
Parameters:
x1 - x1
y1 - y1
x2 - x2
y2 - y2
minVal - min axis value
maxVal - max axis value
aboveLeft - above and / or to the left
label - axis label
format - format of numbers
Method Detail

setPosition

public void setPosition(int x1,
                        int y1,
                        int x2,
                        int y2)
Set the axis default coordinates

Parameters:
x1 -
y1 -
x2 -
y2 -

setInterval

public void setInterval(double interval)
Set the interval between ticks. This is by default set to INTERVAL_AUTO whereby an interval is chosen so that there is a tick mark approximately every 100 pixels.

Parameters:
interval - the interval or INTERVAL_AUTO

setRange

public void setRange(double minVal,
                     double maxVal)
Set the minimum and maximum values for the axis.

Parameters:
minVal -
maxVal -

drawAxis

public void drawAxis(java.awt.Graphics g,
                     int x1,
                     int y1,
                     int x2,
                     int y2)
Draw the axis in the graphics context g at the given position

Parameters:
g -
x1 -
y1 -
x2 -
y2 -

drawAxis

public void drawAxis(java.awt.Graphics g)
Draw the axis in the Graphics Context g

Parameters:
g - graphics context to draw on

getAxisPoints

public java.util.ArrayList<java.awt.Point> getAxisPoints(boolean extraOne)
Work out the coordinates of all tick marks.

Parameters:
extraOne -
Returns:
list of axis points.

getLinearPoints

private java.util.ArrayList<java.awt.Point> getLinearPoints(boolean extraOne)
work out the coordinates of all tick marks using a linear scale

Parameters:
extraOne -
Returns:

drawLinearAxis

protected void drawLinearAxis(java.awt.Graphics2D g2d)
Draw a linear axis on the graphics context

Parameters:
g2d - graphics context to draw on

drawLogAxis

protected void drawLogAxis(java.awt.Graphics2D g2d)
Draw a logarithmic axis on the graphics context

Parameters:
g2d - graphics context to draw on

getLogPoints

protected java.util.ArrayList<java.awt.Point> getLogPoints(boolean extraOne)
Get the coordinates of tick points for a logarithmic axis

Parameters:
extraOne -
Returns:
Array of points

drawTickAndLabel

protected void drawTickAndLabel(java.awt.Graphics2D g2d,
                                java.awt.Point xy,
                                double value)
Draw a tick and a label at the given point.

Parameters:
g2d -
xy -
value -

drawTickAndLabel

private void drawTickAndLabel(java.awt.Graphics2D g2d,
                              double x,
                              double y,
                              double value)
Draw a tick and a label at the given point

Parameters:
g2d -
x -
y -
value -

formatValue

protected java.lang.String formatValue(double val)
Format the text for the label.

Parameters:
val - value
Returns:
formatted String

drawGrid

public void drawGrid(java.awt.Graphics g,
                     java.awt.Dimension plotSize,
                     int minorGrid)
Draw a grid to go with the axis. Generally, the graphics handle for grid drawing will be some inner window sitting inside the window containing the axis.

Parameters:
g - Graphics context to draw on
plotSize - size of the graphic
minorGrid - draw a minor grid as well as lines at the main tick marks.

drawGrid

public void drawGrid(java.awt.Graphics g,
                     java.awt.Dimension plotSize,
                     java.awt.Insets insets,
                     int minorGrid)

drawLinearMinorGrid

private void drawLinearMinorGrid(java.awt.Graphics g,
                                 java.util.ArrayList<java.awt.Point> axisPoints,
                                 int xExtent,
                                 int yExtent,
                                 int minorGrid)

drawLogMinorGrid

private void drawLogMinorGrid(java.awt.Graphics g,
                              java.util.ArrayList<java.awt.Point> axisPoints,
                              int xExtent,
                              int yExtent,
                              int minorGrid)

drawAxisTitle

private void drawAxisTitle(java.awt.Graphics2D g2d)

getExtent2

public int getExtent2(java.awt.Graphics g)
Gets the dimension of the axis perpendicular to the direction of the tickmark - i.e. for a vertical axis, it's half the text height, for a horizontal axis it's half the typical string width.

Parameters:
g - graphics handle for component the axis is to be draw on
Returns:
axis extent in pixels

getExtent2

public int getExtent2(java.awt.Graphics g,
                      java.lang.String typicalString)
Gets the dimension of the axis perpendicular to the direction of the tickmark - i.e. for a vertical axis, it's half the text height, for a horizontal axis it's half the typical string width.

Parameters:
g - graphics handle for component the axis is to be draw on
typicalString - typical text string.
Returns:
Axis extent

getExtent

public int getExtent(java.awt.Graphics g)
Gets the dimension of the axis parallel to the direction of the tickmark - i.e. the ticklength + the text height + the label height.

Parameters:
g - graphics handle for component the axis is to be draw on
Returns:
axis extent in pixels

getExtent

public int getExtent(java.awt.Graphics g,
                     java.lang.String typicalString)
Gets the dimension of the axis parallel to the direction of the tickmark - i.e. the ticklength + the text height + the label height.

Parameters:
g - graphics handle the axis is being draw on
typicalString - typical label string for a tick
Returns:
axisextent in pixels

getInterval

private double getInterval(double range,
                           int totalPixs,
                           int minPixs)

isLogScale

public boolean isLogScale()

setLogScale

public void setLogScale(boolean logScale)

isDrawLine

public boolean isDrawLine()

setDrawLine

public void setDrawLine(boolean drawLine)

getFormat

public java.lang.String getFormat()

setFormat

public void setFormat(java.lang.String format)
Set the format string for writing out numbers on the axis.

Parameters:
format - format string

isIntegerFormat

public boolean isIntegerFormat()

getLabel

public java.lang.String getLabel()

setLabel

public void setLabel(java.lang.String label)

getLabelPos

public int getLabelPos()

setLabelPos

public void setLabelPos(int labelPos)

getTickPosition

public int getTickPosition()

setTickPosition

public void setTickPosition(int tickPosition)

getMaxVal

public double getMaxVal()

setMaxVal

public void setMaxVal(double maxVal)

getMinVal

public double getMinVal()

setMinVal

public void setMinVal(double minVal)

getDefaultScaleEnd

public static double getDefaultScaleEnd(double val,
                                        double minVal)

setForceFirstVal

public void setForceFirstVal(boolean forceFirstVal,
                             double forcedFirstVal)

isLogTenthsScale

public boolean isLogTenthsScale()

setLogTenthsScale

public void setLogTenthsScale(boolean logTenthsScale)

getAxisValues

public java.util.ArrayList<java.lang.Double> getAxisValues()

getX1

public int getX1()

setX1

public void setX1(int x1)

getX2

public int getX2()

setX2

public void setX2(int x2)

getY1

public int getY1()

setY1

public void setY1(int y1)

getY2

public int getY2()

setY2

public void setY2(int y2)

getPosition

public double getPosition(double dataValue)
The axis knows all about scale and can tell us the pixel value for any given data value.

Parameters:
dataValue -
Returns:
position in pixels along the axis for a given data value.

getDataValue

public double getDataValue(double position)
Converts a position on the plot into a data value based on the axis max, min and scale.

This is the exact compliment of getPosition()

Parameters:
position - position along the axis in pixels.
Returns:
data value.

isAllowScaleMultiples

public boolean isAllowScaleMultiples()

setAllowScaleMultiples

public void setAllowScaleMultiples(boolean allowScaleMultiples)

setAngleScales

public void setAngleScales(boolean angleScales)
Easy way to make scales end at 45, 90, 180, etc and step in sensible 45, 90, etc. steps.

Parameters:
angleScales -

isFractionalScale

public boolean isFractionalScale()

setFractionalScale

public void setFractionalScale(boolean fractionalScale)