Class NiceScale
java.lang.Object
com.arbergashi.charts.util.NiceScale
Calculates "nice" numbers for graph axes.
This class computes a "nice" range and tick spacing for plotting numeric axes. It expands the supplied data range to round, human-friendly values and computes evenly spaced tick marks (for example: 0, 10, 20 instead of 0, 17.5, 35).
Usage example:
NiceScale ns = new NiceScale(minValue, maxValue); double[] ticks = ns.getTicks();The algorithm is based on selecting a "nice" fraction (1, 2, 5, 10) scaled by a power of ten to produce round tick spacings.
- Since:
- 2025-06-01
- Version:
- 1.0.0
- Author:
- Arber Gashi
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumSupported scale types for axes produced by NiceScale. -
Constructor Summary
ConstructorsConstructorDescriptionNiceScale(double min, double max) Create a NiceScale for the provided data range. -
Method Summary
Modifier and TypeMethodDescriptiondoubleGet the largest "nice" value that bounds the data maximum.doubleGet the smallest "nice" value that bounds the data minimum.double[]getTicks()doubleReturn the computed tick spacing (distance between adjacent ticks).voidsetMaxTicks(double maxTicks) Set the maximum number of ticks (approximate) desired on the axis.voidsetRange(double min, double max) Update the data range and recalculate tick spacing and nice bounds.
-
Constructor Details
-
NiceScale
public NiceScale(double min, double max) Create a NiceScale for the provided data range.- Parameters:
min- the minimum data valuemax- the maximum data value
-
-
Method Details
-
setRange
public void setRange(double min, double max) Update the data range and recalculate tick spacing and nice bounds.- Parameters:
min- the new minimum data valuemax- the new maximum data value
-
setMaxTicks
public void setMaxTicks(double maxTicks) Set the maximum number of ticks (approximate) desired on the axis. The algorithm will attempt to return at most this many ticks; the exact number may be less or slightly different to preserve "nice" spacing.- Parameters:
maxTicks- maximum number of ticks to aim for (must be >= 2 for sensible results)
-
getTickSpacing
public double getTickSpacing()Return the computed tick spacing (distance between adjacent ticks).- Returns:
- spacing between ticks
-
getNiceMin
public double getNiceMin()Get the smallest "nice" value that bounds the data minimum.- Returns:
- lower bound (inclusive) of the nice range
-
getNiceMax
public double getNiceMax()Get the largest "nice" value that bounds the data maximum.- Returns:
- upper bound (inclusive) of the nice range
-
getTicks
public double[] getTicks()- Returns:
- an array of tick values. Returns an empty array if spacing is non-positive or if the computed number of ticks is non-positive.
-