Class PlotCanvas

All Implemented Interfaces:
DataChangeListener, ImageObserver, MenuContainer, Serializable, EventListener

public class PlotCanvas extends JComponent implements DataChangeListener
The primary Swing component for rendering a 2-D scientific plot.

Responsibilities

Mouse handling

All mouse interaction (legend dragging, feedback readout, scroll-wheel zoom) is delegated to PlotMouseHandler, which is wired up in the constructor. PlotCanvas no longer implements any mouse listener interfaces directly.

Coordinate systems

Three coordinate spaces are in play:

  1. Screen (local) — Swing pixel coordinates, (0,0) at top-left.
  2. Raw world — stored in _worldSystem; log₁₀(data) when a log axis is active.
  3. Data — the real values in the curves, always linear.

Conversion methods are named screen*, *RawWorld*, and *Data* to make the direction explicit.

Author:
heddle
See Also:
  • Field Details

    • DONEDRAWINGPROP

      public static final String DONEDRAWINGPROP
      Fired after every completed paint pass.
      See Also:
    • TITLETEXTCHANGE

      public static final String TITLETEXTCHANGE
      Fired when the plot title text changes.
      See Also:
    • TITLEFONTCHANGE

      public static final String TITLEFONTCHANGE
      Fired when the plot title font changes.
      See Also:
    • XLABELTEXTCHANGE

      public static final String XLABELTEXTCHANGE
      Fired when the x-axis label text changes.
      See Also:
    • YLABELTEXTCHANGE

      public static final String YLABELTEXTCHANGE
      Fired when the y-axis label text changes.
      See Also:
    • AXESFONTCHANGE

      public static final String AXESFONTCHANGE
      Fired when the axes font changes.
      See Also:
    • STATUSFONTCHANGE

      public static final String STATUSFONTCHANGE
      Fired when the status/feedback font changes.
      See Also:
    • LOGZCHANGE

      public static final String LOGZCHANGE
      Fired when log-Z mode changes (2D histograms).
      See Also:
    • COLORMAPCHANGE

      public static final String COLORMAPCHANGE
      Fired when the color map changes (2D histograms / heatmaps).
      See Also:
    • _screenToRawWorld

      protected AffineTransform _screenToRawWorld
      Transforms screen → raw-world (null when component has no valid size).
    • _rawWorldToScreen

      protected AffineTransform _rawWorldToScreen
      Transforms raw-world → screen (null when component has no valid size).
    • _plotData

      protected PlotData _plotData
      The data backing this canvas.
    • _parameters

      protected PlotParameters _parameters
      Visual and numerical parameters (axis labels, fonts, log scale, etc.).
  • Constructor Details

    • PlotCanvas

      public PlotCanvas(PlotData plotData, String plotTitle, String xLabel, String yLabel)
      Creates a PlotCanvas for the given dataset.
      Parameters:
      plotData - the initial dataset (null → empty XYXY data)
      plotTitle - plot title (may be null)
      xLabel - x-axis label (may be null)
      yLabel - y-axis label (may be null)
  • Method Details

    • removeNotify

      public void removeNotify()
      Overrides:
      removeNotify in class JComponent
    • standUp

      public void standUp()
      Starts the timer and notifies listeners that the canvas is active.
    • shutDown

      public void shutDown()
      Stops the timer and notifies listeners that the canvas is shutting down.
    • setPlotData

      protected void setPlotData(PlotData plotData)
      Replaces the dataset and recomputes the world system.
      Parameters:
      plotData - the new dataset (null → empty XYXY data)
    • getPlotData

      public PlotData getPlotData()
      Returns the dataset backing this canvas.
      Returns:
      the current PlotData (never null after construction)
    • clearData

      public void clearData()
      Clears all data from every curve.
      See Also:
    • getType

      public PlotDataType getType()
      Returns the current PlotDataType.
      Returns:
      data type of the current dataset
    • getParameters

      public PlotParameters getParameters()
      Returns the plot parameters object.
      Returns:
      current PlotParameters
    • getTitle

      public String getTitle()
      Returns the plot title.
      Returns:
      title string (may be null)
    • isBarPlot

      public boolean isBarPlot()
      Returns true if this canvas is configured as a bar plot.
      Returns:
      true if the RenderHint.BARPLOT hint is set
    • setParent

      public void setParent(Component parent)
      Sets the optional parent component (usually a PlotPanel).
      Parameters:
      parent - parent component, or null
    • setToolBar

      public void setToolBar(BaseToolBar toolBar)
      Sets the toolbar that owns this canvas.
      Parameters:
      toolBar - the toolbar, or null if there is no toolbar
    • showPreferencesEditor

      public void showPreferencesEditor()
      Opens the plot preferences dialog.
    • getWorld

      public Rectangle2D.Double getWorld()
      Returns the visible region in raw-world coordinates.

      When log axes are active, this rectangle is in log₁₀(data) space.

      Returns:
      the world rectangle (never null after construction)
    • getActiveBounds

      public Rectangle getActiveBounds()
      Returns the pixel bounds of the active (data) area within the component.
      Returns:
      active bounds, or null if the component has no size yet
    • isXLogActive

      public boolean isXLogActive()
      Returns true if log-10 scaling is active on the x-axis.
      Returns:
      true when x log-scaling is both requested and valid
    • isYLogActive

      public boolean isYLogActive()
      Returns true if log-10 scaling is active on the y-axis.
      Returns:
      true when y log-scaling is both requested and valid
    • getPlotTicks

      public PlotTicks getPlotTicks()
      Returns the tick-mark renderer for this canvas.
      Returns:
      the PlotTicks (never null after construction)
    • plotPanel

      public PlotPanel plotPanel()
      Returns the PlotPanel that contains this canvas, if any.
      Returns:
      the parent PlotPanel, or null
    • needsRedraw

      public void needsRedraw(boolean rescale)
      Signals that this canvas needs a repaint, optionally requesting a world rescale. Safe to call from any thread; the actual work happens on the next timer tick.
      Parameters:
      rescale - if true, the world system is recomputed before repainting
    • setWorldSystem

      public void setWorldSystem()
      Recomputes the world system from the current data and plot parameters.

      Handles manual limits, algorithmic (NiceScale) limits, and log-axis bounds. Activates log scaling only when the final data range is strictly positive.

    • getDataWorld

      public Rectangle2D.Double getDataWorld()
      Returns the visible region in data (linear) coordinates.
      Returns:
      data-space rectangle, or null if the world system is uninitialised
    • paintComponent

      public void paintComponent(Graphics g)
      Paints the canvas: background, data, frame, ticks, legend, extra text. Fires DONEDRAWINGPROP when complete.
      Overrides:
      paintComponent in class JComponent
      Parameters:
      g - graphics context
    • zoomToRect

      public void zoomToRect(Rectangle rbrect)
      Zooms to a rubber-band rectangle specified in screen (pixel) coordinates. Rectangles smaller than 15×15 pixels are ignored to prevent accidental micro-zooms.
      Parameters:
      rbrect - rubber-band rectangle in screen coordinates
    • scale

      public void scale(double amount)
      Scales the visible area by amount around its center. Values greater than 1 zoom out; values less than 1 zoom in.
      Parameters:
      amount - scale factor (> 0)
    • addPlotChangeListener

      public void addPlotChangeListener(PlotChangeListener listener)
      Registers a PlotChangeListener for lifecycle events. Duplicate registrations are silently de-duplicated.
      Parameters:
      listener - the listener to add (non-null)
    • removePlotChangeListener

      public void removePlotChangeListener(PlotChangeListener listener)
      Removes a previously registered PlotChangeListener.
      Parameters:
      listener - the listener to remove (ignored if null or not registered)
    • remoteFirePropertyChange

      public void remoteFirePropertyChange(String propName, Object oldValue, Object newValue)
      Triggers a property-change event on behalf of another object (e.g. a sub-component that cannot fire its own property changes).
      Parameters:
      propName - property name
      oldValue - old value
      newValue - new value
    • dataSetChanged

      public void dataSetChanged(PlotData plotData, ACurve curve, CurveChangeType type)
      Called by the data model when curve data changes. Schedules a rescaled redraw.
      Specified by:
      dataSetChanged in interface DataChangeListener
      Parameters:
      plotData - the plot data that changed
      curve - the specific curve that changed
      type - the type of change
    • screenToRawWorld

      public void screenToRawWorld(Point screenPoint, Point2D.Double rawPoint)
      Converts a screen (pixel) point to raw-world coordinates.

      Raw world is the coordinate system of _worldSystem. When log axes are active, raw world is in log₁₀(data) space. Use this for rubber-band zoom operations.

      Parameters:
      screenPoint - screen point in component coordinates (non-null)
      rawPoint - output point filled with raw-world coordinates (non-null)
      Throws:
      IllegalArgumentException - if either argument is null
    • screenToRawWorld

      public void screenToRawWorld(Rectangle screenRect, Rectangle2D.Double rawRect)
      Converts a screen (pixel) rectangle to a raw-world rectangle.

      Use this for rubber-band zoom: the result can be assigned directly into _worldSystem.

      Parameters:
      screenRect - screen rectangle in component coordinates
      rawRect - output rectangle in raw-world coordinates (non-null)
      Throws:
      IllegalArgumentException - if rawRect is null
    • screenToData

      public void screenToData(Point screenPoint, Point2D.Double dataPoint)
      Converts a screen (pixel) point to data coordinates (always linear units).

      Use this for mouse readouts and hit-tests in data space.

      Parameters:
      screenPoint - screen point in component coordinates
      dataPoint - output point in data coordinates (non-null)
      Throws:
      IllegalArgumentException - if dataPoint is null
    • screenToData

      public void screenToData(Rectangle screenRect, Rectangle2D.Double dataRect)
      Converts a screen rectangle to data-space bounds (always linear units).

      Do not use this to update _worldSystem; for zoom you want raw-world coordinates.

      Parameters:
      screenRect - screen rectangle in component coordinates
      dataRect - output rectangle in data coordinates (non-null)
      Throws:
      IllegalArgumentException - if dataRect is null
    • dataToScreen

      public boolean dataToScreen(Point screenPt, Point2D.Double dataPoint)
      Converts a data point (linear units) to screen (pixel) coordinates.

      Handles log-axis conversion internally (data → log₁₀ → raw-world → screen).

      Parameters:
      screenPt - output screen point (non-null)
      dataPoint - input data point in linear units (non-null)
      Returns:
      true on success; false if the point is invalid for the current axes (e.g. non-positive on a log axis) or transforms are unavailable
    • dataToScreen

      public boolean dataToScreen(Rectangle screenRect, Rectangle2D.Double dataRect)
      Converts a data rectangle (linear units) to a screen rectangle.
      Parameters:
      screenRect - output screen rectangle (non-null)
      dataRect - input data rectangle in linear units (non-null)
      Returns:
      true on success; false if any corner is invalid for the current axes or transforms are unavailable
    • dataToScreen

      public Rectangle dataToScreen(double dataX0, double dataY0, double dataX1, double dataY1)
      Converts a data rectangle specified by two corners to a screen rectangle.
      Parameters:
      dataX0 - data-space x₀ (linear)
      dataY0 - data-space y₀ (linear)
      dataX1 - data-space x₁ (linear)
      dataY1 - data-space y₁ (linear)
      Returns:
      screen rectangle, or null if any corner is invalid
    • dataToScreen

      public Point dataToScreen(double dataX, double dataY)
      Converts a single data point to a screen point.
      Parameters:
      dataX - data-space x (linear)
      dataY - data-space y (linear)
      Returns:
      screen point, or null if the point is invalid for the current axes
    • setAffineTransforms

      protected void setAffineTransforms()
      Rebuilds the affine transforms from the current world system and active bounds.