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

📄 meterlegend.java

📁 jfreechart安装程序和使用说明
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (this.showCritical) {
            legendCount++;
        }
        if (this.showWarning) {
            legendCount++;
        }
        if (this.showNormal) {
            legendCount++;
        }

        LegendItem[] legendItems = new LegendItem[legendCount];
        Color[] legendItemColors = new Color[legendCount];

        int currentItem = 0;
        String label = this.legendText
            + (data.getValue() != null ? ("   Current Value: " + data.getValue().toString()) : "");
        legendItems[currentItem] = new LegendItem(label, label, null, true, null, null, null, null);
        legendItemColors[currentItem] = null;  // no color
        currentItem++;
        if (updateInformation(meterPlot, data, MeterPlot.FULL_DATA_RANGE,
            currentItem, legendItems, legendItemColors)) {
            currentItem++;
        }
        if (this.showCritical && updateInformation(meterPlot, data,
            MeterPlot.CRITICAL_DATA_RANGE, currentItem, legendItems, legendItemColors)) {
            currentItem++;
        }
        if (this.showWarning && updateInformation(meterPlot, data,
            MeterPlot.WARNING_DATA_RANGE, currentItem, legendItems, legendItemColors)) {
            currentItem++;
        }
        if (this.showNormal && updateInformation(meterPlot, data,
            MeterPlot.NORMAL_DATA_RANGE, currentItem, legendItems, legendItemColors)) {
            currentItem++;
        }

        if (legendItems != null) {

            Rectangle2D legendArea = new Rectangle2D.Double();
            double availableWidth = available.getWidth();
            double availableHeight = available.getHeight();

            // the translation point for the origin of the drawing system
            Point2D translation = new Point2D.Double();

            // Create buffer for individual rectangles within the legend
            DrawableLegendItem[] items = new DrawableLegendItem[legendItems.length];
            g2.setFont(getItemFont());

            // Compute individual rectangles in the legend, translation point
            // as well as the bounding box for the legend.
            if (horizontal) {
                double xstart = available.getX() + getOuterGap().getLeftSpace(availableWidth);
                double xlimit = available.getMaxX()
                                + getOuterGap().getRightSpace(availableWidth) - 1;
                double maxRowWidth = 0;
                double xoffset = 0;
                double rowHeight = 0;
                double totalHeight = 0;
                boolean startingNewRow = true;

                for (int i = 0; i < legendItems.length; i++) {
                    items[i] = createLegendItem(g2, legendItems[i], xoffset, totalHeight);
                    if ((!startingNewRow)
                        && (items[i].getX() + items[i].getWidth() + xstart > xlimit)) {
                        maxRowWidth = Math.max(maxRowWidth, xoffset);
                        xoffset = 0;
                        totalHeight += rowHeight;
                        i--;
                        startingNewRow = true;
                    }
                    else {
                        rowHeight = Math.max(rowHeight, items[i].getHeight());
                        xoffset += items[i].getWidth();
                        startingNewRow = false;
                    }
                }

                maxRowWidth = Math.max(maxRowWidth, xoffset);
                totalHeight += rowHeight;

                // Create the bounding box
                legendArea = new Rectangle2D.Double(0, 0, maxRowWidth, totalHeight);

                // The yloc point is the variable part of the translation point
                // for horizontal legends. xloc is constant.
                 double yloc = (inverted)
                    ? available.getMaxY() - totalHeight
                                          - getOuterGap().getBottomSpace(availableHeight)
                    : available.getY() + getOuterGap().getTopSpace(availableHeight);
                double xloc = available.getX() + available.getWidth() / 2 - maxRowWidth / 2;

                // Create the translation point
                translation = new Point2D.Double(xloc, yloc);
            }
            else {  // vertical...
                double totalHeight = 0;
                double maxWidth = 0;
                g2.setFont(getItemFont());
                for (int i = 0; i < items.length; i++) {
                    items[i] = createLegendItem(g2, legendItems[i], 0, totalHeight);
                    totalHeight += items[i].getHeight();
                    maxWidth = Math.max(maxWidth, items[i].getWidth());
                }

                // Create the bounding box
                legendArea = new Rectangle2D.Float(0, 0, (float) maxWidth, (float) totalHeight);

                // The xloc point is the variable part of the translation point
                // for vertical legends. yloc is constant.
                double xloc = (inverted)
                    ? available.getMaxX() - maxWidth - getOuterGap().getRightSpace(availableWidth)
                    : available.getX() + getOuterGap().getLeftSpace(availableWidth);
                double yloc = available.getY() + (available.getHeight() / 2) - (totalHeight / 2);

                // Create the translation point
                translation = new Point2D.Double(xloc, yloc);
            }

            // Move the origin of the drawing to the appropriate location
            g2.translate(translation.getX(), translation.getY());

            // Draw the legend's bounding box
            g2.setPaint(getBackgroundPaint());
            g2.fill(legendArea);
            g2.setPaint(getOutlinePaint());
            g2.setStroke(getOutlineStroke());
            g2.draw(legendArea);

            // Draw individual series elements
            for (int i = 0; i < items.length; i++) {
                Color color = legendItemColors[i];
                if (color != null) {
                    g2.setPaint(color);
                    g2.fill(items[i].getMarker());
                }
                g2.setPaint(getItemPaint());
                g2.drawString(items[i].getItem().getLabel(),
                              (float) items[i].getLabelPosition().getX(),
                              (float) items[i].getLabelPosition().getY());
            }

            // translate the origin back to what it was prior to drawing the
            // legend
            g2.translate(-translation.getX(), -translation.getY());

            if (horizontal) {
                // The remaining drawing area bounding box will have the same
                // x origin, width and height independent of the anchor's
                // location. The variable is the y coordinate. If the anchor is
                // SOUTH, the y coordinate is simply the original y coordinate
                // of the available area. If it is NORTH, we adjust original y
                // by the total height of the legend and the initial gap.
                double yy = available.getY();
                double yloc = (inverted) ? yy
                                         : yy + legendArea.getHeight()
                                              + getOuterGap().getBottomSpace(availableHeight);

                // return the remaining available drawing area
                return new Rectangle2D.Double(available.getX(), yloc, availableWidth,
                    availableHeight - legendArea.getHeight()
                                    - getOuterGap().getTopSpace(availableHeight)
                                    - getOuterGap().getBottomSpace(availableHeight));
            }
            else {
                // The remaining drawing area bounding box will have the same
                // y origin, width and height independent of the anchor's
                // location. The variable is the x coordinate. If the anchor is
                // EAST, the x coordinate is simply the original x coordinate
                // of the available area. If it is WEST, we adjust original x
                // by the total width of the legend and the initial gap.
                double xloc = (inverted) ? available.getX()
                                         : available.getX()
                                           + legendArea.getWidth()
                                           + getOuterGap().getLeftSpace(availableWidth)
                                           + getOuterGap().getRightSpace(availableWidth);


                // return the remaining available drawing area
                return new Rectangle2D.Double(xloc, available.getY(),
                    availableWidth - legendArea.getWidth()
                                   - getOuterGap().getLeftSpace(availableWidth)
                                   - getOuterGap().getRightSpace(availableWidth),
                    availableHeight);
            }
        }
        else {
            return available;
        }
    }

    /**
     * Creates a legend item
     *
     * @param graphics  the graphics device.
     * @param item  the legend item.
     * @param x  the x coordinate.
     * @param y  the y coordinate.
     *
     * @return the legend item.
     */
    private DrawableLegendItem createLegendItem(Graphics graphics,
                                                LegendItem item, double x, double y) {

        int innerGap = 2;
        FontMetrics fm = graphics.getFontMetrics();
        LineMetrics lm = fm.getLineMetrics(item.getLabel(), graphics);
        float textHeight = lm.getHeight();

        DrawableLegendItem drawable = new DrawableLegendItem(item);

        float xloc = (float) (x + innerGap + 1.15f * textHeight);
        float yloc = (float) (y + innerGap + (textHeight - lm.getLeading() - lm.getDescent()));

        drawable.setLabelPosition(new Point2D.Float(xloc, yloc));

        float boxDim = textHeight * 0.70f;
        xloc = (float) (x + innerGap + 0.15f * textHeight);
        yloc = (float) (y + innerGap + 0.15f * textHeight);

        drawable.setMarker(new Rectangle2D.Float(xloc, yloc, boxDim, boxDim));

        float width = (float) (drawable.getLabelPosition().getX() - x
                               + fm.stringWidth(item.getLabel()) + 0.5 * textHeight);

        float height = 2 * innerGap + textHeight;
        drawable.setBounds(x, y, width, height);
        return drawable;

    }

}

⌨️ 快捷键说明

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