Class Plotter

java.lang.Object
  |
  +--Plotter
Direct Known Subclasses:
Plotter2, RotatedPlotter

class Plotter
extends java.lang.Object

Responsible for plotting graphs. As many applets require special functionality, this class has grown a little too feature-rich. The first step to improve this class would be to take out some of the specialised behaviour (such as folding the x-axis) into several subclasses (the strategy employed with RotatedPlotter).

Plotter2 went some way towards improving this class, providing extra methods to simplify common tasks; the subclassing was mostly to break up the code, rather than to separate the functionality.


Field Summary
protected static double ABS_MAX
           
protected static double ABS_MIN
           
protected  boolean axes
          true if axes will be drawn, else false
protected  double bottomMargin
          When scaling to fit, the upper bound of the y range will be extended by this proportion.
protected  Solvable eqn
          The Solvable object providing solution data
protected  boolean fold
          true if the x axis will be folded, else false
protected  double foldSize
          The size of the folding interval
protected  boolean foldSym
          true if the folding interval is centred on the origin, else false
 boolean infY
          Set this to true if the y axis is to be infinite.
protected  boolean interpolate
          true if values will be interpolated, else false
protected  boolean log
          true if the vertical scale is logarithmic, else false
protected  int majorTickSize
          The length of the line drawn to indicate a major tick
protected  double maxX
          The maximum x value for which the Solvable object will be evaluated.
protected  double maxY
          The maximum y value to be included on the graph
protected  int minorTickSize
          The length of the line drawn to indicate a minor tick
protected  double minX
          The minimum x value for which the Solvable object will be evaluated.
protected  double minY
          The minimum y value to be included on the graph
 boolean noUnitsX
          Set this to true to suppress numerical labels, but still draw the main label for the axis.
 boolean noUnitsY
          Set this to true to suppress numerical labels, but still draw the main label for the axis.
protected  double rangeX
          The difference maxX - minX
protected  double rangeY
          The difference maxY - minY
protected  double[] soln
          The cache of graph data to be plotted
protected  int tickRatio
          The number of minor ticks per major tick
protected  double topMargin
          When scaling to fit, the upper bound of the y range will be extended by this proportion.
protected  double viewLeft
          The minimum x value to be included on the graph
protected  double viewRange
          The difference viewRight - viewLeft
protected  double viewRight
          The maximum x value to be included on the graph
protected  GraphLabel xLabel
          The main label for the x axis
protected  boolean xScale
          true if the horizontal scale will be drawn, else false
protected  GraphLabel yLabel
          The main label for the y axis
protected  boolean yScale
          true if the vertical scale will be drawn, else false
 
Constructor Summary
Plotter(Solvable s)
          Creates a new plotter to graph the specifed Solvable object.
 
Method Summary
protected  double chooseTickSize(double range)
          Attempts to calculate an approprite tick size (distance between minor ticks) based on the size of the axis.
 void copyScale(Plotter p)
          Sets the view range, x range and y range of this plotter according to the settings of another.
protected  double fold(double x, double f, boolean sym)
          Returns the folded version of an x coordinate.
 double fromScreenX(int x, int w)
          Converts the specified x value from screen space to graph space
 double fromScreenY(int y, int h)
          Converts the specified y value from screen space to graph space
protected static double getMax(double[] vals, int left, int right)
          Returns the maximum value from an array
protected static double getMin(double[] vals, int left, int right)
          Returns the minimum value from an array
 int getSpaceX(java.awt.FontMetrics fm)
          Calculates the size of the gap between the left edge of the display and the start of the actual graph area.
 int getSpaceY(java.awt.FontMetrics fm)
          Calculates the size of the gap between the bottom edge of the display and the start of the actual graph area.
 boolean isInterpolated()
          Returns true if y values between samples will be interpolated, else false
 boolean isLogarithmic()
          Returns whether graph data should be log()'d before plotting
 void labelAxisX(java.awt.Graphics g, int w, int h)
          Labels the x axis.
 void labelAxisY(java.awt.Graphics g, int w, int h)
          Labels the y axis.
 void paint(java.awt.Graphics g, int w, int h)
          Paints the graph, axes and scales.
 void paintAxes(java.awt.Graphics g, int w, int h)
          Paints a set of axes (the lines x=0, y=0).
 void paintGraph(java.awt.Graphics g, int w, int h)
          Paints the graph (without scales, labels or axes).
 void paintGraphFolded(java.awt.Graphics g, int w, int h, double fold, boolean sym)
          Paints the graph (without scales, labels or axes), with a folded x axis.
