Class CircularChartModel

java.lang.Object
com.arbergashi.charts.model.CircularChartModel
All Implemented Interfaces:
ChartModel

public final class CircularChartModel extends Object implements ChartModel
Fixed-capacity ring-buffer model optimized for realtime streams.

Threading contract: readers are lock-free and use per-slot sequencing to ensure consistent field reads. Writers are serialized by an internal spin lock so multiple writer threads are safe but will be ordered. For highest throughput, prefer a single writer.

Atomic reads: use readPoint(int, double[]) when you need a consistent snapshot of x/y/min/max/weight. Individual getters are safe but may observe different points if called separately.

Capacity is rounded to the next power-of-two to enable fast index masking.

  • Constructor Details

    • CircularChartModel

      public CircularChartModel(int capacity)
    • CircularChartModel

      public CircularChartModel(String name, int capacity)
  • Method Details

    • getCapacity

      public int getCapacity()
    • setLabelsEnabled

      public CircularChartModel setLabelsEnabled(boolean enabled)
    • getPointCount

      public int getPointCount()
      Description copied from interface: ChartModel
      Returns the number of data points.
      Specified by:
      getPointCount in interface ChartModel
      Returns:
      count
    • getX

      public double getX(int index)
      Description copied from interface: ChartModel
      Returns the X value at the given index.
      Specified by:
      getX in interface ChartModel
      Parameters:
      index - data point index
      Returns:
      X value or 0.0 if out of range
    • getY

      public double getY(int index)
      Description copied from interface: ChartModel
      Returns the Y value at the given index.
      Specified by:
      getY in interface ChartModel
      Parameters:
      index - data point index
      Returns:
      Y value or 0.0 if out of range
    • getMin

      public double getMin(int index)
      Description copied from interface: ChartModel
      Optional per-point minimum value. Default: fallback to the Y value for that index.
      Specified by:
      getMin in interface ChartModel
    • getMax

      public double getMax(int index)
      Description copied from interface: ChartModel
      Optional per-point maximum value. Default: fallback to the Y value for that index.
      Specified by:
      getMax in interface ChartModel
    • getWeight

      public double getWeight(int index)
      Description copied from interface: ChartModel
      Returns the weight value at the given index.
      Specified by:
      getWeight in interface ChartModel
      Parameters:
      index - data point index
      Returns:
      weight value or 0.0 if out of range
    • getValue

      public double getValue(int index, int component)
      Description copied from interface: ChartModel
      Generic multi-component accessor used by statistical/financial renderers. Default: fallback to `getY(index)`; implementors may override for boxplot/candlestick semantics.
      Specified by:
      getValue in interface ChartModel
    • getXData

      public double[] getXData()
      Description copied from interface: ChartModel
      Direct access to the X-coordinate array.

      Framework contract: Implementations may return either:

      Consumers must never assume getXData().length == getPointCount(). Always bound iteration by the logical size:

      int n = Math.min(model.getPointCount(), model.getXData().length);
      

      Performance note: If you need strict size and immutability semantics, use a model implementation that returns copies (e.g. DefaultChartModel).

      Specified by:
      getXData in interface ChartModel
      Returns:
      X values array (may be larger than logical size)
    • getYData

      public double[] getYData()
      Description copied from interface: ChartModel
      Direct access to the Y-coordinate array.

      Warning: For performance reasons, the internal array is often returned. Do not modify!

      Specified by:
      getYData in interface ChartModel
      Returns:
      double array of Y values
    • getLowData

      public double[] getLowData()
      Description copied from interface: ChartModel
      Direct access to low values (for financial charts). Default: Fallback to Y-data.
      Specified by:
      getLowData in interface ChartModel
    • getHighData

      public double[] getHighData()
      Description copied from interface: ChartModel
      Direct access to high values (for financial charts). Default: Fallback to Y-data.
      Specified by:
      getHighData in interface ChartModel
    • getWeightData

      public double[] getWeightData()
      Description copied from interface: ChartModel
      Optional weight array for pie/donut-like renderers. Default: fallback to Y-data. Renderers should prefer this primitive API for zero-allocation rendering.
      Specified by:
      getWeightData in interface ChartModel
    • getLabel

      public String getLabel(int index)
      Description copied from interface: ChartModel
      Returns a label for a specific index (optional).
      Specified by:
      getLabel in interface ChartModel
      Parameters:
      index - Index of the point
      Returns:
      Label or null
    • getDataRange

      public double[] getDataRange()
      Description copied from interface: ChartModel
      Returns the {minX, maxX, minY, maxY} data range based on available X/Y arrays.
      Specified by:
      getDataRange in interface ChartModel
      Returns:
      range array or {0,0,0,0} if data is empty
    • getName

      public String getName()
      Description copied from interface: ChartModel
      Returns the title of the data series.
      Specified by:
      getName in interface ChartModel
      Returns:
      Name of the series
    • setName

      public void setName(String name)
    • getSubtitle

      public String getSubtitle()
    • setSubtitle

      public void setSubtitle(String subtitle)
    • getColor

      public Color getColor()
      Description copied from interface: ChartModel
      Optional visual color for a series; default is null.
      Specified by:
      getColor in interface ChartModel
    • setColor

      public void setColor(Color color)
      Description copied from interface: ChartModel
      Optional setter for a visual color; default no-op.
      Specified by:
      setColor in interface ChartModel
    • setDispatchOnEdt

      public CircularChartModel setDispatchOnEdt(boolean enabled)
    • addChangeListener

      public void addChangeListener(ChartModel.ChartModelListener listener)
      Description copied from interface: ChartModel
      Registers a listener for data changes.
      Specified by:
      addChangeListener in interface ChartModel
      Parameters:
      listener - The listener
    • removeChangeListener

      public void removeChangeListener(ChartModel.ChartModelListener listener)
      Description copied from interface: ChartModel
      Removes a listener.
      Specified by:
      removeChangeListener in interface ChartModel
      Parameters:
      listener - The listener
    • getUpdateStamp

      public long getUpdateStamp()
      Description copied from interface: ChartModel
      Optional timestamp for model updates; default 0 for simple models.
      Specified by:
      getUpdateStamp in interface ChartModel
    • clear

      public void clear()
    • addPoint

      public void addPoint(ChartPoint p)
    • addPoint

      public void addPoint(double x, double y, double weight, String label)
    • addPoint

      public void addPoint(double x, double y, double min, double max, double weight, String label)
    • addXY

      public void addXY(double x, double y)
    • addXY

      public void addXY(double x, double y, String label)
    • addOHLC

      public void addOHLC(OHLCBar bar)
    • addOHLC

      public void addOHLC(double time, double open, double high, double low, double close)
    • addWithError

      public void addWithError(ErrorBarPoint point)
    • addWithError

      public void addWithError(double x, double y, double error)
    • addWithError

      public void addWithError(double x, double y, double errorLow, double errorHigh)
    • readPoint

      public boolean readPoint(int index, double[] buffer)
      Reads a stable point into the provided buffer.

      This method uses a per-slot sequence guard to ensure all fields are from the same write. For concurrent usage, prefer this method over calling getX/getY/getMin/getMax individually.

      Buffer order: x, y, min, max, weight.

      Parameters:
      index - point index in the logical series
      buffer - output buffer with length >= 5
      Returns:
      true when a stable point was read