org.opensourcephysics.display
Class Histogram

java.lang.Object
  extended byjavax.swing.table.AbstractTableModel
      extended byorg.opensourcephysics.display.Histogram
All Implemented Interfaces:
Drawable, Measurable, java.io.Serializable, javax.swing.table.TableModel

public class Histogram
extends javax.swing.table.AbstractTableModel
implements Measurable

Histogram maps bin number to occurences. Histogram is Drawable and can be rendered on a DrawingPanel. Histogram also implements TableModel and can be displayed in a JTable. By default, bins consist of (notation: [ inclusive, ) exclusive): ..., [-1,0), [0,1), [1,2), ...

See Also:
Serialized Form

Field Summary
 boolean adjustForWidth
          Should the height be adjusted by bin width? Default is false.
protected  java.awt.Color binColor
          color of bins
protected  short binStyle
          style for drawing bins
static short DRAW_BIN
          draw bin from y min to top of bin
static short DRAW_POINT
          draw point at top of bin
 boolean logScale
          Should histogram be drawn on a log scale? Default is false.
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
Histogram()
          Histogram contructor.
 
Method Summary
 void append(double value)
          Appends a value with 1 occurence.
 void append(double[] values)
          Appends an array of values with 1 occurence.
 void append(double value, double numberOfOccurences)
          Append a value with number of occurences to the Histogram.
 void append(java.lang.String inputPathName)
          Appends values from an input file.
 void clear()
          Clears all data from this histogram and resets min and max values.
 void draw(DrawingPanel drawingPanel, java.awt.Graphics g)
          Draws this histogram in the drawing panel.
protected  void drawBin(DrawingPanel drawingPanel, java.awt.Graphics g, int binNumber, double occurences)
          Draws a filled bin.
protected  void drawPoint(DrawingPanel drawingPanel, java.awt.Graphics g, int binNumber, double occurences)
          Draws a point at the top of a bin.
 java.util.Map.Entry[] entries()
          Gets an array of bin number-occurences pairs
 double getBinOffset()
          Gets the offset of the bins.
 double getBinWidth()
          Gets the width of a bin.
 java.lang.Class getColumnClass(int columnIndex)
          Gets the type of object for JTable entry.
 int getColumnCount()
          Gets the name of the colummn for rendering in a JTable
 java.lang.String getColumnName(int column)
          Gets the name of the colummn for rendering in a JTable
 double getLeftMostBinPosition(int binNumber)
          Method getLeftMostBinPosition
 double[][] getLogPoints()
          Gets a data array containing both the bin centers and the values within the bins.
 double[][] getPoints()
          Gets a data array containing both the bin centers and the values within the bins.
 double getRightMostBinPosition(int binNumber)
          Method getRightMostBinPosition
 int getRowCount()
          Gets the number of rows for rendering in a JTable.
 java.lang.Object getValueAt(int row, int column)
          Gets a bin number or occurences for bin number for rendering in a JTable.
 double getXMax()
          Gets the x world coordinate for the right hand side of this histogram.
 double getXMin()
          Gets the x world coordinate for the left hand side of this histogram.
 double[] getXPoints()
          Gets an array containing the bin centers.
 double getYMax()
          Gets the y world coordinate for the top of this histogram.
 double getYMin()
          Gets the y world coordinate for the bottom of this histogram.
 double[] getYPoints()
          Gets an array containing the values within the bins.
 int hashCode(double value)
          Computes the hash code (bin number) for the specified value
 boolean isMeasured()
          Gets the valid measure flag.
 void read(java.lang.String inputPathName)
          Reads a file and appends the data contained in the file to this Histogram.
 void setBarOffset(double _barOffset)
          Set the offset of the bars as a fraction of a bin width.
 void setBinColor(java.awt.Color _binColor)
          Sets the bin color.
 void setBinOffset(double _binOffset)
          Sets the offset of the bins.
 void setBinStyle(short style)
          Sets the style for drawing this histogram.
 void setBinWidth(double _binWidth)
          Sets the width of a bin.
 void setDiscrete(boolean _discrete)
          Sets the discrete flag.
 void setNormalizedToOne(boolean b)
          Normalizes the occurences in this histogram to one.
 void setXYColumnNames(java.lang.String _binColumnName, java.lang.String _yColumnName)
          Sets the column names when rendering this histogram in a JTable.
 java.lang.String toSortedString()
          Creates a string representation of this Histogram.
 java.lang.String toString()
          Creates a string representation of this Histogram.
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, isCellEditable, removeTableModelListener, setValueAt
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DRAW_POINT