protected  void paintLabelBelow(java.awt.Graphics g, java.lang.String s, int x, int y)
          Paints a string just below the specified point, centred horizontally.
protected  void paintLabelToLeft(java.awt.Graphics g, java.lang.String s, int x, int y)
          Paints a string just to the left of specified point, centred vertically.
 void paintVerticalMarkers(java.awt.Graphics g, int w, int h, double[] markers)
          Plots vertical markers at the specified coordinates.
protected  void scaleToFit()
          Adjusts the minY, maxY and rangeY parameters so that the y axis range is set to the minimum interval into which all graph data falls.
protected  void scaleToFit(int left, int right)
          Adjusts the minY, maxY and rangeY parameters so that the y axis range is set to the minimum interval into which all graph data within the specified interval falls.
protected  void setAxes(boolean b)
          Set to true if axes (x = 0, y = 0) should be displayed, else false
protected  void setFold(boolean b)
          Used to enable or disable folding
protected  void setFold(boolean b, double size, boolean sym)
          Used to enable or disable folding, and sets the folding parameters.
protected  void setFold(double size, boolean sym)
          Enables folding, and sets the folding parameters.
 void setInterpolated(boolean b)
          Set to true if y values between samples should be interpolated, else false
protected  void setLabels(java.lang.String x, java.lang.String y)
          Sets the labels for the axes.
 void setLogarithmic(boolean b)
          Sets whether graph data should be log()'d before plotting
 void setRangeX(double min, double max)
          Sets the range for the x axis.
 void setRangeY(double min, double max)
          Sets the portion of the x axis displayed on the graph.
protected  void setScalesEnabled(boolean x, boolean y)
          Enables or disables the x and y scales
 void setView(double min, double max)
          Sets the portion of the x axis displayed on the graph.
 void setViewAndRange(double min, double max)
          Combines a call to setView() and setRangeX(), passing the same interval bounds to each.
protected  double snap(double d, double base)
          Helper method to round off d to the nearest multiple of base
protected  void squareValues()
          Convenience method to square all the y values.
 int toScreenX(double x, int w)
          Converts the specified x value from graph space to screen space
 int toScreenY(double y, int h)
          Converts the specified y value from graph space to screen space
protected  java.lang.String toString(double d)
          Convenience method to format a double for display.
protected  void updateSolution(int steps)
          Updates this plotter's cache of solution data.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

minorTickSize

protected int minorTickSize
The length of the line drawn to indicate a minor tick

majorTickSize

protected int majorTickSize
The length of the line drawn to indicate a major tick

tickRatio

protected int tickRatio
The number of minor ticks per major tick

log

protected boolean log
true if the vertical scale is logarithmic, else false

interpolate

protected boolean interpolate
true if values will be interpolated, else false

axes

protected boolean axes
true if axes will be drawn, else false

xScale

protected boolean xScale
true if the horizontal scale will be drawn, else false

yScale

protected boolean yScale
true if the vertical scale will be drawn, else false

fold

protected boolean fold
true if the x axis will be folded, else false

foldSym

protected boolean foldSym
true if the folding interval is centred on the origin, else false

noUnitsX

public boolean noUnitsX
Set this to true to suppress numerical labels, but still draw the main label for the axis.

noUnitsY

public boolean noUnitsY
Set this to true to suppress numerical labels, but still draw the main label for the axis.

infY

public boolean infY
Set this to true if the y axis is to be infinite. A messy hack; if a graph is to be scaled to fit, and the data is known to become infinite, setting this flag will provide a meaningful label (eg. "Infinity") on the y axis.

xLabel

protected GraphLabel xLabel
The main label for the x axis

yLabel

protected GraphLabel yLabel
The main label for the y axis

soln

protected double[] soln
The cache of graph data to be plotted

eqn

protected Solvable eqn
The Solvable object providing solution data

foldSize

protected double foldSize
The size of the folding interval

topMargin

protected double topMargin
When scaling to fit, the upper bound of the y range will be extended by this proportion.

bottomMargin

protected double bottomMargin
When scaling to fit, the upper bound of the y range will be extended by this proportion.

minX

protected double minX
The minimum x value for which the Solvable object will be evaluated.

maxX

protected double maxX
The maximum x value for which the Solvable object will be evaluated.

viewLeft

protected double viewLeft
The minimum x value to be included on the graph

viewRight

protected double viewRight
The maximum x value to be included on the graph

minY

protected double minY
The minimum y value to be included on the graph

maxY

