Class SessionTimeAxis

java.lang.Object
com.codename1.ui.chart.axis.Axis
com.codename1.ui.chart.axis.SessionTimeAxis

public class SessionTimeAxis extends Axis

A time axis that skips the gaps between sessions: weekends, holidays, overnight hours.

Series on this axis use the index of each bar as their x value (0, 1, 2...) and the axis maps every index to its timestamp for labelling, the way trading charts do. Each bar is one slot wide no matter how much time separates it from the previous one, so a Monday candle sits right next to Friday's.

Live data. A streaming series appends with XYSeries.add(y), which gives each new bar the next index, and with setMaxPoints(n) drops its oldest bar, so its first index climbs past 0. Follow it by calling addTime(long) with each bar's timestamp, and setMaxTimes(n) with the same n: the axis then remembers the index of its first stored timestamp (getFirstIndex()) and labels the climbing indices with the times that belong to them.

Chart chart = new Chart();
long day = 24L * 60 * 60 * 1000;
long[] timestamps = {0, day, 4 * day};
double[] open = {10, 11, 12}, high = {12, 13, 14}, low = {9, 10, 11}, close = {11, 12, 13};
SessionTimeAxis axis = new SessionTimeAxis(Axis.BOTTOM, timestamps);
chart.setXAxis(axis);
chart.addSeries(new CandlestickSeries("ACME").setOhlc(null, open, high, low, close));
  • Constructor Details

    • SessionTimeAxis

      public SessionTimeAxis(int position, long[] times)

      Creates the axis.

      Parameters
      • position: one of the Axis position constants

      • times: the timestamp of each bar in milliseconds, ascending

  • Method Details

    • setTimes

      public SessionTimeAxis setTimes(long[] times)

      Replaces the timestamps.

      An axis does not know the chart it is on, so the chart is not repainted by this call, as with CategoryAxis.setCategories: call repaint() on the chart, or change the series data in the same step, which repaints it.

      Parameters
      • times: the timestamp of each bar in milliseconds, ascending
      Returns

      this axis

    • addTime

      public SessionTimeAxis addTime(long time)

      Appends the timestamp of the next bar, for a series that grows with XYSeries.add(y).

      An axis does not know the chart it is on, so the chart is not repainted by this call, as with CategoryAxis.setCategories: call repaint() on the chart, or change the series data in the same step, which repaints it.

      Parameters
      • time: the new bar's timestamp in milliseconds, not before the previous one
      Returns

      this axis

    • setMaxTimes

      public SessionTimeAxis setMaxTimes(int n)

      Keeps only the newest n timestamps, dropping the oldest as addTime appends, the way XYSeries.setMaxPoints(n) drops the oldest bar. Each dropped timestamp moves getFirstIndex() up by one, so the remaining bars keep their indices. Like setTimes, this does not repaint the chart.

      Parameters
      • n: the maximum number of timestamps, or 0 for no limit
      Returns

      this axis

    • getFirstIndex

      public int getFirstIndex()

      The bar index of the oldest stored timestamp: 0 until setMaxTimes drops one.

      Returns

      the index of the first timestamp

    • timeAt

      public long timeAt(double index)

      The timestamp of bar index, extrapolated past either end.

      Parameters
      • index: the bar index
      Returns

      milliseconds since the epoch

    • setDataBounds

      public void setDataBounds(double min, double max)
      Description copied from class: Axis

      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

      Overrides:
      setDataBounds in class Axis
    • adjustBounds

      protected double[] adjustBounds(double min, double max)
      Description copied from class: Axis

      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

      Overrides:
      adjustBounds in class Axis
    • ticks

      public double[] ticks(int maxCount)
      Description copied from class: Axis

      Ticks for the visible range.

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

      ascending tick values

      Overrides:
      ticks in class Axis
    • defaultFormat

      protected String defaultFormat(double value, double step)
      Description copied from class: Axis
      The axis' built in formatting; numbers are grouped and localized.
      Overrides:
      defaultFormat in class Axis