public static final short DRAW_POINT
draw point at top of bin

See Also:
Constant Field Values

DRAW_BIN

public static final short DRAW_BIN
draw bin from y min to top of bin

See Also:
Constant Field Values

logScale

public boolean logScale
Should histogram be drawn on a log scale? Default is false.


adjustForWidth

public boolean adjustForWidth
Should the height be adjusted by bin width? Default is false.


binColor

protected java.awt.Color binColor
color of bins


binStyle

protected short binStyle
style for drawing bins

Constructor Detail

Histogram

public Histogram()
Histogram contructor.

Method Detail

read

public void read(java.lang.String inputPathName)
          throws java.io.IOException
Reads a file and appends the data contained in the file to this Histogram. The format of the file is bins \t occurences. Lines beginning with # and empty lines are ignored.

Parameters:
inputPathName - A pathname string.
Throws:
java.io.IOException - Description of the Exception

toSortedString

public java.lang.String toSortedString()
Creates a string representation of this Histogram. The bins are displayed in ascending order. The format of this string is bin number \t occurences. Each bin starts on a new line.

Returns:
A String with the number of occurences for each bin.
See Also:
toString()

toString

public java.lang.String toString()
Creates a string representation of this Histogram. The format is bin number\t occurences. Each new bin starts on a new line.

Returns:
A String with the number of occurences for each bin.

hashCode

public int hashCode(double value)
Computes the hash code (bin number) for the specified value

Parameters:
value -
Returns:
the hash code

append

public void append(double value,
                   double numberOfOccurences)
Append a value with number of occurences to the Histogram.

Parameters:
value -
numberOfOccurences -

append

public void append(double value)
Appends a value with 1 occurence.

Parameters:
value -

append

public void append(java.lang.String inputPathName)
            throws java.io.IOException
Appends values from an input file. Each value is separated by a \n

Parameters:
inputPathName - A pathname string.
Throws:
java.io.IOException - Description of the Exception

append

public void append(double[] values)
Appends an array of values with 1 occurence.

Parameters:
values -

draw

public void draw(DrawingPanel drawingPanel,
                 java.awt.Graphics g)
Draws this histogram in the drawing panel.

Specified by:
draw in interface Drawable
Parameters:
drawingPanel -
g -

clear

public void clear()
Clears all data from this histogram and resets min and max values.


entries

public java.util.Map.Entry[] entries()
Gets an array of bin number-occurences pairs

Returns:
The entries.

setBinStyle

public void setBinStyle(short style)
Sets the style for drawing this histogram. Options are DRAW_POINT, which draws a point at the top of the bin, and DRAW_BIN which draws the entire bin down to the x axis. Default is DRAW_BIN.

Parameters:
style -

setDiscrete

public void setDiscrete(boolean _discrete)
Sets the discrete flag.

Parameters:
_discrete - true<\code> if bins are discrete, false<\code> if bins are continuous.

setBinOffset

public void setBinOffset(double _binOffset)
Sets the offset of the bins. Default is 0.

Parameters:
_binOffset -

setBarOffset

public void setBarOffset(double _barOffset)
Set the offset of the bars as a fraction of a bin width. The offset is the amount by which this histogram is shifted to the right, so that it peeks out from behind later histograms when displayed in a DrawingPanel.

Parameters:
_barOffset - The new barOffset value

setBinColor

public void setBinColor(java.awt.Color _binColor)
Sets the bin color.

Parameters:
_binColor -

setBinWidth

