⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 categoryaxis.java

📁 大家打开看看啊, 很有用的东西
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                return b0 && b1 && b2;
            }
        }

        return false;

    }

    /**
     * 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 cursor  the cursor location.
     * @param plotArea  the area where the plot and axes will be drawn.
     * @param dataArea  the area inside the axes.
     * @param edge  the location of the axis.
     */
    public void refreshTicks(Graphics2D g2, double cursor,
                             Rectangle2D plotArea, Rectangle2D dataArea,
                             RectangleEdge edge) {

        if (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM) {
            refreshTicksHorizontal(g2, cursor, plotArea, dataArea, edge);
        }
        else if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
            refreshTicksVertical(g2, cursor, plotArea, dataArea, edge);
        }
    }

    /**
     * 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 cursor  the cursor location.
     * @param drawArea  the area where the plot and axes will be drawn.
     * @param plotArea  the area inside the axes.
     * @param edge  the location of the axis.
     */
    public void refreshTicksVertical(Graphics2D g2, double cursor,
                                     Rectangle2D drawArea, Rectangle2D plotArea,
                                     RectangleEdge edge) {

        getTicks().clear();
        CategoryPlot plot = (CategoryPlot) getPlot();
        List categories = plot.getCategories();
        if (categories != null) {
            Font font = getTickLabelFont();
            g2.setFont(font);
            FontRenderContext frc = g2.getFontRenderContext();
            int categoryIndex = 0;
            float xx = 0.0f;
            float yy = 0.0f;
            Iterator iterator = categories.iterator();
            while (iterator.hasNext()) {
                Object category = iterator.next();
                String label = category.toString();
                Rectangle2D labelBounds = font.getStringBounds(label, frc);
                LineMetrics metrics = font.getLineMetrics(label, frc);

                if (edge == RectangleEdge.LEFT) {
                    xx = (float) (cursor - getTickLabelInsets().right - labelBounds.getWidth());
                }
                else {
                    xx = (float) (cursor + getTickLabelInsets().left);
                }
                yy = (float) (getCategoryMiddle(categoryIndex,
                                                categories.size(),
                                                plotArea, edge)
                                                - metrics.getStrikethroughOffset() + 0.5f);
                Tick tick = new Tick(category, label, xx, yy);
                getTicks().add(tick);
                categoryIndex = categoryIndex + 1;
            }
        }
    }

    /**
     * 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 cursor  the cursor location.
     * @param plotArea  the area where the plot and axes will be drawn.
     * @param dataArea  the area inside the axes.
     * @param edge  the location of the axis.
     */
    public void refreshTicksHorizontal(Graphics2D g2, double cursor,
                                       Rectangle2D plotArea, Rectangle2D dataArea,
                                       RectangleEdge edge) {

        this.maxTickLineCount = 1;
        getTicks().clear();
        CategoryPlot categoryPlot = (CategoryPlot) getPlot();
        List categories = categoryPlot.getCategories();
        if (categories != null) {
            Font font = getTickLabelFont();
            g2.setFont(font);
            FontRenderContext frc = g2.getFontRenderContext();
            int categorySkip = 0;
            int categoryIndex = 0;
            float maxWidth = (float) (dataArea.getWidth() / categories.size() * 0.9f);
            float xx = 0.0f;
            float yy = 0.0f;
            Iterator iterator = categories.iterator();
            while (iterator.hasNext()) {
                Object category = iterator.next();

                if (categorySkip != 0) {
                    ++categoryIndex;
                    --categorySkip;
                    continue;
                }

                String label = category.toString();
                Rectangle2D labelBounds = font.getStringBounds(label, frc);
                LineMetrics metrics = font.getLineMetrics(label, frc);
                float catX = (float) getCategoryMiddle(categoryIndex,
                                                       categories.size(),
                                                       dataArea, edge);
                if (isVerticalCategoryLabels()) {
                    xx = (float) (catX + labelBounds.getHeight() / 2 - metrics.getDescent());
                    if (edge == RectangleEdge.TOP) {
                        yy = (float) (cursor - getTickLabelInsets().bottom);
                                                        // - labelBounds.getWidth());
                    }
                    else {
                        yy = (float) (cursor + getTickLabelInsets().top + labelBounds.getWidth());
                    }
                    getTicks().add(new Tick(category, label, xx, yy));
                    if (this.skipCategoryLabelsToFit) {
                        categorySkip = (int) ((labelBounds.getHeight() - maxWidth / 2)
                                             / maxWidth) + 1;
                    }
                }
                else if (labelBounds.getWidth() > maxWidth) {
                    if (this.skipCategoryLabelsToFit) {
                        xx = (float) (catX - maxWidth / 2);
                        if (edge == RectangleEdge.TOP) {
                            yy = (float) (dataArea.getMinY() - getTickLabelInsets().bottom
                                                             - metrics.getDescent()
                                                             - metrics.getLeading());
                        }
                        else {
                            yy = (float) (dataArea.getMaxY() + getTickLabelInsets().top
                                                             + metrics.getHeight()
                                                             - metrics.getDescent());
                        }
                        getTicks().add(new Tick(category, label, xx, yy));
                        categorySkip = (int) ((labelBounds.getWidth() - maxWidth / 2)
                                             / maxWidth) + 1;
                    }
                    else {
                        String[] labels = breakLine(label, (int) maxWidth, frc);
                        Tick[] ts = new Tick[labels.length];
                        for (int i = 0; i < labels.length; i++) {
                            labelBounds = font.getStringBounds(labels[i], frc);
                            xx = (float) (catX - labelBounds.getWidth() / 2);
                            if (edge == RectangleEdge.TOP) {
                                yy = (float) (dataArea.getMinY() - getTickLabelInsets().bottom
                                              - (labels.length - i) * metrics.getHeight()
                                              + metrics.getAscent());
                            }
                            else {
                                yy = (float) (dataArea.getMaxY() + getTickLabelInsets().top
                                                                 + (i + 1) * (metrics.getHeight())
                                                                 - metrics.getDescent());
                            }
                            ts[i] = new Tick(category, labels[i], xx, yy);
                        }
                        if (labels.length > this.maxTickLineCount) {
                            this.maxTickLineCount = labels.length;
                        }
                        getTicks().add(ts);
                    }
                }
                else {
                    xx = (float) (catX - labelBounds.getWidth() / 2);
                    if (edge == RectangleEdge.TOP) {
                        yy = (float) (dataArea.getMinY() - getTickLabelInsets().bottom
                                                         - metrics.getLeading()
                                                         - metrics.getDescent());
                    }
                    else {
                        yy = (float) (dataArea.getMaxY() + getTickLabelInsets().top
                                                         + metrics.getHeight()
                                                         - metrics.getDescent());
                    }
                    getTicks().add(new Tick(category, label, xx, yy));
                }
                categoryIndex = categoryIndex + 1;
            }
        }

    }

    /**
     * A utility method for determining the height of the tallest tick label.
     *
     * @param g2  the graphics device.
     * @param drawArea  the drawing area.
     * @param vertical  a flag indicating whether the tick labels are drawn vertically.
     *
     * @return The maximum tick label height.
     */
    protected double getMaxTickLabelHeight(Graphics2D g2, Rectangle2D drawArea, boolean vertical) {
        Font font = getTickLabelFont();
        g2.setFont(font);
        FontRenderContext frc = g2.getFontRenderContext();
        double maxHeight = 0.0;
        if (vertical) {
            Iterator iterator = getTicks().iterator();
            while (iterator.hasNext()) {
                Tick tick = (Tick) iterator.next();
                Rectangle2D labelBounds = font.getStringBounds(tick.getText(), frc);
                if (labelBounds.getWidth() > maxHeight) {
                    maxHeight = labelBounds.getWidth();
                }
            }
        }
        else {
            LineMetrics metrics = font.getLineMetrics("Sample", frc);
            maxHeight = (metrics.getHeight() * this.maxTickLineCount)
                        - (metrics.getDescent() * (this.maxTickLineCount - 1));
        }
        return maxHeight;
    }

    /**
     * Returns the maximum width of the ticks in the working list (that is set
     * up by refreshTicks()).
     *
     * @param g2  the graphics device.
     * @param plotArea  the area within which the plot is to be drawn.
     *
     * @return the maximum width of the ticks in the working list.
     */
    protected double getMaxTickLabelWidth(Graphics2D g2, Rectangle2D plotArea) {

        double maxWidth = 0.0;
        Font font = getTickLabelFont();
        FontRenderContext frc = g2.getFontRenderContext();

        Iterator iterator = getTicks().iterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            if (obj instanceof Tick) {
                Tick tick = (Tick) obj;
                Rectangle2D labelBounds = font.getStringBounds(tick.getText(), frc);
                if (labelBounds.getWidth() > maxWidth) {
                    maxWidth = labelBounds.getWidth();
                }
            }
            else {
                Tick[] ts = (Tick[]) obj;
                for (int i = 0; i < ts.length; i++) {
                    Rectangle2D labelBounds = font.getStringBounds(ts[i].getText(), frc);
                    if (labelBounds.getWidth() > maxWidth) {
                        maxWidth = labelBounds.getWidth();
                    }
                }

            }
        }
        return maxWidth;

    }

    /**
     * Breaks a line
     *
     * @param text  string at break
     * @param areaWidth  width of tick area
     * @param frc  current Font Renderer Context
     *
     * @return array of breaked strings
     */
    private String[] breakLine(String text, int areaWidth, FontRenderContext frc) {

        ArrayList textList = new ArrayList(5);

        int currWidth = areaWidth;
        AttributedString as = new AttributedString(text, getTickLabelFont().getAttributes());
        AttributedCharacterIterator aci = as.getIterator();
        AffineTransform affine = new AffineTransform();
        for (;;) {
            LineBreakMeasurer measurer = new LineBreakMeasurer(aci, frc);
            int maxWidth = 0, offset = 0;
            TextLayout layout = measurer.nextLayout(currWidth);
            while (layout != null) {
                textList.add(text.substring(offset, offset + layout.getCharacterCount()));
                int width = layout.getOutline(affine).getBounds().width;
                if (maxWidth < width) {
                    maxWidth = width;
                }
                offset += layout.getCharacterCount();
                layout = measurer.nextLayout(currWidth);
            }
            if (maxWidth > areaWidth) {
                currWidth -= maxWidth - currWidth;
                if (currWidth > 0) {
                    textList.clear();
                    continue;
                }
            }
            break;
        }

        String[] texts = new String[textList.size()];
        return (String[]) textList.toArray(texts);

    }

}

⌨️ 快捷键说明

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