protected double maxY
The maximum y value to be included on the graph

viewRange

protected double viewRange
The difference viewRight - viewLeft

rangeX

protected double rangeX
The difference maxX - minX

rangeY

protected double rangeY
The difference maxY - minY

ABS_MIN

protected static double ABS_MIN

ABS_MAX

protected static double ABS_MAX
Constructor Detail

Plotter

public Plotter(Solvable s)
Creates a new plotter to graph the specifed Solvable object.
Method Detail

paintVerticalMarkers

public void paintVerticalMarkers(java.awt.Graphics g,
                                 int w,
                                 int h,
                                 double[] markers)
Plots vertical markers at the specified coordinates. Each marker will be a vertical line running across the whole y-range.
Parameters:
g - The Graphics surface on which to paint
w - The width (in pixels) of the available display area
h - The height (in pixels) of the available display area
markers - An array of x coordinates at which markers

paintAxes

public void paintAxes(java.awt.Graphics g,
                      int w,
                      int h)
Paints a set of axes (the lines x=0, y=0).
Parameters:
g - The Graphics surface on which to paint
w - The width (in pixels) of the available display area
h - The height (in pixels) of the available display area

labelAxisX

public void labelAxisX(java.awt.Graphics g,
                       int w,
                       int h)
Labels the x axis. Major and minor ticks will be drawn, with a numerical label at each major tick. The label for this axis will also be painted. Note that "axis" here refers to the scale across the bottom of the graph, rather than the line y = 0.
Parameters:
g - The Graphics surface on which to paint
w - The width (in pixels) of the available display area
h - The height (in pixels) of the available display area

labelAxisY

public void labelAxisY(java.awt.Graphics g,
                       int w,
                       int h)
Labels the y axis. Major and minor ticks will be drawn, with a numerical label at each major tick. The label for this axis will also be painted. Note that "axis" here refers to the scale to the left of the graph, rather than the line x = 0.
Parameters:
g - The Graphics surface on which to paint
w - The width (in pixels) of the available display area
h - The height (in pixels) of the available display area

paintLabelBelow

protected void paintLabelBelow(java.awt.Graphics g,
                               java.lang.String s,
                               int x,
                               int y)
Paints a string just below the specified point, centred horizontally.

paintLabelToLeft

protected void paintLabelToLeft(java.awt.Graphics g,
                                java.lang.String s,
                                int x,
                                int y)
Paints a string just to the left of specified point, centred vertically. (The right-hand end of the text will appear one pixel to the left of the specified point.)

toString

protected java.lang.String toString(double d)
Convenience method to format a double for display. Implementation is via a call to BaseApplet2.toString().

paint

public void paint(java.awt.Graphics g,
                  int w,
                  int h)
Paints the graph, axes and scales.
Parameters:
g - The Graphics surface on which to paint
w - The width (in pixels) of the available display area
h - The height (in pixels) of the available display area

getSpaceX

public int getSpaceX(java.awt.FontMetrics fm)
Calculates the size of the gap between the left edge of the display and the start of the actual graph area. In other words, the space allocated for labels and scales on the vertical axis.
Parameters:
fm - The FontMetrics with which the graph will be rendered

getSpaceY

public int getSpaceY(java.awt.FontMetrics fm)
Calculates the size of the gap between the bottom edge of the display and the start of the actual graph area. In other words, the space allocated for labels and scales on the horizontal axis.
Parameters:
fm - The FontMetrics with which the graph will be rendered

paintGraph

public void paintGraph(java.awt.Graphics g,
                       int w,
                       int h)
Paints the graph (without scales, labels or axes).
Parameters:
g - The Graphics surface on which to paint
w - The width (in pixels) of the available display area
h - The height (in pixels) of the available display area

paintGraphFolded

public void paintGraphFolded(java.awt.Graphics g,
                             int w,
                             int h,
                             double fold,
                             boolean sym)
Paints the graph (without scales, labels or axes), with a folded x axis. The folding will be on the interval [0,fold] or [-fold,fold], depending on the sym parameter.
Parameters:
g - The Graphics surface on which to paint
w - The width (in pixels) of the available display area
h - The height (in pixels) of the available display area
fold - The width (in graph units) of the interval over which to fold.
sym - true if the graph should be folded onto an interval centred at the origin, else false

fold

protected double fold(double x,
                      double f,
                      boolean sym)
Returns the folded version of an x coordinate.
Parameters:
x - The value to fold
f - The width (in graph units) of the interval over which to fold.
sym - true if the graph should be folded onto an interval centred at the origin, else false

