numberaxis.java

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

JAVA
1,302
字号
     * @return The number formatter (possibly <code>null</code>).     */    public NumberFormat getNumberFormatOverride() {        return this.numberFormatOverride;    }    /**     * Sets the number format override.  If this is non-null, then it will be      * used to format the numbers on the axis.     *     * @param formatter  the number formatter (<code>null</code> permitted).     */    public void setNumberFormatOverride(NumberFormat formatter) {        this.numberFormatOverride = formatter;        notifyListeners(new AxisChangeEvent(this));    }    /**     * Returns the (optional) marker band for the axis.     *     * @return The marker band (possibly <code>null</code>).     */    public MarkerAxisBand getMarkerBand() {        return this.markerBand;    }    /**     * Sets the marker band for the axis.     * <P>     * The marker band is optional, leave it set to <code>null</code> if you      * don't require it.     *     * @param band the new band (<code>null<code> permitted).     */    public void setMarkerBand(MarkerAxisBand band) {        this.markerBand = band;        notifyListeners(new AxisChangeEvent(this));    }    /**     * Configures the axis to work with the specified plot.  If the axis has     * auto-scaling, then sets the maximum and minimum values.     */    public void configure() {        if (isAutoRange()) {            autoAdjustRange();        }    }    /**     * Rescales the axis to ensure that all data is visible.     */    protected void autoAdjustRange() {        Plot plot = getPlot();        if (plot == null) {            return;  // no plot, no data        }        if (plot instanceof ValueAxisPlot) {            ValueAxisPlot vap = (ValueAxisPlot) plot;            Range r = vap.getDataRange(this);            if (r == null) {                r = new Range(DEFAULT_LOWER_BOUND, DEFAULT_UPPER_BOUND);            }                        double upper = r.getUpperBound();            double lower = r.getLowerBound();            if (this.rangeType == RangeType.POSITIVE) {                lower = Math.max(0.0, lower);                upper = Math.max(0.0, upper);            }            else if (this.rangeType == RangeType.NEGATIVE) {                lower = Math.min(0.0, lower);                upper = Math.min(0.0, upper);                               }                        if (getAutoRangeIncludesZero()) {                lower = Math.min(lower, 0.0);                upper = Math.max(upper, 0.0);            }            double range = upper - lower;            // if fixed auto range, then derive lower bound...            double fixedAutoRange = getFixedAutoRange();            if (fixedAutoRange > 0.0) {                lower = upper - fixedAutoRange;            }            else {                // ensure the autorange is at least <minRange> in size...                double minRange = getAutoRangeMinimumSize();                if (range < minRange) {                    double expand = (minRange - range) / 2;                    upper = upper + expand;                    lower = lower - expand;                    if (this.rangeType == RangeType.POSITIVE) {                        if (lower < 0.0) {                            upper = upper - lower;                            lower = 0.0;                        }                    }                    else if (this.rangeType == RangeType.NEGATIVE) {                        if (upper > 0.0) {                            lower = lower - upper;                            upper = 0.0;                        }                    }                }                if (getAutoRangeStickyZero()) {                    if (upper <= 0.0) {                        upper = Math.min(0.0, upper + getUpperMargin() * range);                    }                    else {                        upper = upper + getUpperMargin() * range;                    }                    if (lower >= 0.0) {                        lower = Math.max(0.0, lower - getLowerMargin() * range);                    }                    else {                        lower = lower - getLowerMargin() * range;                    }                }                else {                    upper = upper + getUpperMargin() * range;                    lower = lower - getLowerMargin() * range;                }            }            setRange(new Range(lower, upper), false, false);        }    }    /**     * Converts a data value to a coordinate in Java2D space, assuming that the     * axis runs along one edge of the specified dataArea.     * <p>     * Note that it is possible for the coordinate to fall outside the plotArea.     *     * @param value  the data value.     * @param area  the area for plotting the data.     * @param edge  the axis location.     *     * @return The Java2D coordinate.     */    public double valueToJava2D(double value, Rectangle2D area,                                 RectangleEdge edge) {                Range range = getRange();        double axisMin = range.getLowerBound();        double axisMax = range.getUpperBound();        double min = 0.0;        double max = 0.0;        if (RectangleEdge.isTopOrBottom(edge)) {            min = area.getX();            max = area.getMaxX();        }        else if (RectangleEdge.isLeftOrRight(edge)) {            max = area.getMinY();            min = area.getMaxY();        }        if (isInverted()) {            return max                    - ((value - axisMin) / (axisMax - axisMin)) * (max - min);        }        else {            return min                    + ((value - axisMin) / (axisMax - axisMin)) * (max - min);        }    }    /**     * Converts a coordinate in Java2D space to the corresponding data value,     * assuming that the axis runs along one edge of the specified dataArea.     *     * @param java2DValue  the coordinate in Java2D space.     * @param area  the area in which the data is plotted.     * @param edge  the location.     *     * @return The data value.     */    public double java2DToValue(double java2DValue, Rectangle2D area,                                 RectangleEdge edge) {                Range range = getRange();        double axisMin = range.getLowerBound();        double axisMax = range.getUpperBound();        double min = 0.0;        double max = 0.0;        if (RectangleEdge.isTopOrBottom(edge)) {            min = area.getX();            max = area.getMaxX();        }        else if (RectangleEdge.isLeftOrRight(edge)) {            min = area.getMaxY();            max = area.getY();        }        if (isInverted()) {            return axisMax                    - (java2DValue - min) / (max - min) * (axisMax - axisMin);        }        else {            return axisMin                    + (java2DValue - min) / (max - min) * (axisMax - axisMin);        }    }    /**     * Calculates the value of the lowest visible tick on the axis.     *     * @return The value of the lowest visible tick on the axis.     */    protected double calculateLowestVisibleTickValue() {        double unit = getTickUnit().getSize();        double index = Math.ceil(getRange().getLowerBound() / unit);        return index * unit;    }    /**     * Calculates the value of the highest visible tick on the axis.     *     * @return The value of the highest visible tick on the axis.     */    protected double calculateHighestVisibleTickValue() {        double unit = getTickUnit().getSize();        double index = Math.floor(getRange().getUpperBound() / unit);        return index * unit;    }    /**     * Calculates the number of visible ticks.     *     * @return The number of visible ticks on the axis.     */    protected int calculateVisibleTickCount() {        double unit = getTickUnit().getSize();        Range range = getRange();        return (int) (Math.floor(range.getUpperBound() / unit)                      - Math.ceil(range.getLowerBound() / unit) + 1);    }    /**     * 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) {        AxisState state = null;        // if the axis is not visible, don't draw it...        if (!isVisible()) {            state = new AxisState(cursor);            // even though the axis is not visible, we need ticks for the             // gridlines...            List ticks = refreshTicks(g2, state, dataArea, edge);             state.setTicks(ticks);            return state;        }        // draw the tick marks and labels...        state = drawTickMarksAndLabels(g2, cursor, plotArea, dataArea, edge);//        // draw the marker band (if there is one)...//        if (getMarkerBand() != null) {//            if (edge == RectangleEdge.BOTTOM) {//                cursor = cursor - getMarkerBand().getHeight(g2);//            }//            getMarkerBand().draw(g2, plotArea, dataArea, 0, cursor);//        }                // draw the axis label...        state = drawLabel(getLabel(), g2, plotArea, dataArea, edge, state);        return state;            }    /**     * Creates the standard tick units.     * <P>     * If you don't like these defaults, create your own instance of TickUnits     * and then pass it to the setStandardTickUnits() method in the     * NumberAxis class.     *     * @return The standard tick units.     */    public static TickUnitSource createStandardTickUnits() {        TickUnits units = new TickUnits();        DecimalFormat df0 = new DecimalFormat("0.00000000");        DecimalFormat df1 = new DecimalFormat("0.0000000");        DecimalFormat df2 = new DecimalFormat("0.000000");        DecimalFormat df3 = new DecimalFormat("0.00000");        DecimalFormat df4 = new DecimalFormat("0.0000");        DecimalFormat df5 = new DecimalFormat("0.000");        DecimalFormat df6 = new DecimalFormat("0.00");        DecimalFormat df7 = new DecimalFormat("0.0");        DecimalFormat df8 = new DecimalFormat("#,##0");

⌨️ 快捷键说明

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