Class DefaultChartModel
- All Implemented Interfaces:
ChartModel
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
-
Nested Class Summary
Nested classes/interfaces inherited from interface ChartModel
ChartModel.ChartModelListener -
Field Summary
Fields inherited from interface ChartModel
EMPTY_DOUBLE -
Constructor Summary
ConstructorsConstructorDescriptionDefaultChartModel(String name) Creates a model with a custom series name.DefaultChartModel(String name, Color color) Creates a model with a custom series name and color. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAll(List<ChartPoint> points) Add multiple chart points in bulk.voidRegisters a listener for data changes.voidaddOHLC(double time, double open, double high, double low, double close) Add OHLC bar data from parameters.voidAdd OHLC bar data.voidAdds a point with explicit min/max, weight, and label.voidAdds a point with weight and label.voidAdds a chart point and invalidates the model.voidaddPoints(List<ChartPoint> pts) Adds multiple points from a list.voidaddWithError(double x, double y, double error) Add error bar point with symmetric error.voidaddWithError(double x, double y, double errorLow, double errorHigh) Add error bar point with asymmetric error.voidaddWithError(ErrorBarPoint point) Add error bar point data.voidaddXY(double x, double y) Convenience method to add a simple XY point.voidConvenience method to add a simple XY point with label.voidaddXYArrays(double[] x, double[] y) Add XY data from parallel arrays.voidclear()Clears all points while keeping internal buffers allocated.protected voidgetColor()Returns the series color override.double[]Returns the {minX, maxX, minY, maxY} data range based on available X/Y arrays.double[]Direct access to high values (for financial charts).getLabel(int index) Returns a label for a specific index (optional).double[]Direct access to low values (for financial charts).doublegetMax(int index) Optional per-point maximum value.double[]Returns a copy of the max (high) data array.doublegetMin(int index) Optional per-point minimum value.double[]Returns a copy of the min (low) data array.getName()Returns the title of the data series.intReturns the number of data points.Returns the optional subtitle.longReturns the update stamp incremented on each modification.doublegetValue(int index, int component) Returns a component by index for multi-component renderers.doublegetWeight(int index) Returns the weight value at the given index.double[]Optional weight array for pie/donut-like renderers.doublegetX(int index) Returns the X value at the given index.double[]getXData()Direct access to the X-coordinate array.doublegetY(int index) Returns the Y value at the given index.double[]getYData()Direct access to the Y-coordinate array.voidRemoves a listener.voidSets a series color override.setDispatchOnEdt(boolean enabled) Controls whether change listeners are notified on the Swing EDT.voidSets the series name.voidsetPoints(List<ChartPoint> pts) Replaces all points with the provided list.voidsetSubtitle(String subtitle) Sets the subtitle.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ChartModel
getOpenData, getY, isEmpty
-
Constructor Details
-
DefaultChartModel
public DefaultChartModel() -
DefaultChartModel
Creates a model with a custom series name.- Parameters:
name- series name
-
DefaultChartModel
-
-
Method Details
-
getPointCount
public int getPointCount()Description copied from interface:ChartModelReturns the number of data points.- Specified by:
getPointCountin interfaceChartModel- Returns:
- count
-
getX
public double getX(int index) Returns the X value at the given index.- Specified by:
getXin interfaceChartModel- Parameters:
index- point index- Returns:
- X value
-
getY
public double getY(int index) Returns the Y value at the given index.- Specified by:
getYin interfaceChartModel- Parameters:
index- point index- Returns:
- Y value
-
getMin
public double getMin(int index) Description copied from interface:ChartModelOptional per-point minimum value. Default: fallback to the Y value for that index.- Specified by:
getMinin interfaceChartModel
-
getMax
public double getMax(int index) Description copied from interface:ChartModelOptional per-point maximum value. Default: fallback to the Y value for that index.- Specified by:
getMaxin interfaceChartModel
-
getWeight
public double getWeight(int index) Description copied from interface:ChartModelReturns the weight value at the given index.- Specified by:
getWeightin interfaceChartModel- 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:
getValuein interfaceChartModel- Parameters:
index- data indexcomponent- component selector- Returns:
- component value
-
getUpdateStamp
public long getUpdateStamp()Returns the update stamp incremented on each modification.- Specified by:
getUpdateStampin interfaceChartModel- Returns:
- update stamp
-
getXData
public double[] getXData()Description copied from interface:ChartModelDirect access to the X-coordinate array.Framework contract: Implementations may return either:
- a defensive copy sized to
ChartModel.getPointCount()(safe, but potentially slower), or - a backing array with a capacity larger than
ChartModel.getPointCount()(fast, zero-allocation).
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:
getXDatain interfaceChartModel- Returns:
- X values array (may be larger than logical size)
- a defensive copy sized to
-
getYData
public double[] getYData()Description copied from interface:ChartModelDirect access to the Y-coordinate array.Warning: For performance reasons, the internal array is often returned. Do not modify!
- Specified by:
getYDatain interfaceChartModel- Returns:
- double array of Y values
-
getLowData
public double[] getLowData()Description copied from interface:ChartModelDirect access to low values (for financial charts). Default: Fallback to Y-data.- Specified by:
getLowDatain interfaceChartModel
-
getHighData
public double[] getHighData()Description copied from interface:ChartModelDirect access to high values (for financial charts). Default: Fallback to Y-data.- Specified by:
getHighDatain interfaceChartModel
-
getWeightData
public double[] getWeightData()Description copied from interface:ChartModelOptional weight array for pie/donut-like renderers. Default: fallback to Y-data. Renderers should prefer this primitive API for zero-allocation rendering.- Specified by:
getWeightDatain interfaceChartModel
-
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
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
Description copied from interface:ChartModelReturns the title of the data series.- Specified by:
getNamein interfaceChartModel- Returns:
- Name of the series
-
setName
-
getSubtitle
-
setSubtitle
-
getColor
Returns the series color override.- Specified by:
getColorin interfaceChartModel- Returns:
- series color or null to use theme defaults
-
setColor
Sets a series color override.- Specified by:
setColorin interfaceChartModel- Parameters:
color- series color (null resets)
-
setDispatchOnEdt
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
Description copied from interface:ChartModelRegisters a listener for data changes.- Specified by:
addChangeListenerin interfaceChartModel- Parameters:
l- The listener
-
removeChangeListener
Description copied from interface:ChartModelRemoves a listener.- Specified by:
removeChangeListenerin interfaceChartModel- Parameters:
l- The listener
-
fireModelChanged
protected void fireModelChanged() -
addPoints
Adds multiple points from a list.- Parameters:
pts- points to add
-
setPoints
Replaces all points with the provided list.- Parameters:
pts- new points
-
addPoint
Adds a point with weight and label.- Parameters:
x- X valuey- Y valueweight- weight valuelabel- label text
-
addPoint
Adds a point with explicit min/max, weight, and label.- Parameters:
x- X valuey- Y valuemin- min valuemax- max valueweight- weight valuelabel- label text
-
addXY
public void addXY(double x, double y) Convenience method to add a simple XY point.- Parameters:
x- X valuey- Y value
-
addXY
Convenience method to add a simple XY point with label.- Parameters:
x- X valuey- Y valuelabel- Label
-
addAll
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 valuesy- Array of Y values
-
addOHLC
-
addOHLC
public void addOHLC(double time, double open, double high, double low, double close) Add OHLC bar data from parameters.- Parameters:
time- Time/X valueopen- Open pricehigh- High pricelow- Low priceclose- Close price
-
addWithError
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 valuey- Y valueerror- Symmetric error (±)
-
addWithError
public void addWithError(double x, double y, double errorLow, double errorHigh) Add error bar point with asymmetric error.- Parameters:
x- X valuey- Y valueerrorLow- Lower error bounderrorHigh- Upper error bound
-
getDataRange
public double[] getDataRange()Description copied from interface:ChartModelReturns the {minX, maxX, minY, maxY} data range based on available X/Y arrays.- Specified by:
getDataRangein interfaceChartModel- Returns:
- range array or {0,0,0,0} if data is empty
-
getLabel
Description copied from interface:ChartModelReturns a label for a specific index (optional).- Specified by:
getLabelin interfaceChartModel- Parameters:
index- Index of the point- Returns:
- Label or null
-