updateSolution

protected void updateSolution(int steps)
Updates this plotter's cache of solution data.
Parameters:
steps - The number of x values at which the solution should be sampled

scaleToFit

protected void scaleToFit()
Adjusts the minY, maxY and rangeY parameters so that the y axis range is set to the minimum interval into which all graph data falls.

scaleToFit

protected void scaleToFit(int left,
                          int right)
Adjusts the minY, maxY and rangeY parameters so that the y axis range is set to the minimum interval into which all graph data within the specified interval falls.
Parameters:
left - The minimum bound for the x-axis interval
right - The maximum bound for the x-axis interval

toScreenX

public int toScreenX(double x,
                     int w)
Converts the specified x value from graph space to screen space
Parameters:
x - The value
w - The width (in pixels) of the available display area

toScreenY

public int toScreenY(double y,
                     int h)
Converts the specified y value from graph space to screen space
Parameters:
y - The value
h - The height (in pixels) of the available display area

fromScreenX

public double fromScreenX(int x,
                          int w)
Converts the specified x value from screen space to graph space
Parameters:
x - The value
w - The width (in pixels) of the available display area

fromScreenY

public double fromScreenY(int y,
                          int h)
Converts the specified y value from screen space to graph space
Parameters:
y - The value
h - The height (in pixels) of the available display area

snap

protected double snap(double d,
                      double base)
Helper method to round off d to the nearest multiple of base

setRangeX

public void setRangeX(double min,
                      double max)
Sets the range for the x axis. This is not necessarily the range of values displayed on the screen (see setView()), but does determine the range of values for which the solution is evaluated. For graphs where the values are not independed (eg. numerical integration), this is often set larger than than the view range.

setView

public void setView(double min,
                    double max)
Sets the portion of the x axis displayed on the graph. This will normally be contained within the x axis bounds (see setRange()), but may overlap or be entirely outside that interval (although it is difficult to think of a useful application of the latter).

setViewAndRange

public void setViewAndRange(double min,
                            double max)
Combines a call to setView() and setRangeX(), passing the same interval bounds to each.

setRangeY

public void setRangeY(double min,
                      double max)
Sets the portion of the x axis displayed on the graph.

copyScale

public void copyScale(Plotter p)
Sets the view range, x range and y range of this plotter according to the settings of another.
Parameters:
p - The plotter, whose example this shall follow

squareValues

protected void squareValues()
Convenience method to square all the y values.

isInterpolated

public boolean isInterpolated()
Returns true if y values between samples will be interpolated, else false

setInterpolated

public void setInterpolated(boolean b)
Set to true if y values between samples should be interpolated, else false

getMax

protected static double getMax(double[] vals,
                               int left,
                               int right)
Returns the maximum value from an array
Parameters:
vals - The data
left - The index at which to begin searching
right - The index at which to stop searching

getMin

protected static double getMin(double[] vals,
                               int left,
                               int right)
Returns the minimum value from an array
Parameters:
vals - The data
left - The index at which to begin searching
right - The index at which to stop searching

chooseTickSize

protected double chooseTickSize(double range)
Attempts to calculate an approprite tick size (distance between minor ticks) based on the size of the axis.

Bug: does not take size of display area into account

Parameters:
range - The size of the axis

setAxes

protected void setAxes(boolean b)
Set to true if axes (x = 0, y = 0) should be displayed, else false

setLabels

protected void setLabels(java.lang.String x,
                         java.lang.String y)
Sets the labels for the axes.
Parameters:
x - The label for the x axis
y - The label for the y axis

setScalesEnabled

protected void setScalesEnabled(boolean x,
                                boolean y)
Enables or disables the x and y scales
Parameters:
x - true if the x axis is to have a scale
y - true if the y axis is to have a scale

setFold

protected void setFold(boolean b)
Used to enable or disable folding

setFold

protected void setFold(boolean b,
                       double size,
                       boolean sym)
Used to enable or disable folding, and sets the folding parameters.
Parameters:
b - true if folding should be enabled, else false
size - The size of interval over which to fold
sym - true if the interval extends in both directions from the origin, else false

setFold

protected void setFold(double size,
                       boolean sym)
Enables folding, and sets the folding parameters.
Parameters:
size - The size of interval over which to fold
sym - true if the interval extends in both directions from the origin, else false

isLogarithmic

public boolean isLogarithmic()
Returns whether graph data should be log()'d before plotting

setLogarithmic

public void setLogarithmic(boolean b)
Sets whether graph data should be log()'d before plotting