Class DateAxis

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, PublicCloneable

    public class DateAxis
    extends ValueAxis
    implements java.lang.Cloneable, java.io.Serializable
    The base class for axes that display dates. You will find it easier to understand how this axis works if you bear in mind that it really displays/measures integer (or long) data, where the integers are milliseconds since midnight, 1-Jan-1970. When displaying tick labels, the millisecond values are converted back to dates using a DateFormat instance.

    You can also create a Timeline and supply in the constructor to create an axis that only contains certain domain values. For example, this allows you to create a date axis that only contains working days.

    See Also:
    Serialized Form
    • Field Detail

      • serialVersionUID

        private static final long serialVersionUID
        For serialization.
        See Also:
        Constant Field Values
      • DEFAULT_DATE_RANGE

        public static final DateRange DEFAULT_DATE_RANGE
        The default axis range.
      • DEFAULT_AUTO_RANGE_MINIMUM_SIZE_IN_MILLISECONDS

        public static final double DEFAULT_AUTO_RANGE_MINIMUM_SIZE_IN_MILLISECONDS
        The default minimum auto range size.
        See Also:
        Constant Field Values
      • DEFAULT_ANCHOR_DATE

        public static final java.util.Date DEFAULT_ANCHOR_DATE
        The default anchor date.
      • tickUnit

        private DateTickUnit tickUnit
        The current tick unit.
      • dateFormatOverride

        private java.text.DateFormat dateFormatOverride
        The override date format.
      • tickMarkPosition

        private DateTickMarkPosition tickMarkPosition
        Tick marks can be displayed at the start or the middle of the time period.
      • DEFAULT_TIMELINE

        private static final Timeline DEFAULT_TIMELINE
        A static default timeline shared by all standard DateAxis
      • timeZone

        private java.util.TimeZone timeZone
        The time zone for the axis.
      • locale

        private java.util.Locale locale
        The locale for the axis (null is not permitted).
      • timeline

        private Timeline timeline
        Our underlying timeline.
    • Constructor Detail

      • DateAxis

        public DateAxis()
        Creates a date axis with no label.
      • DateAxis

        public DateAxis​(java.lang.String label)
        Creates a date axis with the specified label.
        Parameters:
        label - the axis label (null permitted).
      • DateAxis

        public DateAxis​(java.lang.String label,
                        java.util.TimeZone zone,
                        java.util.Locale locale)
        Creates a date axis.
        Parameters:
        label - the axis label (null permitted).
        zone - the time zone.
        locale - the locale (null not permitted).
    • Method Detail

      • getTimeZone

        public java.util.TimeZone getTimeZone()
        Returns the time zone for the axis.
        Returns:
        The time zone (never null).
        See Also:
        setTimeZone(TimeZone)
      • setTimeZone

        public void setTimeZone​(java.util.TimeZone zone)
        Sets the time zone for the axis and sends an AxisChangeEvent to all registered listeners.
        Parameters:
        zone - the time zone (null not permitted).
        See Also:
        getTimeZone()
      • getLocale

        public java.util.Locale getLocale()
        Returns the locale for this axis.
        Returns:
        The locale (never null).
      • setLocale

        public void setLocale​(java.util.Locale locale)
        Sets the locale for the axis and sends a change event to all registered listeners.
        Parameters:
        locale - the new locale (null not permitted).
      • getTimeline

        public Timeline getTimeline()
        Returns the underlying timeline used by this axis.
        Returns:
        The timeline.
      • setTimeline

        public void setTimeline​(Timeline timeline)
        Sets the underlying timeline to use for this axis. If the timeline is changed, an AxisChangeEvent is sent to all registered listeners.
        Parameters:
        timeline - the timeline.
      • getTickUnit

        public DateTickUnit getTickUnit()
        Returns the tick unit for the axis.

        Note: if the autoTickUnitSelection flag is true the tick unit may be changed while the axis is being drawn, so in that case the return value from this method may be irrelevant if the method is called before the axis has been drawn.

        Returns:
        The tick unit (possibly null).
        See Also:
        setTickUnit(DateTickUnit), ValueAxis.isAutoTickUnitSelection()
      • setTickUnit

        public void setTickUnit​(DateTickUnit unit,
                                boolean notify,
                                boolean turnOffAutoSelection)
        Sets the tick unit attribute and, if requested, sends an AxisChangeEvent to all registered listeners.
        Parameters:
        unit - the new tick unit.
        notify - notify registered listeners?
        turnOffAutoSelection - turn off auto selection?
        See Also:
        getTickUnit()
      • getDateFormatOverride

        public java.text.DateFormat getDateFormatOverride()
        Returns the date format override. If this is non-null, then it will be used to format the dates on the axis.
        Returns:
        The formatter (possibly null).
      • setDateFormatOverride

        public void setDateFormatOverride​(java.text.DateFormat formatter)
        Sets the date format override and sends an AxisChangeEvent to all registered listeners. If this is non-null, then it will be used to format the dates on the axis.
        Parameters:
        formatter - the date formatter (null permitted).
      • setRange

        public void setRange​(Range range)
        Sets the upper and lower bounds for the axis and sends an AxisChangeEvent to all registered listeners. As a side-effect, the auto-range flag is set to false.
        Overrides:
        setRange in class ValueAxis
        Parameters:
        range - the new range (null not permitted).
        See Also:
        ValueAxis.getRange()
      • setRange

        public void setRange​(Range range,
                             boolean turnOffAutoRange,
                             boolean notify)
        Sets the range for the axis, if requested, sends an AxisChangeEvent to all registered listeners. As a side-effect, the auto-range flag is set to false (optional).
        Overrides:
        setRange in class ValueAxis
        Parameters:
        range - the range (null not permitted).
        turnOffAutoRange - a flag that controls whether or not the auto range is turned off.
        notify - a flag that controls whether or not listeners are notified.
        See Also:
        ValueAxis.getRange()
      • setRange

        public void setRange​(java.util.Date lower,
                             java.util.Date upper)
        Sets the axis range and sends an AxisChangeEvent to all registered listeners.
        Parameters:
        lower - the lower bound for the axis.
        upper - the upper bound for the axis.
      • setMinimumDate

        public void setMinimumDate​(java.util.Date date)
        Sets the minimum date visible on the axis and sends an AxisChangeEvent to all registered listeners. If date is on or after the current maximum date for the axis, the maximum date will be shifted to preserve the current length of the axis.
        Parameters:
        date - the date (null not permitted).
        See Also:
        getMinimumDate(), setMaximumDate(Date)
      • setMaximumDate

        public void setMaximumDate​(java.util.Date maximumDate)
        Sets the maximum date visible on the axis and sends an AxisChangeEvent to all registered listeners. If maximumDate is on or before the current minimum date for the axis, the minimum date will be shifted to preserve the current length of the axis.
        Parameters:
        maximumDate - the date (null not permitted).
        See Also:
        getMinimumDate(), setMinimumDate(Date)
      • getTickMarkPosition

        public DateTickMarkPosition getTickMarkPosition()
        Returns the tick mark position (start, middle or end of the time period).
        Returns:
        The position (never null).
      • setTickMarkPosition

        public void setTickMarkPosition​(DateTickMarkPosition position)
        Sets the tick mark position (start, middle or end of the time period) and sends an AxisChangeEvent to all registered listeners.
        Parameters:
        position - the position (null not permitted).
      • configure

        public void configure()
        Configures the axis to work with the specified plot. If the axis has auto-scaling, then sets the maximum and minimum values.
        Specified by:
        configure in class Axis
      • isHiddenValue

        public boolean isHiddenValue​(long millis)
        Returns true if the axis hides this value, and false otherwise.
        Parameters:
        millis - the data value.
        Returns:
        A value.
      • valueToJava2D

        public double valueToJava2D​(double value,
                                    java.awt.geom.Rectangle2D area,
                                    RectangleEdge edge)
        Translates the data value to the display coordinates (Java 2D User Space) of the chart.
        Specified by:
        valueToJava2D in class ValueAxis
        Parameters:
        value - the date to be plotted.
        area - the rectangle (in Java2D space) where the data is to be plotted.
        edge - the axis location.
        Returns:
        The coordinate corresponding to the supplied data value.
        See Also:
        ValueAxis.java2DToValue(double, Rectangle2D, RectangleEdge)
      • dateToJava2D

        public double dateToJava2D​(java.util.Date date,
                                   java.awt.geom.Rectangle2D area,
                                   RectangleEdge edge)
        Translates a date to Java2D coordinates, based on the range displayed by this axis for the specified data area.
        Parameters:
        date - the date.
        area - the rectangle (in Java2D space) where the data is to be plotted.
        edge - the axis location.
        Returns:
        The coordinate corresponding to the supplied date.
      • java2DToValue

        public double java2DToValue​(double java2DValue,
                                    java.awt.geom.Rectangle2D area,
                                    RectangleEdge edge)
        Translates a Java2D coordinate into the corresponding data value. To perform this translation, you need to know the area used for plotting data, and which edge the axis is located on.
        Specified by:
        java2DToValue in class ValueAxis
        Parameters:
        java2DValue - the coordinate in Java2D space.
        area - the rectangle (in Java2D space) where the data is to be plotted.
        edge - the axis location.
        Returns:
        A data value.
        See Also:
        ValueAxis.valueToJava2D(double, Rectangle2D, RectangleEdge)
      • calculateLowestVisibleTickValue

        public java.util.Date calculateLowestVisibleTickValue​(DateTickUnit unit)
        Calculates the value of the lowest visible tick on the axis.
        Parameters:
        unit - date unit to use.
        Returns:
        The value of the lowest visible tick on the axis.
      • calculateHighestVisibleTickValue

        public java.util.Date calculateHighestVisibleTickValue​(DateTickUnit unit)
        Calculates the value of the highest visible tick on the axis.
        Parameters:
        unit - date unit to use.
        Returns:
        The value of the highest visible tick on the axis.
      • previousStandardDate

        protected java.util.Date previousStandardDate​(java.util.Date date,
                                                      DateTickUnit unit)
        Returns the previous "standard" date, for a given date and tick unit.
        Parameters:
        date - the reference date.
        unit - the tick unit.
        Returns:
        The previous "standard" date.
      • calculateDateForPosition

        private java.util.Date calculateDateForPosition​(RegularTimePeriod period,
                                                        DateTickMarkPosition position)
        Returns a Date corresponding to the specified position within a RegularTimePeriod.
        Parameters:
        period - the period.
        position - the position (null not permitted).
        Returns:
        A date.
      • nextStandardDate

        protected java.util.Date nextStandardDate​(java.util.Date date,
                                                  DateTickUnit unit)
        Returns the first "standard" date (based on the specified field and units).
        Parameters:
        date - the reference date.
        unit - the date tick unit.
        Returns:
        The next "standard" date.
      • createStandardDateTickUnits

        public static TickUnitSource createStandardDateTickUnits()
        Returns a collection of standard date tick units that uses the default time zone. This collection will be used by default, but you are free to create your own collection if you want to (see the ValueAxis.setStandardTickUnits(TickUnitSource) method inherited from the ValueAxis class).
        Returns:
        A collection of standard date tick units.
      • createStandardDateTickUnits

        public static TickUnitSource createStandardDateTickUnits​(java.util.TimeZone zone,
                                                                 java.util.Locale locale)
        Returns a collection of standard date tick units. This collection will be used by default, but you are free to create your own collection if you want to (see the ValueAxis.setStandardTickUnits(TickUnitSource) method inherited from the ValueAxis class).
        Parameters:
        zone - the time zone (null not permitted).
        locale - the locale (null not permitted).
        Returns:
        A collection of standard date tick units.
      • autoAdjustRange

        protected void autoAdjustRange()
        Rescales the axis to ensure that all data is visible.
        Specified by:
        autoAdjustRange in class ValueAxis
      • selectAutoTickUnit

        protected void selectAutoTickUnit​(java.awt.Graphics2D g2,
                                          java.awt.geom.Rectangle2D dataArea,
                                          RectangleEdge edge)
        Selects an appropriate tick value for the axis. The strategy is to display as many ticks as possible (selected from an array of 'standard' tick units) without the labels overlapping.
        Parameters:
        g2 - the graphics device.
        dataArea - the area defined by the axes.
        edge - the axis location.
      • selectHorizontalAutoTickUnit

        protected void selectHorizontalAutoTickUnit​(java.awt.Graphics2D g2,
                                                    java.awt.geom.Rectangle2D dataArea,
                                                    RectangleEdge edge)
        Selects an appropriate tick size for the axis. The strategy is to display as many ticks as possible (selected from a collection of 'standard' tick units) without the labels overlapping.
        Parameters:
        g2 - the graphics device.
        dataArea - the area defined by the axes.
        edge - the axis location.
      • selectVerticalAutoTickUnit

        protected void selectVerticalAutoTickUnit​(java.awt.Graphics2D g2,
                                                  java.awt.geom.Rectangle2D dataArea,
                                                  RectangleEdge edge)
        Selects an appropriate tick size for the axis. The strategy is to display as many ticks as possible (selected from a collection of 'standard' tick units) without the labels overlapping.
        Parameters:
        g2 - the graphics device.
        dataArea - the area in which the plot should be drawn.
        edge - the axis location.
      • estimateMaximumTickLabelWidth

        private double estimateMaximumTickLabelWidth​(java.awt.Graphics2D g2,
                                                     DateTickUnit unit)
        Estimates the maximum width of the tick labels, assuming the specified tick unit is used.

        Rather than computing the string bounds of every tick on the axis, we just look at two values: the lower bound and the upper bound for the axis. These two values will usually be representative.

        Parameters:
        g2 - the graphics device.
        unit - the tick unit to use for calculation.
        Returns:
        The estimated maximum width of the tick labels.
      • estimateMaximumTickLabelHeight

        private double estimateMaximumTickLabelHeight​(java.awt.Graphics2D g2,
                                                      DateTickUnit unit)
        Estimates the maximum width of the tick labels, assuming the specified tick unit is used.

        Rather than computing the string bounds of every tick on the axis, we just look at two values: the lower bound and the upper bound for the axis. These two values will usually be representative.

        Parameters:
        g2 - the graphics device.
        unit - the tick unit to use for calculation.
        Returns:
        The estimated maximum width of the tick labels.
      • refreshTicks

        public java.util.List refreshTicks​(java.awt.Graphics2D g2,
                                           AxisState state,
                                           java.awt.geom.Rectangle2D dataArea,
                                           RectangleEdge edge)
        Calculates the positions of the tick labels for the axis, storing the results in the tick label list (ready for drawing).
        Specified by:
        refreshTicks in class Axis
        Parameters:
        g2 - the graphics device.
        state - the axis state.
        dataArea - the area in which the plot should be drawn.
        edge - the location of the axis.
        Returns:
        A list of ticks.
      • correctTickDateForPosition

        private java.util.Date correctTickDateForPosition​(java.util.Date time,
                                                          DateTickUnit unit,
                                                          DateTickMarkPosition position)
        Corrects the given tick date for the position setting.
        Parameters:
        time - the tick date/time.
        unit - the tick unit.
        position - the tick position.
        Returns:
        The adjusted time.
      • refreshTicksHorizontal

        protected java.util.List refreshTicksHorizontal​(java.awt.Graphics2D g2,
                                                        java.awt.geom.Rectangle2D dataArea,
                                                        RectangleEdge edge)
        Recalculates the ticks for the date axis.
        Parameters:
        g2 - the graphics device.
        dataArea - the area in which the data is to be drawn.
        edge - the location of the axis.
        Returns:
        A list of ticks.
      • refreshTicksVertical

        protected java.util.List refreshTicksVertical​(java.awt.Graphics2D g2,
                                                      java.awt.geom.Rectangle2D dataArea,
                                                      RectangleEdge edge)
        Recalculates the ticks for the date axis.
        Parameters:
        g2 - the graphics device.
        dataArea - the area in which the plot should be drawn.
        edge - the location of the axis.
        Returns:
        A list of ticks.
      • draw

        public AxisState draw​(java.awt.Graphics2D g2,
                              double cursor,
                              java.awt.geom.Rectangle2D plotArea,
                              java.awt.geom.Rectangle2D dataArea,
                              RectangleEdge edge,
                              PlotRenderingInfo plotState)
        Draws the axis on a Java 2D graphics device (such as the screen or a printer).
        Specified by:
        draw in class Axis
        Parameters:
        g2 - the graphics device (null not permitted).
        cursor - the cursor location.
        plotArea - the area within which the axes and data should be drawn (null not permitted).
        dataArea - the area within which the data should be drawn (null not permitted).
        edge - the location of the axis (null not permitted).
        plotState - collects information about the plot (null permitted).
        Returns:
        The axis state (never null).
      • zoomRange

        public void zoomRange​(double lowerPercent,
                              double upperPercent)
        Zooms in on the current range (zoom-in stops once the axis length reaches the equivalent of one millisecond).
        Overrides:
        zoomRange in class ValueAxis
        Parameters:
        lowerPercent - the new lower bound.
        upperPercent - the new upper bound.
      • equals

        public boolean equals​(java.lang.Object obj)
        Tests this axis for equality with an arbitrary object.
        Overrides:
        equals in class ValueAxis
        Parameters:
        obj - the object (null permitted).
        Returns:
        A boolean.
      • hashCode

        public int hashCode()
        Returns a hash code for this object.
        Overrides:
        hashCode in class Axis
        Returns:
        A hash code.
      • clone

        public java.lang.Object clone()
                               throws java.lang.CloneNotSupportedException
        Returns a clone of the object.
        Specified by:
        clone in interface PublicCloneable
        Overrides:
        clone in class ValueAxis
        Returns:
        A clone.
        Throws:
        java.lang.CloneNotSupportedException - if some component of the axis does not support cloning.