numberaxis.java

来自「JfreeChart 常用图表例子」· Java 代码 · 共 1,302 行 · 第 1/4 页

JAVA
1,302
字号
            selectVerticalAutoTickUnit(g2, dataArea, 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.     *     * @param g2  the graphics device.     * @param dataArea  the area defined by the axes.     * @param edge  the axis location.     */   protected void selectHorizontalAutoTickUnit(Graphics2D g2,                                               Rectangle2D dataArea,                                               RectangleEdge edge) {        double tickLabelWidth = estimateMaximumTickLabelWidth(            g2, getTickUnit()        );        // start with the current tick unit...        TickUnitSource tickUnits = getStandardTickUnits();        TickUnit unit1 = tickUnits.getCeilingTickUnit(getTickUnit());        double unit1Width = lengthToJava2D(unit1.getSize(), dataArea, edge);        // then extrapolate...        double guess = (tickLabelWidth / unit1Width) * unit1.getSize();        NumberTickUnit unit2             = (NumberTickUnit) tickUnits.getCeilingTickUnit(guess);        double unit2Width = lengthToJava2D(unit2.getSize(), dataArea, edge);        tickLabelWidth = estimateMaximumTickLabelWidth(g2, unit2);        if (tickLabelWidth > unit2Width) {            unit2 = (NumberTickUnit) tickUnits.getLargerTickUnit(unit2);        }        setTickUnit(unit2, false, false);    }    /**     * 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.     *     * @param g2  the graphics device.     * @param dataArea  the area in which the plot should be drawn.     * @param edge  the axis location.     */    protected void selectVerticalAutoTickUnit(Graphics2D g2,                                               Rectangle2D dataArea,                                               RectangleEdge edge) {        double tickLabelHeight = estimateMaximumTickLabelHeight(g2);        // start with the current tick unit...        TickUnitSource tickUnits = getStandardTickUnits();        TickUnit unit1 = tickUnits.getCeilingTickUnit(getTickUnit());        double unitHeight = lengthToJava2D(unit1.getSize(), dataArea, edge);        // then extrapolate...        double guess = (tickLabelHeight / unitHeight) * unit1.getSize();                NumberTickUnit unit2             = (NumberTickUnit) tickUnits.getCeilingTickUnit(guess);        double unit2Height = lengthToJava2D(unit2.getSize(), dataArea, edge);        tickLabelHeight = estimateMaximumTickLabelHeight(g2);        if (tickLabelHeight > unit2Height) {            unit2 = (NumberTickUnit) tickUnits.getLargerTickUnit(unit2);        }        setTickUnit(unit2, false, false);    }        /**     * Calculates the positions of the tick labels for the axis, storing the      * results in the tick label list (ready for drawing).     *     * @param g2  the graphics device.     * @param state  the axis state.     * @param dataArea  the area in which the plot should be drawn.     * @param edge  the location of the axis.     *      * @return A list of ticks.     *     */    public List refreshTicks(Graphics2D g2,                              AxisState state,                             Rectangle2D dataArea,                             RectangleEdge edge) {        List result = new java.util.ArrayList();        if (RectangleEdge.isTopOrBottom(edge)) {            result = refreshTicksHorizontal(g2, dataArea, edge);        }        else if (RectangleEdge.isLeftOrRight(edge)) {            result = refreshTicksVertical(g2, dataArea, edge);        }        return result;    }    /**     * Calculates the positions of the tick labels for the axis, storing the      * results in the tick label list (ready for drawing).     *     * @param g2  the graphics device.     * @param dataArea  the area in which the data should be drawn.     * @param edge  the location of the axis.     *      * @return A list of ticks.     */    protected List refreshTicksHorizontal(Graphics2D g2,                                          Rectangle2D dataArea,                                          RectangleEdge edge) {        List result = new java.util.ArrayList();        Font tickLabelFont = getTickLabelFont();        g2.setFont(tickLabelFont);                if (isAutoTickUnitSelection()) {            selectAutoTickUnit(g2, dataArea, edge);        }        double size = getTickUnit().getSize();        int count = calculateVisibleTickCount();        double lowestTickValue = calculateLowestVisibleTickValue();        if (count <= ValueAxis.MAXIMUM_TICK_COUNT) {            for (int i = 0; i < count; i++) {                double currentTickValue = lowestTickValue + (i * size);                String tickLabel;                NumberFormat formatter = getNumberFormatOverride();                if (formatter != null) {                    tickLabel = formatter.format(currentTickValue);                }                else {                    tickLabel = getTickUnit().valueToString(currentTickValue);                }                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 NumberTick(                    new Double(currentTickValue), tickLabel, anchor,                     rotationAnchor, angle                );                result.add(tick);            }        }        return result;    }    /**     * Calculates the positions of the tick labels for the axis, storing the      * results in the tick label list (ready for drawing).     *     * @param g2  the graphics device.     * @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,                                        Rectangle2D dataArea,                                        RectangleEdge edge) {        List result = new java.util.ArrayList();        result.clear();        Font tickLabelFont = getTickLabelFont();        g2.setFont(tickLabelFont);        if (isAutoTickUnitSelection()) {            selectAutoTickUnit(g2, dataArea, edge);        }        double size = getTickUnit().getSize();        int count = calculateVisibleTickCount();        double lowestTickValue = calculateLowestVisibleTickValue();        if (count <= ValueAxis.MAXIMUM_TICK_COUNT) {            for (int i = 0; i < count; i++) {                double currentTickValue = lowestTickValue + (i * size);                String tickLabel;                NumberFormat formatter = getNumberFormatOverride();                if (formatter != null) {                    tickLabel = formatter.format(currentTickValue);                }                else {                    tickLabel = getTickUnit().valueToString(currentTickValue);                }                TextAnchor anchor = null;                TextAnchor rotationAnchor = null;                double angle = 0.0;                if (isVerticalTickLabels()) {                    if (edge == RectangleEdge.LEFT) {                         anchor = TextAnchor.BOTTOM_CENTER;                        rotationAnchor = TextAnchor.BOTTOM_CENTER;                        angle = -Math.PI / 2.0;                    }                    else {                        anchor = TextAnchor.BOTTOM_CENTER;                        rotationAnchor = TextAnchor.BOTTOM_CENTER;                        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 NumberTick(                    new Double(currentTickValue), tickLabel, anchor,                     rotationAnchor, angle                );                result.add(tick);            }        }        return result;    }        /**     * Returns a clone of the axis.     *      * @return A clone     *      * @throws CloneNotSupportedException if some component of the axis does      *         not support cloning.     */    public Object clone() throws CloneNotSupportedException {        NumberAxis clone = (NumberAxis) super.clone();        if (this.numberFormatOverride != null) {            clone.numberFormatOverride                 = (NumberFormat) this.numberFormatOverride.clone();        }        return clone;    }    /**     * Tests an object for equality with this instance.     *      * @param obj  the object.     *      * @return A boolean.     */        public boolean equals(Object obj) {                   if (obj == this) {            return true;        }        if (!(obj instanceof NumberAxis)) {            return false;        }        if (!super.equals(obj)) {            return false;        }        NumberAxis that = (NumberAxis) obj;                if (this.autoRangeIncludesZero != that.autoRangeIncludesZero) {            return false;        }        if (this.autoRangeStickyZero != that.autoRangeStickyZero) {            return false;        }        if (!ObjectUtilities.equal(this.tickUnit, that.tickUnit)) {            return false;        }        if (!ObjectUtilities.equal(this.numberFormatOverride,                 that.numberFormatOverride)) {            return false;        }                return true;     }        /**     * Returns a hash code for this object.     *      * @return A hash code.     */    public int hashCode() {        if (getLabel() != null) {            return getLabel().hashCode();        }        else {            return 0;        }    }}

⌨️ 快捷键说明

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