Class StripChartCurve

java.lang.Object
edu.cnu.mdi.splot.pdata.ACurve
edu.cnu.mdi.splot.pdata.StripChartCurve

public class StripChartCurve extends ACurve
Backing data model for a strip chart / time-series plot.

This class periodically samples a provided Evaluator and appends (t, y) points into bounded X and Y columns. When capacity is reached, the oldest samples are dropped ("scrolls off the left").

Threading

Sampling runs on a background scheduler thread. To preserve the ACurve contract that notifications occur on the Swing Event Dispatch Thread (EDT), this class uses a lock-free staging queue:

  • The background sampler thread computes samples and enqueues them.
  • An EDT drain pass applies samples in bounded batches under ACurve.lock.
  • A single consolidated ACurve.markDataChanged() is fired per batch (EDT-only).

The optional setOnSample(Runnable) callback, if provided, is invoked on the EDT after each drain pass. This is a convenient place to call repaint() safely.

  • Constructor Details

    • StripChartCurve

      public StripChartCurve(String name, int capacity, Evaluator accumulator, long intervalMs)
      Create strip-chart data.
      Parameters:
      name - series name
      capacity - max number of retained samples (>= 2)
      accumulator - value source; called as accumulator.value(tMs) (non-null)
      intervalMs - update interval in milliseconds (> 0)
  • Method Details

    • getCapacity

      public int getCapacity()
      Returns:
      current capacity (max samples retained).
    • setCapacity

      public void setCapacity(int capacity)
      Set the capacity (max samples retained). If the current size exceeds the new capacity, oldest samples are dropped immediately.

      If called off the EDT, this method schedules the capacity enforcement on the EDT to keep column mutation and notifications Swing-correct.

      Parameters:
      capacity - must be >= 2
    • getIntervalMs

      public long getIntervalMs()
      Returns:
      sampling interval in milliseconds.
    • getTimeUnit

      public TimeUnit getTimeUnit()
      Returns:
      the current display unit for time on the x-axis.
    • setTimeUnit

      public void setTimeUnit(TimeUnit unit)
      Set the display unit for the time axis.

      Internal storage remains milliseconds; snapshot/min/max are scaled. This triggers a data change notification so axes/ticks update.

      Parameters:
      unit - non-null
    • getTimeUnitShortLabel

      public String getTimeUnitShortLabel()
      A short label you can use in axis titles ("Time (s)", etc.).
    • setIntervalMs

      public void setIntervalMs(long intervalMs)
      Set the sampling interval. If currently running, restarts the schedule.
      Parameters:
      intervalMs - must be > 0
    • setOnSample

      public void setOnSample(Runnable onSample)
      Set a callback to be invoked after each drain pass on the EDT.

      This is a convenient place to call repaint() without additional SwingUtilities.invokeLater(...).

      Parameters:
      onSample - may be null
    • isRunning

      public boolean isRunning()
      Returns:
      true if actively sampling.
    • start

      public void start()
      Start sampling. Safe to call multiple times; subsequent calls do nothing if already running.
    • stop

      public void stop()
      Stop sampling. Safe to call multiple times.

      This cancels the scheduled task but keeps the scheduler alive so you can start() again.

    • shutdown

      public void shutdown()
      Permanently shut down this StripChartCurve (cannot be restarted). Use when the owning plot is being disposed.
    • clearData

      public void clearData()
      Clear all retained samples.

      Thread-safe: if called off-EDT, schedules the clear on the EDT. Pending queued samples are also discarded so the curve becomes truly empty.

      Specified by:
      clearData in class ACurve
    • snapshot

      public Snapshot snapshot()
      Obtain a consistent snapshot of the current data, suitable for plotting without locking.
      Specified by:
      snapshot in class ACurve
      Returns:
      snapshot containing primitive arrays of x and y data. Those arrays are copies of the internal data at the moment of the snapshot.
    • add

      public void add(double tms, double y)
      Add an explicit sample (t, y) to this strip chart.

      Thread-safe: may be called from any thread. Off-EDT calls enqueue and schedule a coalesced EDT drain. Notifications remain EDT-only per ACurve.

      Parameters:
      tms - time value (typically ms since start)
      y - sample value
    • doFit

      public void doFit(boolean force)
      Perform a curve computation (fit or spline) on the strip chart's current data, depending on the configured CurveDrawingMethod.

      Same dispatch as Curve.doFit(boolean): CUBICSPLINE builds a CubicSpline, the parametric methods (POLYNOMIAL, ERF, ERFC, GAUSSIAN, GAUSSIANS, EXPONENTIAL_DECAY, POWER_LAW, LORENTZIAN) run the corresponding IFitter, and NONE/CONNECT/STAIRS require no computed artifact. Must be called on the EDT.

      Specified by:
      doFit in class ACurve
      Parameters:
      force - true to force recomputation even if not dirty
    • length

      public int length()
      Description copied from class: ACurve
      Get curve length (number of points or effective points in the data columns).
      Specified by:
      length in class ACurve
      Returns:
      curve length
    • xMin

      public double xMin()
      Specified by:
      xMin in class ACurve
      Returns:
      the minimum x value for this curve
    • xMax

      public double xMax()
      Specified by:
      xMax in class ACurve
      Returns:
      the maximum x value for this curve
    • yMin

      public double yMin()
      Specified by:
      yMin in class ACurve
      Returns:
      the minimum y value for this curve
    • yMax

      public double yMax()
      Specified by:
      yMax in class ACurve
      Returns:
      the maximum y value for this curve