abstractxyitemrenderer.java

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

JAVA
1,452
字号
                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 = rangeAxis.getRange();            if (!(range.intersects(start, end))) {                return;            }            // don't draw beyond the axis range...            start = range.constrain(start);            end = range.constrain(end);                        double v0 = rangeAxis.valueToJava2D(                start, dataArea, plot.getRangeAxisEdge()            );            double v1 = rangeAxis.valueToJava2D(                end, dataArea, plot.getRangeAxisEdge()            );            PlotOrientation orientation = plot.getOrientation();            Rectangle2D rect = null;            if (orientation == PlotOrientation.HORIZONTAL) {                rect = new Rectangle2D.Double(                    Math.min(v0, v1), dataArea.getMinY(),                     Math.abs(v1 - v0), dataArea.getHeight()                );            }            else if (orientation == PlotOrientation.VERTICAL) {                rect = new Rectangle2D.Double(                    dataArea.getMinX(), Math.min(v0, v1),                     dataArea.getWidth(), Math.abs(v0 - v1)                );            }            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 a marker label.     *     * @param g2  the graphics device.     * @param orientation  the plot orientation.     * @param dataArea  the data area.     * @param markerArea  the marker area.     * @param markerOffset  the marker offset.     * @param anchor  the label anchor.     *     * @return The coordinates for drawing the marker label.     */    private Point2D calculateRangeMarkerTextAnchorPoint(Graphics2D g2,                                      PlotOrientation orientation,                                      Rectangle2D dataArea,                                      Rectangle2D markerArea,                                      RectangleInsets markerOffset,                                      LengthAdjustmentType labelOffsetForRange,                                      RectangleAnchor anchor) {        Rectangle2D anchorRect = null;        if (orientation == PlotOrientation.HORIZONTAL) {            anchorRect = markerOffset.createAdjustedRectangle(                markerArea, labelOffsetForRange, LengthAdjustmentType.CONTRACT            );        }        else if (orientation == PlotOrientation.VERTICAL) {            anchorRect = markerOffset.createAdjustedRectangle(                markerArea, LengthAdjustmentType.CONTRACT, labelOffsetForRange            );        }        return RectangleAnchor.coordinates(anchorRect, anchor);    }    /**     * Returns a clone of the renderer.     *     * @return A clone.     *     * @throws CloneNotSupportedException if the renderer does not support      *         cloning.     */    protected Object clone() throws CloneNotSupportedException {        AbstractXYItemRenderer clone = (AbstractXYItemRenderer) super.clone();        // 'plot' : just retain reference, not a deep copy        if (this.itemLabelGenerator != null                 && this.itemLabelGenerator instanceof PublicCloneable) {            PublicCloneable pc = (PublicCloneable) this.itemLabelGenerator;            clone.itemLabelGenerator = (XYItemLabelGenerator) pc.clone();        }        return clone;    }    /**     * 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 == null) {            return false;        }        if (obj == this) {            return true;        }        if (!(obj instanceof AbstractXYItemRenderer)) {            return false;        }        AbstractXYItemRenderer renderer = (AbstractXYItemRenderer) obj;        if (!super.equals(obj)) {            return false;        }        if (!ObjectUtilities.equal(            this.itemLabelGenerator, renderer.itemLabelGenerator        )) {            return false;        }        if (!ObjectUtilities.equal(this.urlGenerator, renderer.urlGenerator)) {            return false;        }        return true;    }    /**     * Returns the drawing supplier from the plot.     *     * @return The drawing supplier (possibly <code>null</code>).     */    public DrawingSupplier getDrawingSupplier() {        DrawingSupplier result = null;        XYPlot p = getPlot();        if (p != null) {            result = p.getDrawingSupplier();        }        return result;    }    /**     * Considers the current (x, y) coordinate and updates the crosshair point      * if it meets the criteria (usually means the (x, y) coordinate is the      * closest to the anchor point so far).     *     * @param crosshairState  the crosshair state (<code>null</code> permitted,      *                        but the method does nothing in that case).     * @param x  the x-value (in data space).     * @param y  the y-value (in data space).     * @param transX  the x-value translated to Java2D space.     * @param transY  the y-value translated to Java2D space.     * @param orientation  the plot orientation (<code>null</code> not      *                     permitted).     */    protected void updateCrosshairValues(CrosshairState crosshairState,                                         double x, double y, double transX,                                          double transY,                                         PlotOrientation orientation) {        if (orientation == null) {            throw new IllegalArgumentException("Null 'orientation' argument.");        }        if (crosshairState != null) {            // do we need to update the crosshair values?            if (this.plot.isDomainCrosshairLockedOnData()) {                if (this.plot.isRangeCrosshairLockedOnData()) {                    // both axes                    crosshairState.updateCrosshairPoint(                        x, y, transX, transY, orientation                    );                }                else {                    // just the domain axis...                    crosshairState.updateCrosshairX(x);                }            }            else {                if (this.plot.isRangeCrosshairLockedOnData()) {                    // just the range axis...                    crosshairState.updateCrosshairY(y);                }            }        }    }        /**     * Draws an item label.     *     * @param g2  the graphics device.     * @param orientation  the orientation.     * @param dataset  the dataset.     * @param series  the series index (zero-based).     * @param item  the item index (zero-based).     * @param x  the x coordinate (in Java2D space).     * @param y  the y coordinate (in Java2D space).     * @param negative  indicates a negative value (which affects the item      *                  label position).     */    protected void drawItemLabel(Graphics2D g2,                                  PlotOrientation orientation,                                 XYDataset dataset,                                  int series,                                  int item,                                 double x,                                  double y,                                  boolean negative) {                                             XYItemLabelGenerator generator = getItemLabelGenerator(series, item);        if (generator != null) {            Font labelFont = getItemLabelFont(series, item);            Paint paint = getItemLabelPaint(series, item);            g2.setFont(labelFont);            g2.setPaint(paint);            String label = generator.generateLabel(dataset, series, item);            // get the label position..            ItemLabelPosition position = null;            if (!negative) {                position = getPositiveItemLabelPosition(series, item);            }            else {                position = getNegativeItemLabelPosition(series, item);            }            // work out the label anchor point...            Point2D anchorPoint = calculateLabelAnchorPoint(                position.getItemLabelAnchor(), x, y, orientation            );            TextUtilities.drawRotatedString(                label, g2, (float) anchorPoint.getX(),                 (float) anchorPoint.getY(),                position.getTextAnchor(), position.getAngle(),                 position.getRotationAnchor()            );        }    }        /**     * Draws all the annotations  for the specified layer.     *      * @param g2  the graphics device.     * @param dataArea  the data area.     * @param domainAxis  the domain axis.     * @param rangeAxis  the range axis.     * @param layer  the layer.     * @param info  the plot rendering info.     */    public void drawAnnotations(Graphics2D g2,                                 Rectangle2D dataArea,                                 ValueAxis domainAxis,                                 ValueAxis rangeAxis,                                 Layer layer,                                 PlotRenderingInfo info) {                Iterator iterator = null;        if (layer.equals(Layer.FOREGROUND)) {            iterator = this.foregroundAnnotations.iterator();        }        else if (layer.equals(Layer.BACKGROUND)) {            iterator = this.backgroundAnnotations.iterator();        }        else {            // should not get here            throw new RuntimeException("Unknown layer.");        }        while (iterator.hasNext()) {            XYAnnotation annotation = (XYAnnotation) iterator.next();            annotation.draw(                g2, this.plot, dataArea, domainAxis, rangeAxis, 0, info            );        }            }    /**     * Adds an entity to the collection.     *      * @param entities  the entity collection being populated.     * @param area  the entity area (if <code>null</code> a default will be      *              used).     * @param dataset  the dataset.     * @param series  the series.     * @param item  the item.     * @param entityX  the entity's center x-coordinate in user space.     * @param entityY  the entity's center y-coordinate in user space.     */    protected void addEntity(EntityCollection entities, Shape area,                              XYDataset dataset, int series, int item,                             double entityX, double entityY) {        if (!getItemCreateEntity(series, item)) {            return;        }        if (area == null) {            area = new Ellipse2D.Double(                entityX - this.defaultEntityRadius,                 entityY - this.defaultEntityRadius,                 this.defaultEntityRadius * 2, this.defaultEntityRadius * 2            );        }        String tip = null;        XYToolTipGenerator generator = getToolTipGenerator(series, item);        if (generator != null) {            tip = generator.generateToolTip(dataset, series, item);        }        String url = null;        if (getURLGenerator() != null) {            url = getURLGenerator().generateURL(dataset, series, item);        }        XYItemEntity entity = new XYItemEntity(            area, dataset, series, item, tip, url        );        entities.add(entity);    }}

⌨️ 快捷键说明

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