categoryaxis.java

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

JAVA
1,046
字号
                        categoryIndex, ticks.size(), dataArea, edge                    );                    x1 = getCategoryEnd(                        categoryIndex, ticks.size(), dataArea, edge                    );                    y1 = state.getCursor() - this.categoryLabelPositionOffset;                    y0 = y1 - state.getMax();                }                else if (edge == RectangleEdge.BOTTOM) {                    x0 = getCategoryStart(                        categoryIndex, ticks.size(), dataArea, edge                    );                    x1 = getCategoryEnd(                        categoryIndex, ticks.size(), dataArea, edge                    );                     y0 = state.getCursor() + this.categoryLabelPositionOffset;                    y1 = y0 + state.getMax();                }                else if (edge == RectangleEdge.LEFT) {                    y0 = getCategoryStart(                        categoryIndex, ticks.size(), dataArea, edge                    );                    y1 = getCategoryEnd(                        categoryIndex, ticks.size(), dataArea, edge                    );                    x1 = state.getCursor() - this.categoryLabelPositionOffset;                    x0 = x1 - state.getMax();                }                else if (edge == RectangleEdge.RIGHT) {                    y0 = getCategoryStart(                        categoryIndex, ticks.size(), dataArea, edge                    );                    y1 = getCategoryEnd(                        categoryIndex, ticks.size(), dataArea, edge                    );                    x0 = state.getCursor() + this.categoryLabelPositionOffset;                    x1 = x0 - state.getMax();                }                Rectangle2D area = new Rectangle2D.Double(                    x0, y0, (x1 - x0), (y1 - y0)                );                Point2D anchorPoint = RectangleAnchor.coordinates(                    area, position.getCategoryAnchor()                );                TextBlock block = tick.getLabel();                block.draw(                    g2,                     (float) anchorPoint.getX(), (float) anchorPoint.getY(),                     position.getLabelAnchor(),                     (float) anchorPoint.getX(), (float) anchorPoint.getY(),                     position.getAngle()                );                Shape bounds = block.calculateBounds(                    g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(),                     position.getLabelAnchor(),                     (float) anchorPoint.getX(), (float) anchorPoint.getY(),                     position.getAngle()                );                if (plotState != null) {                    EntityCollection entities                         = plotState.getOwner().getEntityCollection();                    if (entities != null) {                        String tooltip                             = (String) this.categoryLabelToolTips.get(                                tick.getCategory()                            );                        entities.add(                            new TickLabelEntity(bounds, tooltip, null)                        );                    }                }                categoryIndex++;            }            if (edge.equals(RectangleEdge.TOP)) {                double h = state.getMax();                state.cursorUp(h);            }            else if (edge.equals(RectangleEdge.BOTTOM)) {                double h = state.getMax();                state.cursorDown(h);            }            else if (edge == RectangleEdge.LEFT) {                double w = state.getMax();                state.cursorLeft(w);            }            else if (edge == RectangleEdge.RIGHT) {                double w = state.getMax();                state.cursorRight(w);            }        }        return state;    }    /**     * Creates a temporary list of ticks that can be used when drawing the axis.     *     * @param g2  the graphics device (used to get font measurements).     * @param state  the axis state.     * @param dataArea  the area inside the axes.     * @param edge  the location of the axis.     *      * @return A list of ticks.     */    public List refreshTicks(Graphics2D g2,                              AxisState state,                             Rectangle2D dataArea,                             RectangleEdge edge) {        List ticks = new java.util.ArrayList();                // sanity check for data area...        if (dataArea.getHeight() <= 0.0 || dataArea.getWidth() < 0.0) {            return ticks;        }        CategoryPlot plot = (CategoryPlot) getPlot();        List categories = plot.getCategories();        double max = 0.0;                        if (categories != null) {            CategoryLabelPosition position                 = this.categoryLabelPositions.getLabelPosition(edge);            float r = this.maximumCategoryLabelWidthRatio;            if (r <= 0.0) {                r = position.getWidthRatio();               }                              float l = 0.0f;            if (position.getWidthType() == CategoryLabelWidthType.CATEGORY) {                l = (float) calculateCategorySize(                    categories.size(), dataArea, edge                );              }            else {                if (RectangleEdge.isLeftOrRight(edge)) {                    l = (float) dataArea.getWidth();                   }                else {                    l = (float) dataArea.getHeight();                   }            }            int categoryIndex = 0;            Iterator iterator = categories.iterator();            while (iterator.hasNext()) {                Comparable category = (Comparable) iterator.next();                TextBlock label = createLabel(category, l * r, edge, g2);                if (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM) {                    max = Math.max(                        max, calculateTextBlockHeight(label, position, g2)                    );                }                else if (edge == RectangleEdge.LEFT                         || edge == RectangleEdge.RIGHT) {                    max = Math.max(                        max, calculateTextBlockWidth(label, position, g2)                    );                }                Tick tick = new CategoryTick(                    category, label,                     position.getLabelAnchor(), position.getRotationAnchor(),                     position.getAngle()                );                ticks.add(tick);                categoryIndex = categoryIndex + 1;            }        }        state.setMax(max);        return ticks;            }    /**     * Creates a label.     *     * @param category  the category.     * @param width  the available width.      * @param edge  the edge on which the axis appears.     * @param g2  the graphics device.     *     * @return A label.     */    protected TextBlock createLabel(Comparable category, float width,                                     RectangleEdge edge, Graphics2D g2) {        TextBlock label = TextUtilities.createTextBlock(            category.toString(), getTickLabelFont(), getTickLabelPaint(),             width, this.maximumCategoryLabelLines, new G2TextMeasurer(g2)        );          return label;     }        /**     * A utility method for determining the width of a text block.     *     * @param block  the text block.     * @param position  the position.     * @param g2  the graphics device.     *     * @return The width.     */    protected double calculateTextBlockWidth(TextBlock block,                                              CategoryLabelPosition position,                                              Graphics2D g2) {                                                            RectangleInsets insets = getTickLabelInsets();        Size2D size = block.calculateDimensions(g2);        Rectangle2D box = new Rectangle2D.Double(            0.0, 0.0, size.getWidth(), size.getHeight()        );        Shape rotatedBox = ShapeUtilities.rotateShape(            box, position.getAngle(), 0.0f, 0.0f        );        double w = rotatedBox.getBounds2D().getWidth()                    + insets.getTop() + insets.getBottom();        return w;            }    /**     * A utility method for determining the height of a text block.     *     * @param block  the text block.     * @param position  the label position.     * @param g2  the graphics device.     *     * @return The height.     */    protected double calculateTextBlockHeight(TextBlock block,                                               CategoryLabelPosition position,                                               Graphics2D g2) {                                                            RectangleInsets insets = getTickLabelInsets();        Size2D size = block.calculateDimensions(g2);        Rectangle2D box = new Rectangle2D.Double(            0.0, 0.0, size.getWidth(), size.getHeight()        );        Shape rotatedBox = ShapeUtilities.rotateShape(            box, position.getAngle(), 0.0f, 0.0f        );        double h = rotatedBox.getBounds2D().getHeight()                    + insets.getTop() + insets.getBottom();        return h;            }    /**     * Creates 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 {            Object clone = super.clone();        return clone;                }        /**     * Tests this axis for equality with an arbitrary object.     *     * @param obj  the object (<code>null</code> permitted).     *     * @return A boolean.     */    public boolean equals(Object obj) {        if (obj == this) {            return true;        }        if (!(obj instanceof CategoryAxis)) {            return false;        }        if (!super.equals(obj)) {            return false;        }        CategoryAxis that = (CategoryAxis) obj;        if (that.lowerMargin != this.lowerMargin) {            return false;        }        if (that.upperMargin != this.upperMargin) {            return false;        }        if (that.categoryMargin != this.categoryMargin) {            return false;        }        if (that.maximumCategoryLabelWidthRatio                 != this.maximumCategoryLabelWidthRatio) {            return false;        }        if (that.categoryLabelPositionOffset                 != this.categoryLabelPositionOffset) {            return false;        }        if (!ObjectUtilities.equal(                that.categoryLabelPositions, this.categoryLabelPositions            )) {            return false;        }        if (!ObjectUtilities.equal(                that.categoryLabelToolTips, this.categoryLabelToolTips            )) {            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;        }    }        /**     * Provides serialization support.     *     * @param stream  the output stream.     *     * @throws IOException  if there is an I/O error.     */    private void writeObject(ObjectOutputStream stream) throws IOException {        stream.defaultWriteObject();    }    /**     * Provides serialization support.     *     * @param stream  the input stream.     *     * @throws IOException  if there is an I/O error.     * @throws ClassNotFoundException  if there is a classpath problem.     */    private void readObject(ObjectInputStream stream)         throws IOException, ClassNotFoundException {        stream.defaultReadObject();    } }

⌨️ 快捷键说明

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