Class DateAxis


  • public class DateAxis
    extends Axis
    A subclass of Axis to be used for plotting date values. Dates are stored internally as doubles, and the two can be converted back and forth using the toDouble and toDate methods. For example, to plot a number of dates on a LineSeries you could use code like the following:
     AxesGraph graph = new AxesGraph();
     LineSeries series = new LineSeries("Dates");
     series.set(DateAxis.toDouble(date1), value1);
     series.set(DateAxis.toDouble(date2), value2);
     graph.setAxis(Axis.BOTTOM, new DateAxis());
     
    (here date1 and date2 are Date objects). Bars can also be plotted against a DateAxis - see the GeneralBarSeries API documentation for more info.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int FIT_CONTRACT
      Adjust the markers on dates by contracting the start and end of the axis to the nearest "useful" value.
      static int FIT_EXACT
      Adjust the markers on dates by print the start date exactly, and then step in "useful" steps from that point, until the final point which is positioned exactly where the data ends.
      static int FIT_EXACT_CONTRACT
      Adjust the markers on dates by print the start date exactly, and then step in "useful" steps from that point, if necessary dropping the final value on the axis.
      static int FIT_EXACT_EXPAND
      Adjust the markers on dates by print the start date exactly, and then step in "useful" steps from that point, if necessary adding an extra value on the axis after the data.
      static int FIT_EXACT_ROUND
      Adjust the markers on dates by print the start and end of the axis to the nearest "useful" value.
      static int FIT_EXPAND
      Adjust the markers on dates by expanding the start and end of the axis to the nearest "useful" value.
    • Constructor Summary

      Constructors 
      Constructor Description
      DateAxis()
      Create a new DateAxis with a format of "dd-MMM-yyyy"
      DateAxis​(DateFormat format)
      Create a new DateAxis with the specified format
      DateAxis​(DateFormat format, int density)
      Create a new DateAxis with the specified format and density
    • Field Detail

      • FIT_EXACT_ROUND

        public static final int FIT_EXACT_ROUND
        Adjust the markers on dates by print the start and end of the axis to the nearest "useful" value. For example, plotting Feb - Sep in steps of three months would plot Feb, July, September (the start and end dates, plus the first month of each quarter so long as a reasonable gap exists either side). This can lead to uneven gaps next to the first and last ticks. This is the default fit.
        Since:
        2.4.9
        See Also:
        Constant Field Values
      • FIT_EXPAND

        public static final int FIT_EXPAND
        Adjust the markers on dates by expanding the start and end of the axis to the nearest "useful" value. For example, plotting Feb - Sep in steps of three months would plot Jan, Apr, Jul, Oct (useful here metans the start of each quarter).. This can lead to a gap before and after the plotted data
        Since:
        2.4.9
        See Also:
        Constant Field Values
      • FIT_CONTRACT

        public static final int FIT_CONTRACT
        Adjust the markers on dates by contracting the start and end of the axis to the nearest "useful" value. For example, plotting Feb - Sep in steps of three months would plot April, July (useful here means the start of each quarter). This can lead to a gap after the start of the data and before the first tick.
        Since:
        2.4.9
        See Also:
        Constant Field Values
      • FIT_EXACT_EXPAND

        public static final int FIT_EXACT_EXPAND
        Adjust the markers on dates by print the start date exactly, and then step in "useful" steps from that point, if necessary adding an extra value on the axis after the data. This will result in even gaps between ticks, although the ticks may not be on the "best" values. There may also be a gap after the data.
        Since:
        2.4.9
        See Also:
        Constant Field Values
      • FIT_EXACT_CONTRACT

        public static final int FIT_EXACT_CONTRACT
        Adjust the markers on dates by print the start date exactly, and then step in "useful" steps from that point, if necessary dropping the final value on the axis. This will result in even gaps between ticks, although the ticks may not be on the "best" values. The final tick on the axis may be less than the final value of the data.
        Since:
        2.4.9
        See Also:
        Constant Field Values
      • FIT_EXACT

        public static final int FIT_EXACT
        Adjust the markers on dates by print the start date exactly, and then step in "useful" steps from that point, until the final point which is positioned exactly where the data ends. This will result in even gaps between all ticks except the lastticks, although the ticks may not be * on the "best" values.
        Since:
        2.4.9
        See Also:
        Constant Field Values
    • Constructor Detail

      • DateAxis

        public DateAxis()
        Create a new DateAxis with a format of "dd-MMM-yyyy"
      • DateAxis

        public DateAxis​(DateFormat format)
        Create a new DateAxis with the specified format
      • DateAxis

        public DateAxis​(DateFormat format,
                        int density)
        Create a new DateAxis with the specified format and density
        Parameters:
        format - how each date should be formatted
        density - the density of the values on the axis - usually one of Axis.DENSITY_NORMAL, Axis.DENSITY_SPARSE or Axis.DENSITY_MINIMAL, but may be any integer which is roughly the number of intended teeth on the axis.
    • Method Detail

      • format

        public String format​(double in)
        Description copied from class: Axis

        Given the specified number, return the text that should be placed against the tooth at that position. For example, an Axis that simply plotted integer values might return Integer.toString((int)in)

        Those wanting to create their own custom axis will typically override this method and Axis.steps(double, double).

        Specified by:
        format in class Axis
        Parameters:
        in - the value to format
        Returns:
        the value formatted as a String
      • setTimeZone

        public void setTimeZone​(TimeZone tz)
        Set the timezone in use for this date axis.
        Since:
        2.2.1
      • setBarWidth

        public void setBarWidth​(int seconds)
        When plotting bars against this axis, this method can be used to specify the width of those bars. By default this width is determined automatically based on the format (for example, a format that has a resolution of one day will result in bars one day wide). If the library guesses incorrectly however, this method may be used to hard-code the width. For example, setBarWidth(5*60) would give mean bars drawn against this axis are 5-minutes wide.
        Parameters:
        seconds - the number of seconds each bar should cover, or 0 to ask the library to guess (the default).
        Since:
        2.3
      • setBarsAtNoon

        public void setBarsAtNoon​(boolean noon)
        When plotting a GeneralBarSeries against a DateAxis, whether to center the bars on the tick, ie. midnight (false) or midway between the tick and the next tick, ie. noon (true). Typically you will want to set this method to true when plotting bars against lines that represent hour values, not just day values. When not plotting Bars, this method has no effect.
        Parameters:
        noon - whether to center bars plotted against this axis at midday
      • setStretchEnds

        public void setStretchEnds​(double stretch)

        Determines whether to "stretch" the ends of the graph to the next useful value. An example of when you would do this is plotting days of a month - although your data might only start on the 3rd of the month, you want the axis to start on the 1st.

        The "stretch" parameter takes a value from 0 to 1, where 0 means "never stretch the axis" and 1 means "always stretch". If necessary, values between 0 and 1 can be specified which will cause the axis to be stretched depending on how far from the next "useful" endpoint the data is.

        Note since 2.4.9, this method is a secondary way to control the axis. The primary method is the setFit(int) method. Calling this method with a value of 0 will also call setFit(FIT_EXACT_ROUND), and any other value will call setFit(FIT_EXPAND)

        Parameters:
        stretch - when to stretch the axis - 0 for never, 1 for always, or any value in between
        Since:
        2.3.1
      • setFit

        public void setFit​(int fit)

        Set the "fit" method used to determine where dates are placed on the axis. The supplied value should be one of the FIT_EXACT_ROUND values, and will determine which dates are chosen to represent the range of values from the data. Prior to 2.4.9 this was entirely controlled by the setStretchEnds(double) method, which this method largely replaces.

        Since:
        2.4.9
      • toDouble

        public static final double toDouble​(Date in)
        Convert a date to a double, so it can be stored internally. For example, to plot todays date on a Line Graph, you would do something like this:
           LineSeries data = new LineSeries("Dates");
           Date today = new Date();
           data.set(DateAxis.toDouble(today), 123.45);
         
        See Also:
        toDate(double)
      • toDate

        public static final Date toDate​(double in)
        Convert a double to a Date. The opposite of toDouble(java.util.Date). Use this to convert a double passed in to the format(double) method back to a Date.
      • steps

        public double[] steps​(double min,
                              double max)
        Description copied from class: Axis

        The steps method controls where the teeth are placed on the spine. Each subclass of Axis has a different strategy - for instance, the DateAxis will try and place ticks on the 1st of the month, the NumericAxis will try and place them evenly across the range and so on.

        The returned array should consist of a range of numbers, ordered from low to high, which mark the locations of the teeth on the spine. min and max are the minimum and maximum values of the data to plot, and these values will usually be the first and last values in the returned array.

        Those wanting to create their own custom axis will typically override this method and Axis.format(double).

        Specified by:
        steps in class Axis
        Parameters:
        min - the minimum value of the data to plot
        max - the maximum value of the data to plot
        Returns:
        an array of doubles representing the positions to place teeth on this axis