public void setBinWidth(double _binWidth)
Sets the width of a bin.

Parameters:
_binWidth -

setXYColumnNames

public void setXYColumnNames(java.lang.String _binColumnName,
                             java.lang.String _yColumnName)
Sets the column names when rendering this histogram in a JTable.

Parameters:
_binColumnName -
_yColumnName -

setNormalizedToOne

public void setNormalizedToOne(boolean b)
Normalizes the occurences in this histogram to one.

Parameters:
b -

getBinWidth

public double getBinWidth()
Gets the width of a bin.

Returns:
The bin width.

getBinOffset

public double getBinOffset()
Gets the offset of the bins.

Returns:
The bin offset.

getXMin

public double getXMin()
Gets the x world coordinate for the left hand side of this histogram.

Specified by:
getXMin in interface Measurable
Returns:
xmin

getXMax

public double getXMax()
Gets the x world coordinate for the right hand side of this histogram.

Specified by:
getXMax in interface Measurable
Returns:
xmax

getYMin

public double getYMin()
Gets the y world coordinate for the bottom of this histogram.

Specified by:
getYMin in interface Measurable
Returns:
minimum y value

getYMax

public double getYMax()
Gets the y world coordinate for the top of this histogram.

Specified by:
getYMax in interface Measurable
Returns:
xmax

isMeasured

public boolean isMeasured()
Gets the valid measure flag. The measure is valid if this histogram is not empty.

Specified by:
isMeasured in interface Measurable
Returns:
true<\code> if measure is valid.

getColumnName

public java.lang.String getColumnName(int column)
Gets the name of the colummn for rendering in a JTable

Specified by:
getColumnName in interface javax.swing.table.TableModel
Parameters:
column - the column whose value is to be queried
Returns:
the name

getRowCount

public int getRowCount()
Gets the number of rows for rendering in a JTable.

Specified by:
getRowCount in interface javax.swing.table.TableModel
Returns:
the count

getColumnCount

public int getColumnCount()
Gets the name of the colummn for rendering in a JTable

Specified by:
getColumnCount in interface javax.swing.table.TableModel
Returns:
the name

getValueAt

public java.lang.Object getValueAt(int row,
                                   int column)
Gets a bin number or occurences for bin number for rendering in a JTable.

Specified by:
getValueAt in interface javax.swing.table.TableModel
Parameters:
row - the row whose value is to be queried
column - the column whose value is to be queried
Returns:
the datum

getColumnClass

public java.lang.Class getColumnClass(int columnIndex)
Gets the type of object for JTable entry.

Specified by:
getColumnClass in interface javax.swing.table.TableModel
Parameters:
columnIndex - the column whose value is to be queried
Returns:
the class

drawPoint

protected void drawPoint(DrawingPanel drawingPanel,
                         java.awt.Graphics g,
                         int binNumber,
                         double occurences)
Draws a point at the top of a bin.

Parameters:
drawingPanel -
g -
binNumber -
occurences -

drawBin

protected void drawBin(DrawingPanel drawingPanel,
                       java.awt.Graphics g,
                       int binNumber,
                       double occurences)
Draws a filled bin.

Parameters:
drawingPanel -
g -
binNumber -
occurences -

getXPoints

public double[] getXPoints()
Gets an array containing the bin centers.

Returns:
the bins

getYPoints

public double[] getYPoints()
Gets an array containing the values within the bins.

Returns:
the values of the bins

getPoints

public double[][] getPoints()
Gets a data array containing both the bin centers and the values within the bins.

Returns:
a double[index][2] array of data

getLogPoints

public double[][] getLogPoints()
Gets a data array containing both the bin centers and the values within the bins.

Returns:
a double[index][2] array of data

getLeftMostBinPosition

public double getLeftMostBinPosition(int binNumber)
Method getLeftMostBinPosition

Parameters:
binNumber -
Returns:
position

getRightMostBinPosition

public double getRightMostBinPosition(int binNumber)
Method getRightMostBinPosition

Parameters:
binNumber -
Returns:
position