LiveGraph
data visualisation and analysis framework

org.LiveGraph.plot
Class Plotter

java.lang.Object
  extended by org.LiveGraph.plot.Plotter
All Implemented Interfaces:
CacheObserver, SettingsObserver

public class Plotter
extends java.lang.Object
implements CacheObserver, SettingsObserver

This class handles the conversion of the cached data to a screen image and the drawing of the image on a Graphics object.
This class uses an AffineTransform object to convert the data held in the cache to a data plot in screen coordinates. In order to keep the AffineTransform object appropriate for the current display at all times a plotter listens to various DataCache and ObservableSettings events; in addition it offers a setScreenSize(int, int)-method which must be called each time when the canvas-panel that uses the plotter changes its size.
Whenever the dataCache changes, a plotter uses the current datScrTransform object to convert the data from the cache into a plot in screen coordinates according to the current global graph- and series-settings. The screen data obtained this way is locally cached in the screenDataBuffer array. This way the data does not need to be re-computed each time the plot must be drawn on the screen.
In this version the plotter handles data values transformations required by the display options (if any) on the fly. If new options should be added to theinterface, this mechanism should be replaces by a more flexible solution.

This product includes software developed by the LiveGraph project and its contributors.
(http://www.live-graph.org)
Copyright (c) 2007 G. Paperin.
All rights reserved.

File: Plotter.java

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following terms and conditions are met:

1. Redistributions of source code must retain the above acknowledgement of the LiveGraph project and its web-site, the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above acknowledgement of the LiveGraph project and its web-site, the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software or any derived software must display the following acknowledgement:
This product includes software developed by the LiveGraph project and its contributors.
(http://www.live-graph.org)

4. All advertising materials distributed in form of HTML pages or any other technology permitting active hyper-links that mention features or use of this software or any derived software must display the acknowledgment specified in condition 3 of this agreement, and in addition, include a visible and working hyper-link to the LiveGraph homepage (http://www.live-graph.org).

THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version:
"1.1.1"
Author:
Greg Paperin (http://www.paperin.org)

Nested Class Summary
private  class Plotter.MutableInt
          For holding mutable ints as objects.
private  class Plotter.PointsByIndexComparator
          Used in order to compare points referenced by their index in Plotter.SeriesScreenData.points; the comparison is by x-xoordinates.
private  class Plotter.SeriesScreenData
          A data structure to hold the locally cached plot data for a data series.
private static interface Plotter.Transformer
          Used to encapsulate data series points translation routines.
 
Nested classes/interfaces inherited from interface org.LiveGraph.dataCache.CacheObserver
CacheObserver.CacheEvent
 
Field Summary
private static int AXES_LBL_GAP
          Gap between axes labels.
private static int AXES_MARKS_SIZE
          Size of the scale marks on the axes.
private  DataCache dataCache
          The data cache.
private  boolean dataComputationRunning
          Whether screen data computation is in progress.
private static int DATAPOINT_RAD
          Radius for datapoints marks on small graphs.
private  java.awt.geom.Rectangle2D.Double dataViewport
          Viewable area in data coordinates.
private  java.awt.geom.AffineTransform datScrTransform
          Data space to screen space transformation.
private static int FONT_SIZE
          Label font size.
private  GraphSettings graphSetts
          Graph settings.
private static java.awt.Color HAXIS_COL
          X axis colour.
private  double hGridStep
          The actual h-grid step after the consideration of plot size.
private  boolean highlightPoints
          Whether dara points close to the mouse position should be highlighted.
private static int HMARGIN
          Horisiontal margin.
private static Plotter.Transformer IDTransform
           
private static int MIN_GRIDLINE_DIST
          The minimum distance between grid lines (in pixels).
private static java.awt.Dimension minScreenSize
           
private  boolean pointHighlightComputationRunning
          Whether screen data computation is in progress.
private  Plotter.PointsByIndexComparator pointsByIndexComparator
          Used for sorting points by x values.
private  Plotter.SeriesScreenData[] screenDataBuffer
          Buffers the screen coordinates of the graphs.
private  java.awt.Dimension screenSize
          Screen size in pixels.
private  boolean selfSettingHGridSize
          Whether the next change of h-grid settings was initiated by this plotter and should therefore be ignored by the plotter's handler.
private  boolean selfSettingVGridSize
          Whether the next change of v-grid settings was initiated by this plotter and should therefore be ignored by the plotter's handler.
private  DataSeriesSettings seriesSetts
          Data series settings.
private  boolean showAtLeastOneSeries
          Whether anythig at all is to be displayed.
private  double userHGridStep
          The h-grid size get by a settings change that was not initiated by this plotter itself.
private  double userVGridStep
          The v-grid size get by a settings change that was not initiated by this plotter itself.
private static java.awt.Color VAXIS_COL
          Y axis colour.
private  double vGridStep
          The actual v-grid step after the consideration of plot size.
private static int VMARGIN
          Vertical margin.
private  double[] xCoordinates
          Buffers the x coordinates.
 
Constructor Summary
Plotter(DataCache dataCache)
          Creates a plotter for the data held in the specified cache.
 
Method Summary
 void cacheEventFired(DataCache cache, CacheObserver.CacheEvent event)
          If cached label info is changed, the screen buffer is recreated; if cached data is updated the view port and the screen data are recomputed.
private  void computeGridSteps()
          Computes the actual grid mesh sizes taking in account the current plot size.
private  void computeScreenData()
          Computes the screen coordinates for the visible data series.
private  void computeScreenDataForSeries(int seriesIndex)
          Compute the screen coordinates for the specified series.
private  void computeXCoordinates()
          Compute the x coordinates in data space according to the current settings.
 int getScreenHeight()
          Gets canvas screen size (Y).
 int getScreenWidth()
          Gets canvas screen size (X).
 boolean getShowAtLeastOneSeries()
          Gets whether at least one series is to be plotted.
 java.util.List<java.lang.Integer> highlightAround(java.awt.Point sp)
          Highlights the points around the specified point.
private  void initGlobalParameters()
          Used by the constructor to initialise global settings references.
 void paint(java.awt.Graphics g)
          Paints the previously computed graphs along with the axes, labels, grids and so on to the specified graphics context.
private  void paintAxes(java.awt.Graphics g)
          Paints the coordinate axes.
private  void paintData(java.awt.Graphics g)
          Paints the data series.
private  void paintGrids(java.awt.Graphics g)
          Paints the grid.
private  void resetDataViewport()
          Recomputes the currently visible data area according to the current graph and series settings.
private  void resetScreenDataBuffer()
          Reallocates the screen data buffer.
 java.awt.geom.Point2D.Double screenToDataPoint(java.awt.Point sp)
          Map the specified point in screen coordinates into the data space.
 boolean screenTooSmall()
          Gets whether the screen area is large enough to paint the graph.
 void setScreenSize(int width, int height)
          Set the current view screen size.
 void settingHasChanged(DataSeriesSettings settings, java.lang.String info)
          Calls the neccesary recoputations when seties settings are changed.
 void settingHasChanged(GraphSettings settings, java.lang.String info)
          Calls the neccesary recoputations when graph settings are changed.
 void settingHasChanged(ObservableSettings settings, java.lang.Object info)
          Dispatches settings change events.
private  void updateDatScrTransform()
          Updates the data to screen transform map according to the currently visible data area and screen size.
private  void updateScreenData()
          First, recomputes the currently visible data area according to the current graph and series settings; then, computes the screen coordinates for the visible data series..
private  void updateSeriesTransformer(int seriesIndex)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VMARGIN

private static final int VMARGIN
Vertical margin.

See Also:
Constant Field Values

HMARGIN

private static final int HMARGIN
Horisiontal margin.

See Also:
Constant Field Values

minScreenSize

private static final java.awt.Dimension minScreenSize

VAXIS_COL

private static final java.awt.Color VAXIS_COL
Y axis colour.


HAXIS_COL

private static final java.awt.Color HAXIS_COL
X axis colour.


FONT_SIZE

private static final int FONT_SIZE
Label font size.

See Also:
Constant Field Values

AXES_LBL_GAP

private static final int AXES_LBL_GAP
Gap between axes labels.

See Also:
Constant Field Values

AXES_MARKS_SIZE

private static final int AXES_MARKS_SIZE
Size of the scale marks on the axes.

See Also:
Constant Field Values

DATAPOINT_RAD

private static final int DATAPOINT_RAD
Radius for datapoints marks on small graphs.

See Also:
Constant Field Values

MIN_GRIDLINE_DIST

private static final int MIN_GRIDLINE_DIST
The minimum distance between grid lines (in pixels).

See Also:
Constant Field Values

dataCache

private DataCache dataCache
The data cache.


seriesSetts

private DataSeriesSettings seriesSetts
Data series settings.


graphSetts

private GraphSettings graphSetts
Graph settings.


showAtLeastOneSeries

private boolean showAtLeastOneSeries
Whether anythig at all is to be displayed.


screenDataBuffer

private Plotter.SeriesScreenData[] screenDataBuffer
Buffers the screen coordinates of the graphs.


xCoordinates

private double[] xCoordinates
Buffers the x coordinates.


pointsByIndexComparator

private Plotter.PointsByIndexComparator pointsByIndexComparator
Used for sorting points by x values.


dataViewport

private java.awt.geom.Rectangle2D.Double dataViewport
Viewable area in data coordinates.


screenSize

private java.awt.Dimension screenSize
Screen size in pixels.


datScrTransform

private java.awt.geom.AffineTransform datScrTransform
Data space to screen space transformation.


dataComputationRunning

private boolean dataComputationRunning
Whether screen data computation is in progress.


pointHighlightComputationRunning

private boolean pointHighlightComputationRunning
Whether screen data computation is in progress.


selfSettingHGridSize

private boolean selfSettingHGridSize
Whether the next change of h-grid settings was initiated by this plotter and should therefore be ignored by the plotter's handler.


userHGridStep

private double userHGridStep
The h-grid size get by a settings change that was not initiated by this plotter itself.


hGridStep

private double hGridStep
The actual h-grid step after the consideration of plot size.


selfSettingVGridSize

private boolean selfSettingVGridSize
Whether the next change of v-grid settings was initiated by this plotter and should therefore be ignored by the plotter's handler.


userVGridStep

private double userVGridStep
The v-grid size get by a settings change that was not initiated by this plotter itself.


vGridStep

private double vGridStep
The actual v-grid step after the consideration of plot size.


highlightPoints

private boolean highlightPoints
Whether dara points close to the mouse position should be highlighted.


IDTransform

private static Plotter.Transformer IDTransform
Constructor Detail

Plotter

public Plotter(DataCache dataCache)
Creates a plotter for the data held in the specified cache.

Parameters:
dataCache - Cache holding the data to plot.
Method Detail

initGlobalParameters

private void initGlobalParameters()
Used by the constructor to initialise global settings references.


screenTooSmall

public boolean screenTooSmall()
Gets whether the screen area is large enough to paint the graph.

Returns:
true iff the screen area is large enough to paint the graph.

getShowAtLeastOneSeries

public boolean getShowAtLeastOneSeries()
Gets whether at least one series is to be plotted.

Returns:
true if at seast one data series should be plotted, false otherwise.

paint

public void paint(java.awt.Graphics g)
Paints the previously computed graphs along with the axes, labels, grids and so on to the specified graphics context.

Parameters:
g - Paint context.

paintGrids

private void paintGrids(java.awt.Graphics g)
Paints the grid.

Parameters:
g - The graphics context.

paintAxes

private void paintAxes(java.awt.Graphics g)
Paints the coordinate axes.

Parameters:
g - The graphics context.

paintData

private void paintData(java.awt.Graphics g)
Paints the data series.

Parameters:
g - The graphics context.

computeScreenData

private void computeScreenData()
Computes the screen coordinates for the visible data series.


computeXCoordinates

private void computeXCoordinates()
Compute the x coordinates in data space according to the current settings.


computeScreenDataForSeries

private void computeScreenDataForSeries(int seriesIndex)
Compute the screen coordinates for the specified series.

Parameters:
seriesIndex - The cache index of the series to be computed.

highlightAround

public java.util.List<java.lang.Integer> highlightAround(java.awt.Point sp)
Highlights the points around the specified point. This is normally called when the mouse is moved over the plotter canvas.

Parameters:
sp - A marker screen point.
Returns:
A list of series indices on which at least one point was highlighted.

computeGridSteps

private void computeGridSteps()
Computes the actual grid mesh sizes taking in account the current plot size.


screenToDataPoint

public java.awt.geom.Point2D.Double screenToDataPoint(java.awt.Point sp)
Map the specified point in screen coordinates into the data space.

Parameters:
sp - A point in screen coordinates.
Returns:
The corresponding data point.

updateDatScrTransform

private void updateDatScrTransform()
Updates the data to screen transform map according to the currently visible data area and screen size.


resetScreenDataBuffer

private void resetScreenDataBuffer()
Reallocates the screen data buffer.


updateScreenData

private void updateScreenData()
First, recomputes the currently visible data area according to the current graph and series settings; then, computes the screen coordinates for the visible data series..


updateSeriesTransformer

private void updateSeriesTransformer(int seriesIndex)

resetDataViewport

private void resetDataViewport()
Recomputes the currently visible data area according to the current graph and series settings.


setScreenSize

public void setScreenSize(int width,
                          int height)
Set the current view screen size.

Parameters:
width - Canvas width in pixels.
height - Canvas height in pixels

getScreenWidth

public int getScreenWidth()
Gets canvas screen size (X).

Returns:
Canvas screen size (X).

getScreenHeight

public int getScreenHeight()
Gets canvas screen size (Y).

Returns:
Canvas screen size (Y).

cacheEventFired

public void cacheEventFired(DataCache cache,
                            CacheObserver.CacheEvent event)
If cached label info is changed, the screen buffer is recreated; if cached data is updated the view port and the screen data are recomputed.

Specified by:
cacheEventFired in interface CacheObserver

settingHasChanged

public void settingHasChanged(ObservableSettings settings,
                              java.lang.Object info)
Dispatches settings change events.

Specified by:
settingHasChanged in interface SettingsObserver
Parameters:
settings - The settings object that was changed.
info - Is used to further specify the change. Usually this is a String containing the name of the changed setting.

settingHasChanged

public void settingHasChanged(DataSeriesSettings settings,
                              java.lang.String info)
Calls the neccesary recoputations when seties settings are changed.

Parameters:
settings - Series settings.
info - Change event info.

settingHasChanged

public void settingHasChanged(GraphSettings settings,
                              java.lang.String info)
Calls the neccesary recoputations when graph settings are changed.

Parameters:
settings - Graph settings.
info - Change event info.

LiveGraph
data visualisation and analysis framework