Class Axis

java.lang.Object
com.codename1.ui.chart.axis.Axis
Direct Known Subclasses:
CategoryAxis, LogAxis, NumberAxis, SessionTimeAxis, TimeAxis

public abstract class Axis extends Object

Base of every chart axis. An axis owns two ranges: the data range the chart computes from its series, and the visible range the user pans and zooms inside it.

Labels are never laid out by hand: the chart asks the axis for as many ticks as the measured label size allows, so labels on a small screen thin out instead of overlapping.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Axis drawn below the plot.
    static final int
    Axis drawn left of the plot.
    static final int
    Axis drawn right of the plot.
    static final int
    Axis drawn above the plot.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Axis(int position)
    Creates an axis at the given position.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected double[]
    adjustBounds(double min, double max)
    Lets a subclass pad or round the automatic bounds; the default rounds a numeric range outward to the nearest nice step so the outermost grid lines sit on the plot edge.
    protected String
    defaultFormat(double value, double step)
    The axis' built in formatting; numbers are grouped and localized.
    protected double
    flatPad(double anchor)
    How far a range with a single value is padded on each side; 10% of the value, or 1 at zero.
    format(double value, double step)
    Formats a tick or value with the custom formatter if set, else the default.
    static String
    formatNumber(double value, int decimals)
    Formats value with exactly decimals decimal places through the localization manager.
    protected double
    forward(double value)
    Maps a value into the space the axis is linear in; the identity here, log10 for a logarithmic axis.
    double
    fromNormalized(double n)
    The inverse of toNormalized.
    double
    The high end of the data range.
    double
    The low end of the data range.
    The formatter, or null for the axis' default.
    int
    The side of the plot this axis is drawn on.
    The axis title, or null.
    double
    The high end of what is on screen.
    double
    The low end of what is on screen.
    protected double
    inverse(double t)
    The inverse of forward.
    boolean
    Whether grid lines are drawn at the ticks.
    boolean
    True when zero is forced into the range.
    protected boolean
    True when setRange pinned the high end, so adjustBounds must keep it as given.
    protected boolean
    True when setRange pinned the low end, so adjustBounds must keep it as given.
    boolean
    True for an axis drawn left or right of the plot, which maps values vertically.
    boolean
    Whether the axis line and labels are drawn.
    boolean
    True when the user has zoomed in, so the visible range is narrower than the data.
    boolean
    Whether gestures may zoom and pan this axis.
    void
    pan(double normalizedDelta)
    Pans by a fraction of the visible span; positive moves toward larger values.
    void
    Returns to the full data range.
    void
    restoreViewport(double min, double max, boolean zoomed)
    Puts back a viewport exactly as getVisibleMin(), getVisibleMax() and isZoomed() read it, without clamping or snapping; for undoing a temporary change such as an export, so the screen is left bit for bit as it was even mid-animation.
    void
    setDataBounds(double min, double max)
    Called by the chart with the extent of the data on this axis.
    void
    setDisplayRange(double min, double max)
    Sets what is drawn for one frame of an animated transition, without clamping and without marking the axis as zoomed; the next data update replaces it.
    Replaces how labels are formatted.
    setGridVisible(boolean gridVisible)
    Enables or disables grid lines at the ticks.
    setIncludeZero(boolean includeZero)
    Forces zero into the automatic range, as a bar chart's value axis needs.
    setMaxZoom(double maxZoom)
    The largest magnification pan and zoom may reach, relative to the full data range.
    setPosition(int position)
    Moves the axis to another side.
    setRange(Double min, Double max)
    Pins the bounds instead of deriving them from the data; pass null to restore automatic.
    Sets the title drawn alongside the axis.
    setVisible(boolean visible)
    Hides or shows the axis; a hidden axis still maps values.
    void
    setVisibleRange(double min, double max)
    Shows exactly [min, max], clamped to the data range and the zoom limit.
    setZoomEnabled(boolean zoomEnabled)
    Allows or blocks zooming and panning along this axis.
    double[]
    ticks(int maxCount)
    Ticks for the visible range.
    double
    toNormalized(double value)
    Maps a value to 0..1 across the visible range (outside 0..1 when off screen).
    void
    zoom(double factor, double anchor)
    Zooms around a point.

    Methods inherited from class Object

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

  • Constructor Details

    • Axis

      protected Axis(int position)

      Creates an axis at the given position.

      Parameters
      • position: one of BOTTOM, LEFT, RIGHT or TOP
  • Method Details

    • getPosition

      public int getPosition()
      The side of the plot this axis is drawn on.
    • setPosition

      public Axis setPosition(int position)
      Moves the axis to another side.
    • isVertical

      public boolean isVertical()
      True for an axis drawn left or right of the plot, which maps values vertically.
    • getTitle

      public String getTitle()
      The axis title, or null.
    • setTitle

      public Axis setTitle(String title)
      Sets the title drawn alongside the axis.
    • isVisible

      public boolean isVisible()
      Whether the axis line and labels are drawn.
    • setVisible

      public Axis setVisible(boolean visible)
      Hides or shows the axis; a hidden axis still maps values.
    • isGridVisible

      public boolean isGridVisible()
      Whether grid lines are drawn at the ticks.
    • setGridVisible

      public Axis setGridVisible(boolean gridVisible)
      Enables or disables grid lines at the ticks.
    • getFormatter

      public ValueFormatter getFormatter()
      The formatter, or null for the axis' default.
    • setFormatter

      public Axis setFormatter(ValueFormatter formatter)
      Replaces how labels are formatted.
    • setRange

      public Axis setRange(Double min, Double max)

      Pins the bounds instead of deriving them from the data; pass null to restore automatic. Bounds given in the wrong order are taken as a range, and two equal bounds are widened by a pad on each side so the axis is never empty. A single bound pinned at the very end of the double range gives way by that pad when the free side has no room left.

      Parameters
      • min: the low end, or null to derive it from the data
      • max: the high end, or null to derive it from the data
      Returns

      this axis

    • isMinFixed

      protected boolean isMinFixed()
      True when setRange pinned the low end, so adjustBounds must keep it as given.
    • isMaxFixed

      protected boolean isMaxFixed()
      True when setRange pinned the high end, so adjustBounds must keep it as given.
    • setIncludeZero

      public Axis setIncludeZero(boolean includeZero)
      Forces zero into the automatic range, as a bar chart's value axis needs.
    • isIncludeZero

      public boolean isIncludeZero()
      True when zero is forced into the range.
    • setMaxZoom

      public Axis setMaxZoom(double maxZoom)
      The largest magnification pan and zoom may reach, relative to the full data range.
    • isZoomEnabled

      public boolean isZoomEnabled()
      Whether gestures may zoom and pan this axis.
    • setZoomEnabled

      public Axis setZoomEnabled(boolean zoomEnabled)
      Allows or blocks zooming and panning along this axis.
    • setDataBounds

      public void setDataBounds(double min, double max)

      Called by the chart with the extent of the data on this axis.

      Parameters
      • min: smallest value of any visible series

      • max: largest value of any visible series

    • flatPad

      protected double flatPad(double anchor)

      How far a range with a single value is padded on each side; 10% of the value, or 1 at zero. A time axis overrides it, since 10% of an epoch timestamp is years.

      Parameters
      • anchor: the single value
      Returns

      the pad, positive

    • adjustBounds

      protected double[] adjustBounds(double min, double max)

      Lets a subclass pad or round the automatic bounds; the default rounds a numeric range outward to the nearest nice step so the outermost grid lines sit on the plot edge.

      Parameters
      • min: the raw minimum

      • max: the raw maximum

      Returns

      {min, max} to use

    • getDataMin

      public double getDataMin()
      The low end of the data range.
    • getDataMax

      public double getDataMax()
      The high end of the data range.
    • getVisibleMin

      public double getVisibleMin()
      The low end of what is on screen.
    • getVisibleMax

      public double getVisibleMax()
      The high end of what is on screen.
    • setVisibleRange

      public void setVisibleRange(double min, double max)
      Shows exactly [min, max], clamped to the data range and the zoom limit. Showing the whole data range clears the zoom, so later data changes rescale the axis again.
    • setDisplayRange

      public void setDisplayRange(double min, double max)
      Sets what is drawn for one frame of an animated transition, without clamping and without marking the axis as zoomed; the next data update replaces it.
    • restoreViewport

      public void restoreViewport(double min, double max, boolean zoomed)

      Puts back a viewport exactly as getVisibleMin(), getVisibleMax() and isZoomed() read it, without clamping or snapping; for undoing a temporary change such as an export, so the screen is left bit for bit as it was even mid-animation.

      Parameters
      • min: the visible minimum to restore

      • max: the visible maximum to restore

      • zoomed: the zoom flag to restore

      This is an internal hook for Chart (which lives in another package, so it has to be public); applications zoom with setViewport and resetViewport. A range that is not finite, or whose minimum exceeds its maximum, is ignored, so it can never put an axis into a state no other method allows.

    • resetViewport

      public void resetViewport()
      Returns to the full data range.
    • isZoomed

      public boolean isZoomed()
      True when the user has zoomed in, so the visible range is narrower than the data.
    • zoom

      public void zoom(double factor, double anchor)

      Zooms around a point.

      Parameters
      • factor: above 1 magnifies, below 1 shrinks

      • anchor: the normalized position (0..1 along the axis) that stays put

    • pan

      public void pan(double normalizedDelta)
      Pans by a fraction of the visible span; positive moves toward larger values.
    • forward

      protected double forward(double value)
      Maps a value into the space the axis is linear in; the identity here, log10 for a logarithmic axis. Zoom, pan and normalization all work in this space.
    • inverse

      protected double inverse(double t)
      The inverse of forward.
    • toNormalized

      public double toNormalized(double value)
      Maps a value to 0..1 across the visible range (outside 0..1 when off screen).
    • fromNormalized

      public double fromNormalized(double n)
      The inverse of toNormalized.
    • ticks

      public double[] ticks(int maxCount)

      Ticks for the visible range.

      Parameters
      • maxCount: the most ticks the measured label size allows
      Returns

      ascending tick values

    • format

      public String format(double value, double step)

      Formats a tick or value with the custom formatter if set, else the default.

      Parameters
      • value: the value

      • step: neighbouring tick spacing, or 0

      Returns

      the label

    • defaultFormat

      protected String defaultFormat(double value, double step)
      The axis' built in formatting; numbers are grouped and localized.
    • formatNumber

      public static String formatNumber(double value, int decimals)

      Formats value with exactly decimals decimal places through the localization manager. Whole numbers print without a decimal point, which the double overload of L10NManager.format does not guarantee.

      Parameters
      • value: the number

      • decimals: decimal places, 0 or more

      Returns

      the localized text