Class NiceScale

java.lang.Object
com.arbergashi.charts.util.NiceScale

public class NiceScale extends Object
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 Classes
    Modifier and Type
    Class
    Description
    static enum 
    Supported scale types for axes produced by NiceScale.
  • Constructor Summary

    Constructors
    Constructor
    Description
    NiceScale(double min, double max)
    Create a NiceScale for the provided data range.
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    Get the largest "nice" value that bounds the data maximum.
    double
    Get the smallest "nice" value that bounds the data minimum.
    double[]
    Returns an array of tick positions from getNiceMin() to getNiceMax() spaced by getTickSpacing().
    double
    Return the computed tick spacing (distance between adjacent ticks).
    void
    setMaxTicks(double maxTicks)
    Set the maximum number of ticks (approximate) desired on the axis.
    void
    setRange(double min, double max)
    Update the data range and recalculate tick spacing and nice bounds.

    Methods inherited from class Object

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

    • NiceScale

      public NiceScale(double min, double max)
      Create a NiceScale for the provided data range.
      Parameters:
      min - the minimum data value
      max - 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 value
      max - 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 positions from getNiceMin() to getNiceMax() spaced by getTickSpacing().
      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.