Class DefaultChartModel

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

public class DefaultChartModel extends Object implements ChartModel
Default ChartModel implementation backed by primitive arrays.

This model is designed for fast ingestion and rendering. Internally it keeps growable primitive arrays for X/Y and optional metadata (min/max/weight/label). The logical size is reported by getPointCount().

Framework contract: This implementation returns defensive copies from getXData() and getYData() to keep consumer code safe from accidental mutation. For high-frequency ingestion scenarios, use the per-point accessors (getX(int), getY(int), getValue(int, int)) and avoid repeatedly requesting full arrays.

Since:
2025-06-01
Version:
1.0.0
Author:
Arber Gashi
  • Constructor Details

    • DefaultChartModel

      public DefaultChartModel()
    • DefaultChartModel

      public DefaultChartModel(String name)
      Creates a model with a custom series name.
      Parameters:
      name - series name
    • DefaultChartModel

      public DefaultChartModel(String name, Color color)
      Creates a model with a custom series name and color.
      Parameters:
      name - series name
      color - series color
  • Method Details

    • 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)
      Returns the X value at the given index.
      Specified by:
      getX in interface ChartModel
      Parameters:
      index - point index
      Returns:
      X value
    • getY

      public double getY(int index)
      Returns the Y value at the given index.
      Specified by:
      getY in interface ChartModel
      Parameters:
      index - point index
      Returns:
      Y value
    • 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)
      Returns a component by index for multi-component renderers.

      Component mapping: 0=x, 1=y, 2=weight, 3=min, 4=max.

      Specified by:
      getValue in interface ChartModel
      Parameters:
      index - data index
      component - component selector
      Returns:
      component value
    • getUpdateStamp

      public long getUpdateStamp()
      Returns the update stamp incremented on each modification.
      Specified by:
      getUpdateStamp in interface ChartModel
      Returns:
      update stamp
    • 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
    • getMinData

      public double[] getMinData()
      Returns a copy of the min (low) data array.
      Returns:
      min data array sized to point count
    • getMaxData

      public double[] getMaxData()
      Returns a copy of the max (high) data array.
      Returns:
      max data array sized to point count
    • addPoint

      public void addPoint(ChartPoint p)
      Adds a chart point and invalidates the model.
      Parameters:
      p - point to add
    • clear

      public void clear()
      Clears all points while keeping internal buffers allocated.
    • 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)
      Sets the series name.
      Parameters:
      name - series name
    • getSubtitle

      public String getSubtitle()
      Returns the optional subtitle.
      Returns:
      subtitle or null
    • setSubtitle

      public void setSubtitle(String subtitle)
      Sets the subtitle.
      Parameters:
      subtitle - subtitle or null
    • getColor

      public Color getColor()
      Returns the series color override.
      Specified by:
      getColor in interface ChartModel
      Returns:
      series color or null to use theme defaults
    • setColor

      public void setColor(Color color)
      Sets a series color override.
      Specified by:
      setColor in interface ChartModel
      Parameters:
      color - series color (null resets)
    • setDispatchOnEdt

      public DefaultChartModel setDispatchOnEdt(boolean enabled)
      Controls whether change listeners are notified on the Swing EDT.

      When enabled, background updates will dispatch listener notifications via EventQueue.invokeLater(Runnable). Default is disabled.

    • addChangeListener

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

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

      protected void fireModelChanged()
    • addPoints

      public void addPoints(List<ChartPoint> pts)
      Adds multiple points from a list.
      Parameters:
      pts - points to add
    • setPoints

      public void setPoints(List<ChartPoint> pts)
      Replaces all points with the provided list.
      Parameters:
      pts - new points
    • addPoint

      public void addPoint(double x, double y, double weight, String label)
      Adds a point with weight and label.
      Parameters:
      x - X value
      y - Y value
      weight - weight value
      label - label text
    • addPoint

      public void addPoint(double x, double y, double min, double max, double weight, String label)
      Adds a point with explicit min/max, weight, and label.
      Parameters:
      x - X value
      y - Y value
      min - min value
      max - max value
      weight - weight value
      label - label text
    • addXY

      public void addXY(double x, double y)
      Convenience method to add a simple XY point.
      Parameters:
      x - X value
      y - Y value
    • addXY

      public void addXY(double x, double y, String label)
      Convenience method to add a simple XY point with label.
      Parameters:
      x - X value
      y - Y value
      label - Label
    • addAll

      public void addAll(List<ChartPoint> points)
      Add multiple chart points in bulk.
      Parameters:
      points - List of chart points
    • addXYArrays

      public void addXYArrays(double[] x, double[] y)
      Add XY data from parallel arrays.
      Parameters:
      x - Array of X values
      y - Array of Y values
    • addOHLC

      public void addOHLC(OHLCBar bar)
      Add OHLC bar data.
      Parameters:
      bar - OHLC bar
    • addOHLC

      public void addOHLC(double time, double open, double high, double low, double close)
      Add OHLC bar data from parameters.
      Parameters:
      time - Time/X value
      open - Open price
      high - High price
      low - Low price
      close - Close price
    • addWithError

      public void addWithError(ErrorBarPoint point)
      Add error bar point data.
      Parameters:
      point - Error bar point
    • addWithError

      public void addWithError(double x, double y, double error)
      Add error bar point with symmetric error.
      Parameters:
      x - X value
      y - Y value
      error - Symmetric error (±)
    • addWithError

      public void addWithError(double x, double y, double errorLow, double errorHigh)
      Add error bar point with asymmetric error.
      Parameters:
      x - X value
      y - Y value
      errorLow - Lower error bound
      errorHigh - Upper error bound
    • 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
    • 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