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

📄 abstractcategoryitemrenderer.java

📁 jfreechart-1
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

        Comparable category = marker.getKey();
        CategoryDataset dataset = plot.getDataset(plot.getIndexOf(this));
        int columnIndex = dataset.getColumnIndex(category);
        if (columnIndex < 0) {
            return;   
        }
        PlotOrientation orientation = plot.getOrientation();
        Rectangle2D bounds = null;
        if (marker.getDrawAsLine()) {
            double v = axis.getCategoryMiddle(
                columnIndex, dataset.getColumnCount(),
                dataArea, plot.getDomainAxisEdge()
            );
            Line2D line = null;
            if (orientation == PlotOrientation.HORIZONTAL) {
                line = new Line2D.Double(
                    dataArea.getMinX(), v, dataArea.getMaxX(), v
                );
            }
            else if (orientation == PlotOrientation.VERTICAL) {
                line = new Line2D.Double(
                    v, dataArea.getMinY(), v, dataArea.getMaxY()
                );
            }

            g2.setPaint(marker.getPaint());
            g2.setStroke(marker.getStroke());
            g2.draw(line);
            bounds = line.getBounds2D();
        }
        else {
            double v0 = axis.getCategoryStart(
                columnIndex, dataset.getColumnCount(),
                dataArea, plot.getDomainAxisEdge()
            );
            double v1 = axis.getCategoryEnd(
                columnIndex, dataset.getColumnCount(),
                dataArea, plot.getDomainAxisEdge()
            );
            Rectangle2D area = null;
            if (orientation == PlotOrientation.HORIZONTAL) {
                area = new Rectangle2D.Double(
                    dataArea.getMinX(), v0, dataArea.getWidth(), (v1 - v0)
                );
            }
            else if (orientation == PlotOrientation.VERTICAL) {
                area = new Rectangle2D.Double(
                    v0, dataArea.getMinY(), (v1 - v0), dataArea.getHeight()
                );
            }
            g2.setPaint(marker.getPaint());
            g2.fill(area);
            bounds = area;
        }
        
        String label = marker.getLabel();
        RectangleAnchor anchor = marker.getLabelAnchor();
        if (label != null) {
            Font labelFont = marker.getLabelFont();
            g2.setFont(labelFont);
            g2.setPaint(marker.getLabelPaint());
            Point2D coordinates = calculateDomainMarkerTextAnchorPoint(
                g2, orientation, dataArea, bounds, 
                marker.getLabelOffset(), marker.getLabelOffsetType(), anchor
            );
            TextUtilities.drawAlignedString(
                label, g2, 
                (float) coordinates.getX(), (float) coordinates.getY(), 
                marker.getLabelTextAnchor()
            );
        }
    }

    /**
     * Draws a marker for the range axis.
     *
     * @param g2  the graphics device (not <code>null</code>).
     * @param plot  the plot (not <code>null</code>).
     * @param axis  the range axis (not <code>null</code>).
     * @param marker  the marker to be drawn (not <code>null</code>).
     * @param dataArea  the area inside the axes (not <code>null</code>).
     */
    public void drawRangeMarker(Graphics2D g2,
                                CategoryPlot plot,
                                ValueAxis axis,
                                Marker marker,
                                Rectangle2D dataArea) {

        if (marker instanceof ValueMarker) {
            ValueMarker vm = (ValueMarker) marker;
            double value = vm.getValue();
            Range range = axis.getRange();

            if (!range.contains(value)) {
                return;
            }

            PlotOrientation orientation = plot.getOrientation();
            double v = axis.valueToJava2D(
                value, dataArea, plot.getRangeAxisEdge()
            );
            Line2D line = null;
            if (orientation == PlotOrientation.HORIZONTAL) {
                line = new Line2D.Double(
                    v, dataArea.getMinY(), v, dataArea.getMaxY()
                );
            }
            else if (orientation == PlotOrientation.VERTICAL) {
                line = new Line2D.Double(
                    dataArea.getMinX(), v, dataArea.getMaxX(), v
                );
            }

            g2.setPaint(marker.getPaint());
            g2.setStroke(marker.getStroke());
            g2.draw(line);
        
            String label = marker.getLabel();
            RectangleAnchor anchor = marker.getLabelAnchor();
            if (label != null) {
                Font labelFont = marker.getLabelFont();
                g2.setFont(labelFont);
                g2.setPaint(marker.getLabelPaint());
                Point2D coordinates = calculateRangeMarkerTextAnchorPoint(
                    g2, orientation, dataArea, line.getBounds2D(), 
                    marker.getLabelOffset(), LengthAdjustmentType.EXPAND, anchor
                );
                TextUtilities.drawAlignedString(
                    label, g2, 
                    (float) coordinates.getX(), (float) coordinates.getY(), 
                    marker.getLabelTextAnchor()
                );
            }
        }
        else if (marker instanceof IntervalMarker) {
            
            IntervalMarker im = (IntervalMarker) marker;
            double start = im.getStartValue();
            double end = im.getEndValue();
            Range range = axis.getRange();
            if (!(range.intersects(start, end))) {
                return;
            }
            
            // don't draw beyond the axis range...
            start = range.constrain(start);
            end = range.constrain(end);
            
            double v0 = axis.valueToJava2D(
                start, dataArea, plot.getRangeAxisEdge()
            );
            double v1 = axis.valueToJava2D(
                end, dataArea, plot.getRangeAxisEdge()
            );
            
            PlotOrientation orientation = plot.getOrientation();
            Rectangle2D rect = null;
            if (orientation == PlotOrientation.HORIZONTAL) {
                rect = new Rectangle2D.Double(
                    v0, dataArea.getMinY(), v1 - v0, dataArea.getHeight()
                );            
            }
            else if (orientation == PlotOrientation.VERTICAL) {
                rect = new Rectangle2D.Double(
                    dataArea.getMinX(), Math.min(v0, v1), 
                    dataArea.getWidth(), Math.abs(v1 - v0)
                );
            }
            Paint p = marker.getPaint();
            if (p instanceof GradientPaint) {
                GradientPaint gp = (GradientPaint) p;
                GradientPaintTransformer t = im.getGradientPaintTransformer();
                if (t != null) {
                    gp = t.transform(gp, rect);  
                }
                g2.setPaint(gp);
            }
            else {
                g2.setPaint(p);
            }
            g2.fill(rect);

            String label = marker.getLabel();
            RectangleAnchor anchor = marker.getLabelAnchor();
            if (label != null) {
                Font labelFont = marker.getLabelFont();
                g2.setFont(labelFont);
                g2.setPaint(marker.getLabelPaint());
                Point2D coordinates = calculateRangeMarkerTextAnchorPoint(
                    g2, orientation, dataArea, 
                    rect, marker.getLabelOffset(), 
                    marker.getLabelOffsetType(), anchor
                );
                TextUtilities.drawAlignedString(
                    label, g2, 
                    (float) coordinates.getX(), (float) coordinates.getY(), 
                    marker.getLabelTextAnchor()
                );
            }
            
        }

    }

    /**
     * Calculates the (x, y) coordinates for drawing the label for a marker on
     * the range axis.
     * 
     * @param g2  the graphics device.
     * @param orientation  the plot orientation.
     * @param dataArea  the data area.
     * @param markerArea  the rectangle surrounding the marker.
     * @param markerOffset  the marker offset.
     * @param labelOffsetType  the label offset type.
     * @param anchor  the label anchor.
     * 
     * @return The coordinates for drawing the marker label.
     */
    protected Point2D calculateDomainMarkerTextAnchorPoint(Graphics2D g2, 
                                      PlotOrientation orientation,
                                      Rectangle2D dataArea,
                                      Rectangle2D markerArea,
                                      RectangleInsets markerOffset,
                                      LengthAdjustmentType labelOffsetType,
                                      RectangleAnchor anchor) {
                                                     
        Rectangle2D anchorRect = null;
        if (orientation == PlotOrientation.HORIZONTAL) {
            anchorRect = markerOffset.createAdjustedRectangle(
                markerArea, LengthAdjustmentType.CONTRACT, labelOffsetType
            );
        }
        else if (orientation == PlotOrientation.VERTICAL) {
            anchorRect = markerOffset.createAdjustedRectangle(
                markerArea, labelOffsetType, LengthAdjustmentType.CONTRACT
            );
        }
        return RectangleAnchor.coordinates(anchorRect, anchor);
        
    }

    /**
     * Calculates the (x, y) coordinates for drawing a marker label.
     * 
     * @param g2  the graphics device.
     * @param orientation  the plot orientation.
     * @param dataArea  the data area.
     * @param markerArea  the rectangle surrounding the marker.
     * @param markerOffset  the marker offset.
     * @param labelOffsetType  the label offset type.
     * @param anchor  the label anchor.
     * 
     * @return The coordinates for drawing the marker label.
     */
    protected Point2D calculateRangeMarkerTextAnchorPoint(Graphics2D g2, 
                                      PlotOrientation orientation,
                                      Rectangle2D dataArea,
                                      Rectangle2D markerArea,
                                      RectangleInsets markerOffset,
                                      LengthAdjustmentType labelOffsetType,
                                      RectangleAnchor anchor) {
                                                     
        Rectangle2D anchorRect = null;
        if (orientation == PlotOrientation.HORIZONTAL) {
            anchorRect = markerOffset.createAdjustedRectangle(
                markerArea, labelOffsetType, LengthAdjustmentType.CONTRACT
            );
        }
        else if (orientation == PlotOrientation.VERTICAL) {
            anchorRect = markerOffset.createAdjustedRectangle(
                markerArea, LengthAdjustmentType.CONTRACT, labelOffsetType
            );
        }
        return RectangleAnchor.coordinates(anchorRect, anchor);
        
    }
    
    /**
     * Returns a legend item for a series.
     *
     * @param datasetIndex  the dataset index (zero-based).
     * @param series  the series index (zero-based).
     *
     * @return The legend item.
     */
    public LegendItem getLegendItem(int datasetIndex, int series) {

        CategoryPlot p = getPlot();
        if (p == null) {
            return null;
        }

        CategoryDataset dataset;
        dataset = p.getDataset(datasetIndex);
        String label = this.legendItemLabelGenerator.generateLabel(
            dataset, series
        );
        String description = label;
        String toolTipText = null; 
        if (this.legendItemToolTipGenerator != null) {
            toolTipText = this.legendItemToolTipGenerator.generateLabel(
                dataset, series
            );   
        }
        String urlText = null;
        if (this.legendItemURLGenerator != null) {
            urlText = this.legendItemURLGenerator.generateLabel(
                dataset, series
            );   
        }
        Shape shape = getSeriesShape(series);
        Paint paint = getSeriesPaint(series);
        Paint outlinePaint = getSeriesOutlinePaint(series);
        Stroke outlineStroke = getSeriesOutlineStroke(series);

        return new LegendItem(label, description, toolTipText, urlText, 
            shape, paint, outlineStroke, outlinePaint);

    }

    /**
     * Tests this renderer for equality with another object.
     *
     * @param obj  the object.
     *
     * @return <code>true</code> or <code>false</code>.
     */
    public boolean equals(Object obj) {

        if (obj == this) {
            return true;
        }
        if (!(obj instanceof AbstractCategoryItemRenderer)) {
            return false;
        }
        if (!super.equals(obj)) {
            return false;
        }

        AbstractCategoryItemRenderer that = (AbstractCategoryItemRenderer) obj;

        if (!ObjectUtilities.equal(this.itemLabelGenerator, 
                that.itemLabelGenerator)) {
            return false;
        }
        if (!ObjectUtilities.equal(
            this.itemLabelGeneratorList, that.itemLabelGeneratorList
        )) {
            return false;
        }
        if (!ObjectUtilities.equal(
            this.baseItemLabelGenerator, that.baseItemLabelGenerator
        )) {
            return false;
        }
        if (!ObjectUtilities.equal(
            this.toolTipGenerator, that.toolTipGenerator
        )) {
            return false;
        }
        if (!ObjectUtilities.equal(
            this.toolTipGeneratorList, that.toolTipGeneratorList

⌨️ 快捷键说明

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