subcategoryaxis.java

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

JAVA
442
字号
     *     * @param g2  the graphics device (<code>null</code> not permitted).     * @param cursor  the cursor location.     * @param plotArea  the area within which the axis should be drawn      *                  (<code>null</code> not permitted).     * @param dataArea  the area within which the plot is being 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()) {            return new AxisState(cursor);        }                if (isAxisLineVisible()) {            drawAxisLine(g2, cursor, dataArea, edge);        }        // draw the category labels and axis label        AxisState state = new AxisState(cursor);        state = drawSubCategoryLabels(            g2, plotArea, dataArea, edge, state, plotState        );        state = drawCategoryLabels(            g2, dataArea, edge, state, plotState        );        state = drawLabel(getLabel(), g2, plotArea, dataArea, edge, state);            return state;    }        /**     * Draws the category labels and returns the updated axis state.     *     * @param g2  the graphics device (<code>null</code> not permitted).     * @param plotArea  the plot area (<code>null</code> not permitted).     * @param dataArea  the area inside the axes (<code>null</code> not      *                  permitted).     * @param edge  the axis location (<code>null</code> not permitted).     * @param state  the axis state (<code>null</code> not permitted).     * @param plotState  collects information about the plot (<code>null</code>      *                   permitted).     *      * @return The updated axis state (never <code>null</code>).     */    protected AxisState drawSubCategoryLabels(Graphics2D g2,                                              Rectangle2D plotArea,                                              Rectangle2D dataArea,                                              RectangleEdge edge,                                              AxisState state,                                              PlotRenderingInfo plotState) {        if (state == null) {            throw new IllegalArgumentException("Null 'state' argument.");        }        g2.setFont(this.subLabelFont);        g2.setPaint(this.subLabelPaint);        CategoryPlot plot = (CategoryPlot) getPlot();        CategoryDataset dataset = plot.getDataset();        int categoryCount = dataset.getColumnCount();        double maxdim = getMaxDim(g2, edge);        for (int categoryIndex = 0; categoryIndex < categoryCount;              categoryIndex++) {            double x0 = 0.0;            double x1 = 0.0;            double y0 = 0.0;            double y1 = 0.0;            if (edge == RectangleEdge.TOP) {                x0 = getCategoryStart(                    categoryIndex, categoryCount, dataArea, edge                );                x1 = getCategoryEnd(                    categoryIndex, categoryCount, dataArea, edge                );                y1 = state.getCursor();                y0 = y1 - maxdim;            }            else if (edge == RectangleEdge.BOTTOM) {                x0 = getCategoryStart(                    categoryIndex, categoryCount, dataArea, edge                );                x1 = getCategoryEnd(                    categoryIndex, categoryCount, dataArea, edge                );                 y0 = state.getCursor();                                   y1 = y0 + maxdim;            }            else if (edge == RectangleEdge.LEFT) {                y0 = getCategoryStart(                    categoryIndex, categoryCount, dataArea, edge                );                y1 = getCategoryEnd(                    categoryIndex, categoryCount, dataArea, edge                );                x1 = state.getCursor();                x0 = x1 - maxdim;            }            else if (edge == RectangleEdge.RIGHT) {                y0 = getCategoryStart(                    categoryIndex, categoryCount, dataArea, edge                );                y1 = getCategoryEnd(                    categoryIndex, categoryCount, dataArea, edge                );                x0 = state.getCursor();                x1 = x0 + maxdim;            }            Rectangle2D area = new Rectangle2D.Double(                x0, y0, (x1 - x0), (y1 - y0)            );            int subCategoryCount = this.subCategories.size();            float width = (float) ((x1 - x0) / subCategoryCount);            float height = (float) ((y1 - y0) / subCategoryCount);            float xx = 0.0f;            float yy = 0.0f;            for (int i = 0; i < subCategoryCount; i++) {                if (RectangleEdge.isTopOrBottom(edge)) {                    xx = (float) (x0 + (i + 0.5) * width);                    yy = (float) area.getCenterY();                }                else {                    xx = (float) area.getCenterX();                    yy = (float) (y0 + (i + 0.5) * height);                                   }                String label = this.subCategories.get(i).toString();                TextUtilities.drawRotatedString(                    label, g2, xx, yy, TextAnchor.CENTER, 0.0,                     TextAnchor.CENTER                );            }        }        if (edge.equals(RectangleEdge.TOP)) {            double h = maxdim;            state.cursorUp(h);        }        else if (edge.equals(RectangleEdge.BOTTOM)) {            double h = maxdim;            state.cursorDown(h);        }        else if (edge == RectangleEdge.LEFT) {            double w = maxdim;            state.cursorLeft(w);        }        else if (edge == RectangleEdge.RIGHT) {            double w = maxdim;            state.cursorRight(w);        }        return state;    }        /**     * Tests the 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 SubCategoryAxis && super.equals(obj)) {            SubCategoryAxis axis = (SubCategoryAxis) obj;            if (!this.subCategories.equals(axis.subCategories)) {                return false;            }            if (!this.subLabelFont.equals(axis.subLabelFont)) {                return false;               }            if (!this.subLabelPaint.equals(axis.subLabelPaint)) {                return false;               }            return true;        }        return false;            }        /**     * 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();        SerialUtilities.writePaint(this.subLabelPaint, stream);    }    /**     * 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();        this.subLabelPaint = SerialUtilities.readPaint(stream);    }  }

⌨️ 快捷键说明

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