valueaxis.java

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

JAVA
1,561
字号
     *     * @param ticks  the ticks.     * @param g2  the graphics device.     * @param drawArea  the area within which the plot and axes should be drawn.     * @param vertical  a flag that indicates whether or not the tick labels      *                  are 'vertical'.     *     * @return The height of the tallest tick label.     */    protected double findMaximumTickLabelHeight(List ticks,                                                Graphics2D g2,                                                 Rectangle2D drawArea,                                                 boolean vertical) {                                                            RectangleInsets insets = getTickLabelInsets();        Font font = getTickLabelFont();        double maxHeight = 0.0;        if (vertical) {            FontMetrics fm = g2.getFontMetrics(font);            Iterator iterator = ticks.iterator();            while (iterator.hasNext()) {                Tick tick = (Tick) iterator.next();                Rectangle2D labelBounds = TextUtilities.getTextBounds(                    tick.getText(), g2, fm                );                if (labelBounds.getWidth() + insets.getTop()                         + insets.getBottom() > maxHeight) {                    maxHeight = labelBounds.getWidth()                                 + insets.getTop() + insets.getBottom();                }            }        }        else {            LineMetrics metrics = font.getLineMetrics(                "ABCxyz", g2.getFontRenderContext()            );            maxHeight = metrics.getHeight()                         + insets.getTop() + insets.getBottom();        }        return maxHeight;            }    /**     * A utility method for determining the width of the widest tick label.     *     * @param ticks  the ticks.     * @param g2  the graphics device.     * @param drawArea  the area within which the plot and axes should be drawn.     * @param vertical  a flag that indicates whether or not the tick labels      *                  are 'vertical'.     *     * @return The width of the tallest tick label.     */    protected double findMaximumTickLabelWidth(List ticks,                                                Graphics2D g2,                                                Rectangle2D drawArea,                                                boolean vertical) {                                                           RectangleInsets insets = getTickLabelInsets();        Font font = getTickLabelFont();        double maxWidth = 0.0;        if (!vertical) {            FontMetrics fm = g2.getFontMetrics(font);            Iterator iterator = ticks.iterator();            while (iterator.hasNext()) {                Tick tick = (Tick) iterator.next();                Rectangle2D labelBounds = TextUtilities.getTextBounds(                    tick.getText(), g2, fm                );                if (labelBounds.getWidth() + insets.getLeft()                         + insets.getRight() > maxWidth) {                    maxWidth = labelBounds.getWidth()                                + insets.getLeft() + insets.getRight();                }            }        }        else {            LineMetrics metrics = font.getLineMetrics(                "ABCxyz", g2.getFontRenderContext()            );            maxWidth = metrics.getHeight()                        + insets.getTop() + insets.getBottom();        }        return maxWidth;            }    /**     * Returns a flag that controls the direction of values on the axis.     * <P>     * For a regular axis, values increase from left to right (for a horizontal     * axis) and bottom to top (for a vertical axis).  When the axis is     * 'inverted', the values increase in the opposite direction.     *     * @return The flag.     */    public boolean isInverted() {        return this.inverted;    }    /**     * Sets a flag that controls the direction of values on the axis, and     * notifies registered listeners that the axis has changed.     *     * @param flag  the flag.     */    public void setInverted(boolean flag) {        if (this.inverted != flag) {            this.inverted = flag;            notifyListeners(new AxisChangeEvent(this));        }    }    /**     * Returns the flag that controls whether or not the axis range is      * automatically adjusted to fit the data values.     *     * @return The flag.     */    public boolean isAutoRange() {        return this.autoRange;    }    /**     * Sets a flag that determines whether or not the axis range is     * automatically adjusted to fit the data, and notifies registered     * listeners that the axis has been modified.     *     * @param auto  the new value of the flag.     */    public void setAutoRange(boolean auto) {        setAutoRange(auto, true);    }    /**     * Sets the auto range attribute.  If the <code>notify</code> flag is set,      * an {@link AxisChangeEvent} is sent to registered listeners.     *     * @param auto  the flag.     * @param notify  notify listeners?     */    protected void setAutoRange(boolean auto, boolean notify) {        if (this.autoRange != auto) {            this.autoRange = auto;            if (this.autoRange) {                autoAdjustRange();            }            if (notify) {                notifyListeners(new AxisChangeEvent(this));            }        }    }    /**     * Returns the minimum size allowed for the axis range when it is      * automatically calculated.     *     * @return The minimum range.     */    public double getAutoRangeMinimumSize() {        return this.autoRangeMinimumSize;    }    /**     * Sets the auto range minimum size and sends an {@link AxisChangeEvent}      * to all registered listeners.     *     * @param size  the size.     */    public void setAutoRangeMinimumSize(double size) {        setAutoRangeMinimumSize(size, true);    }    /**     * Sets the minimum size allowed for the axis range when it is      * automatically calculated.     * <p>     * If requested, an {@link AxisChangeEvent} is forwarded to all registered      * listeners.     *     * @param size  the new minimum.     * @param notify  notify listeners?     */    public void setAutoRangeMinimumSize(double size, boolean notify) {        // check argument...        if (size <= 0.0) {            throw new IllegalArgumentException(                "NumberAxis.setAutoRangeMinimumSize(double): must be > 0.0.");        }        // make the change...        if (this.autoRangeMinimumSize != size) {            this.autoRangeMinimumSize = size;            if (this.autoRange) {                autoAdjustRange();            }            if (notify) {                notifyListeners(new AxisChangeEvent(this));            }        }    }    /**     * Returns the lower margin for the axis, expressed as a percentage of the      * axis range.  This controls the space added to the lower end of the axis      * when the axis range is automatically calculated (it is ignored when the      * axis range is set explicitly). The default value is 0.05 (five percent).     *     * @return The lower margin.     */    public double getLowerMargin() {        return this.lowerMargin;    }    /**     * Sets the lower margin for the axis (as a percentage of the axis range)      * and sends an {@link AxisChangeEvent} to all registered listeners.  This     * margin is added only when the axis range is auto-calculated - if you set      * the axis range manually, the margin is ignored.     *     * @param margin  the margin percentage (for example, 0.05 is five percent).     */    public void setLowerMargin(double margin) {        this.lowerMargin = margin;        if (isAutoRange()) {            autoAdjustRange();        }        notifyListeners(new AxisChangeEvent(this));    }    /**     * Returns the upper margin for the axis, expressed as a percentage of the      * axis range.  This controls the space added to the lower end of the axis      * when the axis range is automatically calculated (it is ignored when the      * axis range is set explicitly). The default value is 0.05 (five percent).     *     * @return The upper margin.     */    public double getUpperMargin() {        return this.upperMargin;    }    /**     * Sets the upper margin for the axis (as a percentage of the axis range)      * and sends an {@link AxisChangeEvent} to all registered listeners.  This      * margin is added only when the axis range is auto-calculated - if you set     * the axis range manually, the margin is ignored.     *     * @param margin  the margin percentage (for example, 0.05 is five percent).     */    public void setUpperMargin(double margin) {        this.upperMargin = margin;        if (isAutoRange()) {            autoAdjustRange();        }        notifyListeners(new AxisChangeEvent(this));    }    /**     * Returns the fixed auto range.     *     * @return The length.     */    public double getFixedAutoRange() {        return this.fixedAutoRange;    }    /**     * Sets the fixed auto range for the axis.     *     * @param length  the range length.     */    public void setFixedAutoRange(double length) {        this.fixedAutoRange = length;        notifyListeners(new AxisChangeEvent(this));    }    /**     * Returns the lower bound of the axis range.     *     * @return The lower bound.     */    public double getLowerBound() {        return this.range.getLowerBound();    }    /**     * Sets the lower bound for the axis range.  An {@link AxisChangeEvent} is      * sent to all registered listeners.     *     * @param min  the new minimum.     */    public void setLowerBound(double min) {        if (this.range.getUpperBound() > min) {            setRange(new Range(min, this.range.getUpperBound()));                    }        else {            setRange(new Range(min, min + 1.0));                                }    }    /**     * Returns the upper bound for the axis range.     *     * @return The upper bound.     */    public double getUpperBound() {        return this.range.getUpperBound();    }    /**     * Sets the upper bound for the axis range.  An {@link AxisChangeEvent} is      * sent to all registered listeners.     *     * @param max  the new maximum.     */    public void setUpperBound(double max) {        if (this.range.getLowerBound() < max) {            setRange(new Range(this.range.getLowerBound(), max));        }        else {            setRange(max - 1.0, max);        }    }    /**     * Returns the range for the axis.     *     * @return The axis range (never <code>null</code>).     */    public Range getRange() {        return this.range;    }    /**     * Sets the range attribute and sends an {@link AxisChangeEvent} to all      * registered listeners.  As a side-effect, the auto-range flag is set to      * <code>false</code>.     *     * @param range  the range (<code>null</code> not permitted).     */    public void setRange(Range range) {        // defer argument checking        setRange(range, true, true);    }    /**     * Sets the range for the axis, if requested, sends an      * {@link AxisChangeEvent} to all registered listeners.  As a side-effect,      * the auto-range flag is set to <code>false</code> (optional).     *     * @param range  the range (<code>null</code> not permitted).     * @param turnOffAutoRange  a flag that controls whether or not the auto      *                          range is turned off.              * @param notify  a flag that controls whether or not listeners are      *                notified.     */    public void setRange(Range range, boolean turnOffAutoRange,                          boolean notify) {        if (range == null) {            throw new IllegalArgumentException("Null 'range' argument.");        }        if (turnOffAutoRange) {            this.autoRange = false;        }        this.range = range;        if (notify) {            notifyListeners(new AxisChangeEvent(this));        }    }    /**     * Sets the axis range and sends an {@link AxisChangeEvent} to all      * registered listeners.  As a side-effect, the auto-range flag is set to      * <code>false</code>.     *     * @param lower  the lower axis limit.     * @param upper  the upper axis limit.     */    public void setRange(double lower, double upper) {        setRange(new Range(lower, upper));    }

⌨️ 快捷键说明

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