⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dateaxis.java

📁 jfreechart安装程序和使用说明
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            if (!isHiddenValue(tickDate.getTime())) {
                // work out the value, label and position
                String tickLabel;
                DateFormat formatter = getDateFormatOverride();
                if (formatter != null) {
                    tickLabel = formatter.format(tickDate);
                }
                else {
                    tickLabel = this.tickUnit.dateToString(tickDate);
                }
                TextAnchor anchor = null;
                TextAnchor rotationAnchor = null;
                double angle = 0.0;
                if (isVerticalTickLabels()) {
                    anchor = TextAnchor.CENTER_RIGHT;
                    rotationAnchor = TextAnchor.CENTER_RIGHT;
                    if (edge == RectangleEdge.TOP) {
                        angle = Math.PI / 2.0;
                    }
                    else {
                        angle = -Math.PI / 2.0;
                    }
                }
                else {
                    if (edge == RectangleEdge.TOP) {
                        anchor = TextAnchor.BOTTOM_CENTER;
                        rotationAnchor = TextAnchor.BOTTOM_CENTER;
                    }
                    else {
                        anchor = TextAnchor.TOP_CENTER;
                        rotationAnchor = TextAnchor.TOP_CENTER;
                    }
                }

                Tick tick = new DateTick(tickDate, tickLabel, anchor, rotationAnchor, angle);
                result.add(tick);
                tickDate = unit.addToDate(tickDate);
            }
            else {
                tickDate = unit.rollDate(tickDate);
                continue;
            }

            // could add a flag to make the following correction optional...
            switch (unit.getUnit()) {

                case (DateTickUnit.MILLISECOND) :
                case (DateTickUnit.SECOND) :
                case (DateTickUnit.MINUTE) :
                case (DateTickUnit.HOUR) :
                case (DateTickUnit.DAY) :
                    break;
                case (DateTickUnit.MONTH) :
                    tickDate = calculateDateForPosition(new Month(tickDate), this.tickMarkPosition);
                    break;
                case(DateTickUnit.YEAR) :
                    tickDate = calculateDateForPosition(new Year(tickDate), this.tickMarkPosition);
                    break;

                default: break;

            }

        }
        return result;

    }

    /**
     * Recalculates the ticks for the date axis.
     *
     * @param g2  the graphics device.
     * @param cursor  the cursor location.
     * @param plotArea  the area in which the plot and the axes should be drawn.
     * @param dataArea  the area in which the plot should be drawn.
     * @param edge  the location of the axis.
     *
     * @return A list of ticks.
     */
    protected List refreshTicksVertical(Graphics2D g2,
                                        double cursor,
                                        Rectangle2D plotArea,
                                        Rectangle2D dataArea,
                                        RectangleEdge edge) {

        List result = new java.util.ArrayList();

        Font tickLabelFont = getTickLabelFont();
        g2.setFont(tickLabelFont);

        if (isAutoTickUnitSelection()) {
            selectAutoTickUnit(g2, plotArea, dataArea, edge);
        }
        DateTickUnit unit = getTickUnit();
        Date tickDate = calculateLowestVisibleTickValue(unit);
        //Date upperDate = calculateHighestVisibleTickValue(unit);
        Date upperDate = getMaximumDate();
        while (tickDate.before(upperDate)) {

            if (!isHiddenValue(tickDate.getTime())) {
                // work out the value, label and position
                String tickLabel;
                DateFormat formatter = getDateFormatOverride();
                if (formatter != null) {
                    tickLabel = formatter.format(tickDate);
                }
                else {
                    tickLabel = this.tickUnit.dateToString(tickDate);
                }
                TextAnchor anchor = null;
                TextAnchor rotationAnchor = null;
                double angle = 0.0;
                if (this.isVerticalTickLabels()) {
                    anchor = TextAnchor.BOTTOM_CENTER;
                    rotationAnchor = TextAnchor.BOTTOM_CENTER;
                    if (edge == RectangleEdge.LEFT) {
                        angle = -Math.PI / 2.0;
                    }
                    else {
                        angle = Math.PI / 2.0;
                    }
                }
                else {
                    if (edge == RectangleEdge.LEFT) {
                        anchor = TextAnchor.CENTER_RIGHT;
                        rotationAnchor = TextAnchor.CENTER_RIGHT;
                    }
                    else {
                        anchor = TextAnchor.CENTER_LEFT;
                        rotationAnchor = TextAnchor.CENTER_LEFT;
                    }
                }

                Tick tick = new DateTick(tickDate, tickLabel, anchor, rotationAnchor, angle);
                result.add(tick);
                tickDate = unit.addToDate(tickDate);
            }
            else {
                tickDate = unit.rollDate(tickDate);
            }
        }
        return result;
    }

    /**
     * Draws the axis on a Java 2D graphics device (such as the screen or a printer).
     *
     * @param g2  the graphics device (<code>null</code> not permitted).
     * @param cursor  the cursor location.
     * @param plotArea  the area within which the axes and data should be drawn (<code>null</code> 
     *                  not permitted).
     * @param dataArea  the area within which the data should be drawn (<code>null</code> not 
     *                  permitted).
     * @param edge  the location of the axis (<code>null</code> not permitted).
     * @param plotState  collects information about the plot (<code>null</code> permitted).
     *
     * @return the axis state (never <code>null</code>).
     */
    public AxisState draw(Graphics2D g2, 
                          double cursor,
                          Rectangle2D plotArea, 
                          Rectangle2D dataArea, 
                          RectangleEdge edge,
                          PlotRenderingInfo plotState) {

        // if the axis is not visible, don't draw it...
        if (!isVisible()) {
            AxisState state = new AxisState(cursor);
            // even though the axis is not visible, we need to refresh ticks in case the grid
            // is being drawn...
            List ticks = refreshTicks(g2, state, plotArea, dataArea, edge);
            state.setTicks(ticks);
            return state;
        }

        // draw the tick marks and labels...
        AxisState state = drawTickMarksAndLabels(g2, cursor, plotArea, dataArea, edge);

        // draw the axis label (note that 'state' is passed in *and* returned)...
        state = drawLabel(getLabel(), g2, plotArea, dataArea, edge, state);

        return state;

    }

    /**
     * Zooms in on the current range.
     *
     * @param lowerPercent  the new lower bound.
     * @param upperPercent  the new upper bound.
     */
    public void zoomRange(double lowerPercent, double upperPercent) {
        double start = this.timeline.toTimelineValue(new Date((long) getRange().getLowerBound()));
        double length = (this.timeline.toTimelineValue((long) getRange().getUpperBound()) 
            - this.timeline.toTimelineValue((long) getRange().getLowerBound()));
        Range adjusted = null;
        if (isInverted()) {
            adjusted = new DateRange(
                this.timeline.toMillisecond((long) (start + (length * (1 - upperPercent)))),
                this.timeline.toMillisecond((long) (start + (length * (1 - lowerPercent))))
            );
        }
        else {
            adjusted = new DateRange(this.timeline.toMillisecond(
                (long) (start + length * lowerPercent)),
                this.timeline.toMillisecond((long) (start + length * upperPercent))
            );
        }
        setRange(adjusted);
    } 
    
    /**
     * Tests an object for equality with this instance.
     *
     * @param obj  the object to test.
     *
     * @return A boolean.
     */
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj instanceof DateAxis) {
            DateAxis axis = (DateAxis) obj;
            boolean b0 = ObjectUtils.equal(this.tickUnit, axis.tickUnit);
            boolean b1 = ObjectUtils.equal(this.dateFormatOverride, axis.dateFormatOverride);
            boolean b2 = ObjectUtils.equal(this.tickMarkPosition, axis.tickMarkPosition);
            boolean b3 = ObjectUtils.equal(this.timeline, axis.timeline);
            return b0 && b1 && b2 && b3;
        }
        return false;
    }

    /**
     * Returns a clone of the object.
     *
     * @return A clone.
     *
     * @throws CloneNotSupportedException if some component of the axis does not support cloning.
     */
    public Object clone() throws CloneNotSupportedException {

        DateAxis clone = (DateAxis) super.clone();

        // 'dateTickUnit' is immutable : no need to clone
        if (this.dateFormatOverride != null) {
            clone.dateFormatOverride = (DateFormat) this.dateFormatOverride.clone();
        }
        // 'tickMarkPosition' is immutable : no need to clone

        return clone;

    }

    //// DEPRECATED CODE //////////////////////////////////////////////////////////////////////////

    /**
     * Creates a date axis. A timeline is specified for the axis. This allows special
     * transformations to occur between a domain of values and the values included
     * in the axis.
     *
     * @see org.jfree.chart.axis.SegmentedTimeline
     *
     * @param label  the axis label (<code>null</code> permitted).
     * @param timeline  the underlying timeline to use for the axis.
     * 
     * @deprecated Use DateAxis(String) then set the time line separately.
     */
    public DateAxis(String label, Timeline timeline) {
        this(label, TimeZone.getDefault());
        this.timeline = timeline;
    }
    
    /**
     * Translates the data value to the display coordinates (Java 2D User Space)
     * of the chart.
     *
     * @param value  the date to be plotted.
     * @param area  the rectangle (in Java2D space) where the data is to be plotted.
     * @param edge  the axis location.
     *
     * @return the coordinate corresponding to the supplied data value.
     * 
     * @deprecated Use valueToJava2D instead.
     */
    public double translateValueToJava2D(double value, Rectangle2D area, RectangleEdge edge) {
        return valueToJava2D(value, area, 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.
     *
     * @param java2DValue  the coordinate in Java2D space.
     * @param area  the rectangle (in Java2D space) where the data is to be plotted.
     * @param edge  the axis location.
     *
     * @return A data value.
     * 
     * @deprecated Use java2DToValue instead.
     */
    public double translateJava2DToValue(double java2DValue, Rectangle2D area, RectangleEdge edge) {
        return java2DToValue(java2DValue, area, edge);
    }

    /**
     * Translates a date to Java2D coordinates, based on the range displayed by
     * this axis for the specified data area.
     *
     * @param date  the date.
     * @param area  the rectangle (in Java2D space) where the data is to be plotted.
     * @param edge  the axis location.
     *
     * @return the coordinate corresponding to the supplied date.
     * 
     * @deprecated Use dataToJava2D() instead.
     */
    public double translateDateToJava2D(Date date, Rectangle2D area, RectangleEdge edge) {
        return dateToJava2D(date, area, edge);
    }   
    
            
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -