Class AbstractPlot<S extends org.opentrafficsim.animation.graphs.AbstractPlot.PaintState>

java.lang.Object
org.opentrafficsim.animation.graphs.AbstractPlot<S>
Type Parameters:
S - paint state implementation
All Implemented Interfaces:
Identifiable, org.jfree.data.general.Dataset
Direct Known Subclasses:
AbstractBoundedPlot, DistributionPlotExtendedData

public abstract class AbstractPlot<S extends org.opentrafficsim.animation.graphs.AbstractPlot.PaintState> extends Object implements Identifiable, org.jfree.data.general.Dataset
Super class of all plots. This schedules regular updates, creates menus and deals with listeners. There are a number of methods for sub-classes to implement.

Copyright (c) 2013-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.

Author:
Alexander Verbraeck, Peter Knoppers, Wouter Schakel
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final org.djunits.value.vdouble.scalar.Duration
    Initial upper bound for the time scale.
    static final EventType
    The (regular, not timed) event type for pub/sub indicating the addition of a graph.
    static final EventType
    The (regular, not timed) event type for pub/sub indicating the removal of a graph.
  • Constructor Summary

    Constructors
    Constructor
    Description
    AbstractPlot(PlotScheduler scheduler, String caption, org.djunits.value.vdouble.scalar.Duration updateInterval, org.djunits.value.vdouble.scalar.Duration delay)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addChangeListener(org.jfree.data.general.DatasetChangeListener listener)
     
    protected abstract void
    calculatePaintState(org.djunits.value.vdouble.scalar.Duration time)
    Calculates the paint state object and offers it through offerPaintState(S), or delegates this work to a delegate.
    protected abstract S
    Returns an empty paint state.
    byte[]
    encodeAsPng(int width, int height, double fontSize)
    Returns the chart as a byte array representing a PNG image.
    org.djunits.value.vdouble.scalar.Duration
    Returns up to what time data is available for painting.
    Retrieve the caption.
    org.jfree.chart.JFreeChart
    Returns the chart.
    abstract GraphType
    Return the graph type for transceiver.
    final org.jfree.data.general.DatasetGroup
     
     
    protected S
    Returns the current paint state that should be used to return paint data (i.e. x-values, etc.)
    abstract String
    getStatusLabel(double domainValue, double rangeValue)
    Returns the status label when the mouse is over the given location.
    protected void
    Requests a calculation of the paint state.
    void
    Notify all change listeners.
    void
    offerPaintState(S paintState)
    Offer new paint state.
    void
    offerUpdateInterval(org.djunits.value.vdouble.scalar.Duration interval)
    Suggests a new update interval.
    void
    removeChangeListener(org.jfree.data.general.DatasetChangeListener listener)
     
    void
    setAutoBoundDomain(org.jfree.chart.plot.XYPlot plot)
    Overridable; activates auto bounds on domain axis from user input.
    void
    setAutoBoundRange(org.jfree.chart.plot.XYPlot plot)
    Overridable; activates auto bounds on range axis from user input.
    protected void
    setChart(org.jfree.chart.JFreeChart chart)
    Sets the chart and adds menus and listeners.
    final void
    setGroup(org.jfree.data.general.DatasetGroup group)
     
    protected void
    Sets the paint state in a thread safe manner and notifies the listeners.
    void
    Requests the worker thread to perform calculations up to the current time and (in the Swing EDT thread) update the plots.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • GRAPH_ADD_EVENT

      public static final EventType GRAPH_ADD_EVENT
      The (regular, not timed) event type for pub/sub indicating the addition of a graph. Not used internally.
      Payload: String graph caption (not an array, just a String)
    • GRAPH_REMOVE_EVENT

      public static final EventType GRAPH_REMOVE_EVENT
      The (regular, not timed) event type for pub/sub indicating the removal of a graph. Not used internally.
      Payload: String Graph caption (not an array, just a String)
    • DEFAULT_INITIAL_UPPER_TIME_BOUND

      public static final org.djunits.value.vdouble.scalar.Duration DEFAULT_INITIAL_UPPER_TIME_BOUND
      Initial upper bound for the time scale.
  • Constructor Details

    • AbstractPlot

      public AbstractPlot(PlotScheduler scheduler, String caption, org.djunits.value.vdouble.scalar.Duration updateInterval, org.djunits.value.vdouble.scalar.Duration delay)
      Constructor.
      Parameters:
      scheduler - scheduler
      caption - caption
      updateInterval - regular update interval (simulation time)
      delay - amount of time that chart runs behind simulation to prevent gaps in the charted data
  • Method Details

    • emptyPaintState

      protected abstract S emptyPaintState()
      Returns an empty paint state. This is used at plot initialization.
      Returns:
      empty paint state.
    • setChart

      protected void setChart(org.jfree.chart.JFreeChart chart)
      Sets the chart and adds menus and listeners.
      Parameters:
      chart - chart
    • encodeAsPng

      public byte[] encodeAsPng(int width, int height, double fontSize) throws IOException
      Returns the chart as a byte array representing a PNG image.
      Parameters:
      width - width
      height - height
      fontSize - font size (16 is the original on screen size)
      Returns:
      the chart as a byte array representing a PNG image
      Throws:
      IOException - on IO exception
    • getGroup

      public final org.jfree.data.general.DatasetGroup getGroup()
      Specified by:
      getGroup in interface org.jfree.data.general.Dataset
    • setGroup

      public final void setGroup(org.jfree.data.general.DatasetGroup group)
      Specified by:
      setGroup in interface org.jfree.data.general.Dataset
    • setAutoBoundDomain

      public void setAutoBoundDomain(org.jfree.chart.plot.XYPlot plot)
      Overridable; activates auto bounds on domain axis from user input. This class does not force the use of XYPlots, but the auto bounds command comes from the ChartPanel that shows this plot. In case the used plot is a XYPlot, this method is then invoked. Sub classes with auto domain bounds that work with an XYPlot should implement this. The method is not abstract as the use of XYPlot is not obligated.
      Parameters:
      plot - plot
    • setAutoBoundRange

      public void setAutoBoundRange(org.jfree.chart.plot.XYPlot plot)
      Overridable; activates auto bounds on range axis from user input. This class does not force the use of XYPlots, but the auto bounds command comes from the ChartPanel that shows this plot. In case the used plot is a XYPlot, this method is then invoked. Sub classes with auto range bounds that work with an XYPlot should implement this. The method is not abstract as the use of XYPlot is not obligated.
      Parameters:
      plot - plot
    • getGraphType

      public abstract GraphType getGraphType()
      Return the graph type for transceiver.
      Returns:
      the graph type.
    • getStatusLabel

      public abstract String getStatusLabel(double domainValue, double rangeValue)
      Returns the status label when the mouse is over the given location.
      Parameters:
      domainValue - domain value (x-axis)
      rangeValue - range value (y-axis)
      Returns:
      status label when the mouse is over the given location
    • getChart

      public org.jfree.chart.JFreeChart getChart()
      Returns the chart.
      Returns:
      chart
    • getId

      public String getId()
      Specified by:
      getId in interface Identifiable
    • getCaption

      public String getCaption()
      Retrieve the caption.
      Returns:
      the caption of the plot
    • addChangeListener

      public void addChangeListener(org.jfree.data.general.DatasetChangeListener listener)
      Specified by:
      addChangeListener in interface org.jfree.data.general.Dataset
    • removeChangeListener

      public void removeChangeListener(org.jfree.data.general.DatasetChangeListener listener)
      Specified by:
      removeChangeListener in interface org.jfree.data.general.Dataset
    • notifyPlotChange

      public void notifyPlotChange()
      Notify all change listeners.
    • invalidate

      protected void invalidate()
      Requests a calculation of the paint state. May be invoked by sub-classes whenever a setting was changed that needs a recalculation.
    • calculatePaintState

      protected abstract void calculatePaintState(org.djunits.value.vdouble.scalar.Duration time)
      Calculates the paint state object and offers it through offerPaintState(S), or delegates this work to a delegate. This method is invoked by the worker thread and can thus perform heavy calculations outside of the Swing EDT. Intermediate paint states during long calculations may also be offered. It is up to the implementation to either calculate a complete paint state, or cumulatively built on the results from previous calls. It is also up to the implementation to know when the whole time span needs to be recalculated due to property changes.
      Parameters:
      time - time until which data in the paint state should be calculated
    • offerPaintState

      public void offerPaintState(S paintState)
      Offer new paint state. This method can be invoked by any thread, and will make sure the actual setting of the paint state will occur on the Swing EDT. This assures that no paint state is changed as Swing is painting (i.e. as the plot is asked for data to paint). Listeners are notified on the Swing EDT as soon as the paint state has been set.
      Parameters:
      paintState - paint state
    • setPaintState

      protected void setPaintState()
      Sets the paint state in a thread safe manner and notifies the listeners. This method is always invoked on the Swing EDT. This method may be overridden to use a newly set paint state (after calling super.setPaintState()) to set internal properties. For example, setting the block size of an internal block renderer based on the granularity of the data.
    • getPaintState

      protected S getPaintState()
      Returns the current paint state that should be used to return paint data (i.e. x-values, etc.)
      Returns:
      current paint state
    • getAvailableTime

      public org.djunits.value.vdouble.scalar.Duration getAvailableTime()
      Returns up to what time data is available for painting.
      Returns:
      up to what time data is available for painting
    • offerUpdateInterval

      public void offerUpdateInterval(org.djunits.value.vdouble.scalar.Duration interval)
      Suggests a new update interval. This does not affect any time granularity, but only when update events occur. This method will ask the PlotScheduler to schedule an update now. The next update will set the update interval and schedule the next regular update aligning with the new interval. If an update is also desired right now invalidate() needs to be invoked. This method is typically called on the Swing EDT and will request the scheduler asynchronously. This method does not block.
      Parameters:
      interval - update interval
    • update

      public void update()
      Requests the worker thread to perform calculations up to the current time and (in the Swing EDT thread) update the plots. The worker thread will take on this request once any current calculations are done. If multiple updates are requested before the worker thread is done, only the update with latest time is executed. This method should only be invoked by the thread that governs time (typically by the PlotScheduler). Otherwise events may be erroneously scheduled in the past.

      After a new update interval was suggested through offerUpdateInterval(org.djunits.value.vdouble.scalar.Duration) this method does not do the above, but instead only schedules the next update aligning with the new interval.