Class AAxis<T extends IAxisScalePolicy>

  • Type Parameters:
    T - Subtypes may be more picky which scale policies the accept to disallow incorrect scales: This supports it (see IAxis.setAxisScalePolicy(IAxisScalePolicy)).
    All Implemented Interfaces:
    IAxis<T>, java.beans.PropertyChangeListener, java.io.Serializable, java.util.EventListener
    Direct Known Subclasses:
    AAxisTransformation, AxisInverse, AxisLinear

    public abstract class AAxis<T extends IAxisScalePolicy>
    extends java.lang.Object
    implements IAxis<T>, java.beans.PropertyChangeListener
    The base class for an axis of the Chart2D.

    Normally - as the design and interaction of an Axis with the Chart2DD is very fine-grained - it is not instantiated by users of jchart2d: It is automatically instantiated by the constructor of Chart2D. It then may be retrieved from the Chart2D by the methods Chart2D.getAxisX() and Chart2D.getAxisY() for further configuration.

    Version:
    $Revision: 1.61 $
    Author:
    Achim Westermann
    See Also:
    Serialized Form
    • Field Detail

      • m_accessor

        protected AAxis.AChart2DDataAccessor m_accessor
        The accessor to the Chart2D.

        It determines, which axis (x or y) this instance is representing.

      • m_majorTickSpacing

        protected double m_majorTickSpacing
        The major tick spacing for label generations.

        See Also:
        setMajorTickSpacing(double)
      • m_max

        protected double m_max
        The current maximum value for all points in all traces.
      • m_min

        protected double m_min
        The current minimum value for all points in all traces.
      • m_minorTickSpacing

        protected double m_minorTickSpacing
        The minor tick spacing for label generations.

        See Also:
        setMinorTickSpacing(double)
      • m_needsFullRescale

        protected boolean m_needsFullRescale
        Flag to detect if a re-scaling has to be done.

        It is set to false in scale() which is triggered from the painting Thread. Whenever a bound change is detected in propertyChange(PropertyChangeEvent) this is set to true.

        Please remind: In previous versions there was only a test if the bounds had changed since the last scaling. This was not always correct: If in between two paint cycles the bounds were changed and new points added but at the point in time when the 2nd paint cycle starts the bounds would be equal no full rescaling would be performed even if the added points would have been scaled in relation to the changed bounds at their adding time: Bounds checks are not sufficient!

      • m_rangePolicy

        protected IRangePolicy m_rangePolicy
        A plugable range policy.
    • Constructor Detail

      • AAxis

        public AAxis​(IAxisLabelFormatter formatter,
                     T scalePolicy)
        Constructor that uses the given label formatter for formatting labels.

        Parameters:
        formatter - needed for formatting labels of this axis.
        scalePolicy - controls the ticks/labels and their distance.
    • Method Detail

      • addTrace

        public boolean addTrace​(ITrace2D trace)
        Description copied from interface: IAxis
        Adds a trace that belongs to this axis.

        Adding a trace that is already contained may be problematic, so an exception should be raised in that case to warn you that your code is doing unnecessary to malicious operations.

        Specified by:
        addTrace in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        trace - the trace to add.
        Returns:
        true if the trace was added, false else.
        See Also:
        IAxis.addTrace(info.monitorenter.gui.chart.ITrace2D)
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
        See Also:
        Object.equals(java.lang.Object)
      • findMax

        protected final double findMax()
        Searches for the maximum value of all contained ITraces in the dimension this axis stands for.

        This method is triggered when a trace fired a property change for property ITrace2D.PROPERTY_MAX_X or ITrace2D.PROPERTY_MAX_Y with a value lower than the internal stored maximum.

        Performance breakdown is avoided because all ITrace2D implementations cache their max and min values.

        Returns:
        the maximum value of all traces for the dimension this axis works in.
      • findMin

        protected final double findMin()
        Searches for the minimum value of all contained ITraces in the dimension this axis stands for.

        This method is triggered when a trace fired a property change for property ITrace2D.PROPERTY_MAX_X or ITrace2D.PROPERTY_MAX_Y with a value lower than the internal stored minimum.

        Performance breakdown is avoided because all ITrace2D implementations cache their max and min values.

        Returns:
        the minimum value of all traces for the dimension this axis works in.
      • getHeight

        public final int getHeight​(java.awt.Graphics g2d)
        Description copied from interface: IAxis
        Returns the height in pixel this axis needs to paint itself.

        This includes the axis line, it's ticks and labels and it's title.

        Note:
        For an y axis the hight only includes the overhang it needs on the upper edge for painting a complete lable, not the complete space it needs for the complete line.

        Specified by:
        getHeight in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        g2d - needed for font metric information.
        Returns:
        the height in pixel this axis needs to paint itself.
        See Also:
        IAxis.getHeight(java.awt.Graphics)
      • getMax

        public double getMax()
        Description copied from interface: IAxis
        Returns the maximum value from all traces of this axis with respect to the installed range policy.

        Specified by:
        getMax in interface IAxis<T extends IAxisScalePolicy>
        Returns:
        the maximum value from all traces of this axis with respect to the installed range policy.
        See Also:
        IAxis.getMax()
      • getMin

        public double getMin()
        Description copied from interface: IAxis
        Returns the minimum value of all traces of this axis with respect to the installed range policy.

        Specified by:
        getMin in interface IAxis<T extends IAxisScalePolicy>
        Returns:
        the minimum value of all traces of this axis with respect to the installed range policy.
        See Also:
        IAxis.getMin()
      • getPixelXLeft

        public final int getPixelXLeft()
        Description copied from interface: IAxis
        Returns the left pixel of this axis coordinate in the graphic context of the current paint operation.

        Note that this value is only valid throughout a Chart2D.paint(java.awt.Graphics) invocation.

        Specified by:
        getPixelXLeft in interface IAxis<T extends IAxisScalePolicy>
        Returns:
        the left pixel coordinate of this axis in the graphic context of the current paint operation.
        See Also:
        IAxis.getPixelXLeft()
      • getPixelXRight

        public final int getPixelXRight()
        Description copied from interface: IAxis
        Returns the right pixel coordinate of this axis in the graphic context of the current paint operation.

        Note that this value is only valid throughout a Chart2D.paint(java.awt.Graphics) invocation.

        Specified by:
        getPixelXRight in interface IAxis<T extends IAxisScalePolicy>
        Returns:
        the right pixel coordinate of this axis in the graphic context of the current paint operation.
        See Also:
        IAxis.getPixelXRight()
      • getPixelYBottom

        public final int getPixelYBottom()
        Description copied from interface: IAxis
        Returns the bottom pixel coordinate of this axis in the graphic context of the current paint operation.

        Note that this value is only valid throughout a Chart2D.paint(java.awt.Graphics) invocation.

        Specified by:
        getPixelYBottom in interface IAxis<T extends IAxisScalePolicy>
        Returns:
        the bottom pixel coordinate of this axis in the graphic context of the current paint operation.
        See Also:
        IAxis.getPixelYBottom()
      • getPixelYTop

        public final int getPixelYTop()
        Description copied from interface: IAxis
        Returns the top pixel coordinate of this axis in the graphic context of the current paint operation.

        Note that this value is only valid throughout a Chart2D.paint(java.awt.Graphics) invocation.

        Specified by:
        getPixelYTop in interface IAxis<T extends IAxisScalePolicy>
        Returns:
        the top pixel coordinate of this axis in the graphic context of the current paint operation.
        See Also:
        IAxis.getPixelYTop()
      • getRange

        public final Range getRange()
        This method is used by the Chart2D to scale it's values during painting.

        Caution: This method does not necessarily return the Range configured with setRange(Range). The internal IRangePolicy is taken into account.

        Specified by:
        getRange in interface IAxis<T extends IAxisScalePolicy>
        Returns:
        the range corresponding to the upper and lower bound of the values that will be visible on this Axis of the Chart2D.
        See Also:
        setRangePolicy(IRangePolicy)
      • getTitlePainter

        @Deprecated
        public final IAxisTitlePainter getTitlePainter()
        Deprecated.
        this method might be dropped because the painter should be of no concern.
        Description copied from interface: IAxis
        Returns the instance that will paint the title of this axis.

        Specified by:
        getTitlePainter in interface IAxis<T extends IAxisScalePolicy>
        Returns:
        the instance that will paint the title of this axis.
        See Also:
        IAxis.getTitlePainter()
      • getTraces

        public java.util.Set<ITrace2D> getTraces()
        Description copied from interface: IAxis
        Returns a Set<ITrace2D> with all traces covered by this axis.

        Caution!
        The original internal modifiable set is returned for performance reasons and by contract (to allow removing traces) so do not mess with it to avoid ugly unpredictable side effects!

        Specified by:
        getTraces in interface IAxis<T extends IAxisScalePolicy>
        Returns:
        a Set<ITrace2D> with all traces covered by this axis.
        See Also:
        IAxis.getTraces()
      • getValueDistanceForPixel

        protected final double getValueDistanceForPixel​(int pixel)
        Returns the value distance on the current chart that exists for the given amount of pixel distance in the given direction of this Axis.

        Depending on the width of the actual Chart2D and the contained values, the relation between displayed distances (pixel) and value distances (the values of the addes ITrace2D instances changes.

        This method calculates depending on the actual painting area of the Chart2D, the shift in value between two points that have a screen distance of the given pixel.
        This method is not used by the chart itself but a helper for outside use.

        Parameters:
        pixel - The desired distance between to scalepoints of the x- axis in pixel.
        Returns:
        a scaled (from pixel to internal value-range) and normed (to the factor of the current unit of the axis) value usable to calculate the coords for the scalepoints of the axis.
      • getWidth

        public final int getWidth​(java.awt.Graphics g2d)
        Description copied from interface: IAxis
        Returns the width in pixel this axis needs to paint itself.

        This includes the axis line, it's ticks and labels and it's title.

        Note:
        For an x axis the width only includes the overhang it needs on the right edge for painting a complete label, not the complete space it needs for the complete line.

        Specified by:
        getWidth in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        g2d - needed for font metric information.
        Returns:
        the width in pixel this axis needs to paint itself.
        See Also:
        IAxis.getWidth(java.awt.Graphics)
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
        See Also:
        Object.hashCode()
      • initPaintIteration

        public void initPaintIteration()
        Performs expensive calculations for various values that are used by many calls throughout a paint iterations.

        These values are constant throughout a paint iteration by the contract that no point is added removed or changed in this period. Because these values are used from many methods it is impossible to calculate them at a "transparent" method that may perform this caching over a paint period without knowledge from outside. The first method called in a paint iteration is called several further times in the iteration. So this is the common hook to invoke before painting a chart.

        Specified by:
        initPaintIteration in interface IAxis<T extends IAxisScalePolicy>
      • isDirtyScaling

        public final boolean isDirtyScaling()
        Description copied from interface: IAxis
        Returns true if the bounds in the given dimension of all TracePoint2D instances of all internal ITrace2D instances have changed since all points have been normalized to a value between 0 and 1 or true if this axis has different range since the last call to IAxis.scale().

        Specified by:
        isDirtyScaling in interface IAxis<T extends IAxisScalePolicy>
        Returns:
        true if the bounds in the given dimension of all TracePoint2D instances of all internal ITrace2D instances have changed since all points have been normalized to a value between 0 and 1 or true if this axis has different range since the last call to IAxis.scale().
        See Also:
        IAxis.isDirtyScaling()
      • isPaintGrid

        public final boolean isPaintGrid()
        Returns whether the x grid is painted or not.

        Specified by:
        isPaintGrid in interface IAxis<T extends IAxisScalePolicy>
        Returns:
        whether the x grid is painted or not.
      • isPaintScale

        public final boolean isPaintScale()
        Returns whether the scale for this axis should be painted or not.

        Specified by:
        isPaintScale in interface IAxis<T extends IAxisScalePolicy>
        Returns:
        whether the scale for this axis should be painted or not.
      • isVisible

        public boolean isVisible()
        Check if this axis is visible, i.e. needs to be painted on the chart.

        Specified by:
        isVisible in interface IAxis<T extends IAxisScalePolicy>
        Returns:
        true if this axis has to be painted on the chart.
      • paint

        public void paint​(java.awt.Graphics g2d)
        Description copied from interface: IAxis
        Renders the axis line along with title, scale, scale labels and unit label.

        This should only be called from Chart2D, all other uses may cause damaged UI or deadlocks.

        Specified by:
        paint in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        g2d - the graphics context to use.
        See Also:
        IAxis.paint(java.awt.Graphics)
      • paintTitle

        public int paintTitle​(java.awt.Graphics g2d)
        Description copied from interface: IAxis
        Routine for painting the title of this axis.

        Intended for Chart2D only!!!

        Specified by:
        paintTitle in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        g2d - needed for painting.
        Returns:
        the width consumed in pixel for y axis, the height consumed in pixel for x axis.
        See Also:
        IAxis.paintTitle(java.awt.Graphics)
      • propertyChange

        public void propertyChange​(java.beans.PropertyChangeEvent evt)
        Receives all PropertyChangeEvent from all instances the chart registers itself as a PropertyChangeListener .

        Specified by:
        propertyChange in interface java.beans.PropertyChangeListener
        Parameters:
        evt - the property change event that was fired.
        See Also:
        PropertyChangeListener.propertyChange(java.beans.PropertyChangeEvent)
      • removePropertyChangeListener

        public void removePropertyChangeListener​(java.lang.String property,
                                                 java.beans.PropertyChangeListener listener)
        Description copied from interface: IAxis
        Remove a PropertyChangeListener for a specific property. If listener was added more than once to the same event source for the specified property, it will be notified one less time after being removed. If propertyName is null, no exception is thrown and no action is taken. If listener is null, or was never added for the specified property, no exception is thrown and no action is taken.
        Specified by:
        removePropertyChangeListener in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        property - The name of the property that was listened on.
        listener - The PropertyChangeListener to be removed.
        See Also:
        IAxis.removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
      • scalePoint

        protected final void scalePoint​(ITracePoint2D point)
        Internally rescales the given ITracePoint2D in the dimension this axis works in.

        Parameters:
        point - the point to scale (between 0.0 and 1.0) according to the internal bounds.
      • scale

        public void scale()
        Description copied from interface: IAxis
        Scales all ITrace2D instances in the dimension represented by this axis.

        This method is not deadlock - safe and should be called by the Chart2D only!

        Specified by:
        scale in interface IAxis<T extends IAxisScalePolicy>
        See Also:
        IAxis.scale()
      • setAccessor

        protected final void setAccessor​(AAxis.AChart2DDataAccessor accessor)
        Sets the accessor to the axis of the chart.

        Parameters:
        accessor - the accessor to the axis of the chart.
      • setMajorTickSpacing

        public void setMajorTickSpacing​(double majorTickSpacing)
        This method sets the major tick spacing for label generation.

        Only values between 0.0 and 100.0 are allowed.

        The number that is passed-in represents the distance, measured in values, between each major tick mark. If you have a trace with a range from 0 to 50 and the major tick spacing is set to 10, you will get major ticks next to the following values: 0, 10, 20, 30, 40, 50.

        Note:
        Ticks are free of any multiples of 1000. If the chart contains values between 0 an 1000 and configured a tick of 2 the values 0, 200, 400, 600, 800 and 1000 will highly probable to be displayed. This depends on the size (in pixels) of the Chart2D<. Of course there is a difference: ticks are used in divisions and multiplications: If the internal values are very low and the ticks are very high, huge rounding errors might occur (division by ticks results in very low values a double cannot hit exactly. So prefer setting ticks between 0 an 10 or - if you know your values are very small (e.g. in nano range [10 -9 ]) use a small value (e.g. 2*10 -9 ).

        Specified by:
        setMajorTickSpacing in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        majorTickSpacing - the major tick spacing for label generation.
      • setMinorTickSpacing

        public void setMinorTickSpacing​(double minorTickSpacing)
        This method sets the minor tick spacing for label generation.

        The number that is passed-in represents the distance, measured in values, between each major tick mark. If you have a trace with a range from 0 to 50 and the major tick spacing is set to 10, you will get major ticks next to the following values: 0, 10, 20, 30, 40, 50.

        Note:
        Ticks are free of any powers of 10. There is no difference between setting a tick to 2, 200 or 20000 because ticks cannot break the rule that every scale label has to be visible. If the chart contains values between 0 an 1000 and configured a tick of 2 the values 0, 200, 400, 600, 800 and 1000 will highly probable to be displayed. This depends on the size (in pixels) of the Chart2D<. Of course there is a difference: ticks are used in divisions and multiplications: If the internal values are very low and the ticks are very high, huge rounding errors might occur (division by ticks results in very low values a double cannot hit exactly. So prefer setting ticks between 0 an 10 or - if you know your values are very small (e.g. in nano range [10 -9 ]) use a small value (e.g. 2*10 -9 ).

        Specified by:
        setMinorTickSpacing in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        minorTickSpacing - the minor tick spacing to set.
      • setPaintGrid

        public final void setPaintGrid​(boolean grid)
        Set whether the grid in this dimension should be painted or not.

        Specified by:
        setPaintGrid in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        grid - true if the grid should be painted or false if not.
      • setPaintScale

        public final void setPaintScale​(boolean show)
        Set if the scale for this axis should be shown.

        Specified by:
        setPaintScale in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        show - true if the scale on this axis should be shown, false else.
      • setPixelXLeft

        public final void setPixelXLeft​(int pixelXLeft)
        Description copied from interface: IAxis
        Sets a Range to use for filtering the view to the the connected Axis. Note that it's effect will be affected by the internal IRangePolicy.

        This must only be called from the Chart2D itself!

        Specified by:
        setPixelXLeft in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        pixelXLeft - the left pixel coordinate of this axis in the graphic context of the current paint operation.
        See Also:
        IAxis.setPixelXLeft(int)
      • setPixelXRight

        public final void setPixelXRight​(int pixelXRight)
        Description copied from interface: IAxis
        Sets the right pixel of this axis coordinate in the graphic context of the current paint operation.

        This must only be called from the Chart2D itself!

        Specified by:
        setPixelXRight in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        pixelXRight - the right pixel coordinate of this axis in the graphic context of the current paint operation.
        See Also:
        IAxis.setPixelXRight(int)
      • setPixelYBottom

        public final void setPixelYBottom​(int pixelYBottom)
        Description copied from interface: IAxis
        Sets the bottom pixel of this axis coordinate in the graphic context of the current paint operation.

        This must only be called from the Chart2D itself!

        Specified by:
        setPixelYBottom in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        pixelYBottom - the bottom pixel coordinate of this axis in the graphic context of the current paint operation.
        See Also:
        IAxis.setPixelYBottom(int)
      • setPixelYTop

        public final void setPixelYTop​(int pixelYTop)
        Description copied from interface: IAxis
        Sets the top pixel of this axis coordinate in the graphic context of the current paint operation.

        This must only be called from the Chart2D itself!

        Specified by:
        setPixelYTop in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        pixelYTop - the top pixel coordinate of this axis in the graphic context of the current paint operation.
        See Also:
        IAxis.setPixelYTop(int)
      • setRange

        public final void setRange​(Range range)

        Sets a Range to use for filtering the view to the the connected Axis. Note that it's effect will be affected by the internal IRangePolicy.

        To get full control use:
        setRangePolicy(new <AnARangePolicy>(range);

        Specified by:
        setRange in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        range - Range to use for filtering the view to the the connected Axis.
        See Also:
        getRangePolicy(), IRangePolicy.setRange(Range)
      • ensureInitialized

        protected final void ensureInitialized()
        Ensures that no deadlock / NPE due to a missing internal chart reference may occur.

        Throws:
        java.lang.IllegalStateException - if this axis is not assigned to a chart.
      • setRangePolicy

        public void setRangePolicy​(IRangePolicy rangePolicy)

        Sets the RangePolicy.

        If the given RangePolicy has an unconfigured internal Range ( Range.RANGE_UNBOUNDED) the old internal RangePolicy is taken into account:
        If the old RangePolicy has a configured Range this is transferred to the new RangePolicy.

        A property change event for IAxis.PROPERTY_RANGEPOLICY is fired and receives listeners if a change took place.

        Specified by:
        setRangePolicy in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        rangePolicy - The rangePolicy to set.
      • setVisible

        public void setVisible​(boolean visible)
        Set the visibility of this axis.

        Specified by:
        setVisible in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        visible - true to show, false to hide
      • translateMousePosition

        public double translateMousePosition​(java.awt.event.MouseEvent mouseEvent)
                                      throws java.lang.IllegalArgumentException
        Returns the translation of the mouse event coordinates of the given mouse event to the value within the chart for the dimension (x,y) covered by this axis.

        Note that the mouse event has to be an event fired on the correspondinig chart component!

        Parameters:
        mouseEvent - a mouse event that has been fired on this component.
        Returns:
        the translation of the mouse event coordinates of the given mouse event to the value within the chart for the dimension covered by this axis (x or y) or null if no calculations could be performed as the chart was not painted before.
        Throws:
        java.lang.IllegalArgumentException - if the given mouse event is out of the current graphics context (not a mouse event of the chart component).
      • translatePxToValue

        public double translatePxToValue​(int pixel)
        Description copied from interface: IAxis
        Transforms the given pixel value (which has to be a awt value like MouseEvent.getY() into the chart value.

        Internal use only, the interface does not guarantee that the pixel corresponds to any valid awt pixel value within the chart component.

        Specified by:
        translatePxToValue in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        pixel - a pixel value of the chart component as used by awt.
        Returns:
        the awt pixel value transformed to the chart value.
        See Also:
        IAxis.translatePxToValue(int)
      • translateValueToPx

        public final int translateValueToPx​(double value)
        Description copied from interface: IAxis
        Transforms the given chart data value into the corresponding awt pixel value for the chart.

        Specified by:
        translateValueToPx in interface IAxis<T extends IAxisScalePolicy>
        Parameters:
        value - a chart data value.
        Returns:
        the awt pixel value corresponding to the chart data value.
        See Also:
        IAxis.translateValueToPx(double)