standardxyitemrenderer.java

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

JAVA
1,034
字号
     * @param item  the item index (zero-based).     * @param crosshairState  crosshair information for the plot      *                        (<code>null</code> permitted).     * @param pass  the pass index.     */    public void drawItem(Graphics2D g2,                         XYItemRendererState state,                         Rectangle2D dataArea,                         PlotRenderingInfo info,                         XYPlot plot,                         ValueAxis domainAxis,                         ValueAxis rangeAxis,                         XYDataset dataset,                         int series,                         int item,                         CrosshairState crosshairState,                         int pass) {        if (!getItemVisible(series, item)) {            return;           }        // setup for collecting optional entity info...        Shape entityArea = null;        EntityCollection entities = null;        if (info != null) {            entities = info.getOwner().getEntityCollection();        }        PlotOrientation orientation = plot.getOrientation();        Paint paint = getItemPaint(series, item);        Stroke seriesStroke = getItemStroke(series, item);        g2.setPaint(paint);        g2.setStroke(seriesStroke);        // get the data point...        double x1 = dataset.getXValue(series, item);        double y1 = dataset.getYValue(series, item);        if (Double.isNaN(x1) || Double.isNaN(y1)) {            return;        }        RectangleEdge xAxisLocation = plot.getDomainAxisEdge();        RectangleEdge yAxisLocation = plot.getRangeAxisEdge();        double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);        double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);        if (getPlotLines()) {            if (item == 0) {                if (this.drawSeriesLineAsPath) {                    State s = (State) state;                            s.seriesPath.reset();                    s.lastPointGood = false;                     }            }                       if (this.drawSeriesLineAsPath) {                State s = (State) state;                // update path to reflect latest point                if (!Double.isNaN(transX1) && !Double.isNaN(transY1)) {                    float x = (float) transX1;                    float y = (float) transY1;                    if (orientation == PlotOrientation.HORIZONTAL) {                        x = (float) transY1;                        y = (float) transX1;                    }                    if (s.isLastPointGood()) {                        // TODO: check threshold                        s.seriesPath.lineTo(x, y);                    }                    else {                        s.seriesPath.moveTo(x, y);                    }                    s.setLastPointGood(true);                }                else {                    s.setLastPointGood(false);                }                if (item == dataset.getItemCount(series) - 1) {                    // draw path                    g2.setStroke(getSeriesStroke(series));                    g2.setPaint(getSeriesPaint(series));                    g2.draw(s.seriesPath);                }            }            else if (item != 0) {                // get the previous data point...                double x0 = dataset.getXValue(series, item - 1);                double y0 = dataset.getYValue(series, item - 1);                if (!Double.isNaN(x0) && !Double.isNaN(y0)) {                    boolean drawLine = true;                    if (getPlotDiscontinuous()) {                        // only draw a line if the gap between the current and                         // previous data point is within the threshold                        int numX = dataset.getItemCount(series);                        double minX = dataset.getXValue(series, 0);                        double maxX = dataset.getXValue(series, numX - 1);                        if (this.gapThresholdType == UnitType.ABSOLUTE) {                            drawLine = Math.abs(x1 - x0) <= this.gapThreshold;                        }                        else {                            drawLine = Math.abs(x1 - x0) <= ((maxX - minX)                                 / numX * getGapThreshold());                        }                    }                    if (drawLine) {                        double transX0 = domainAxis.valueToJava2D(                            x0, dataArea, xAxisLocation                        );                        double transY0 = rangeAxis.valueToJava2D(                            y0, dataArea, yAxisLocation                        );                        // only draw if we have good values                        if (Double.isNaN(transX0) || Double.isNaN(transY0)                             || Double.isNaN(transX1) || Double.isNaN(transY1)) {                            return;                        }                        if (orientation == PlotOrientation.HORIZONTAL) {                            state.workingLine.setLine(                                transY0, transX0, transY1, transX1                            );                        }                        else if (orientation == PlotOrientation.VERTICAL) {                            state.workingLine.setLine(                                transX0, transY0, transX1, transY1                            );                        }                        if (state.workingLine.intersects(dataArea)) {                            g2.draw(state.workingLine);                        }                    }                }            }        }        if (getPlotShapes()) {            Shape shape = getItemShape(series, item);            if (orientation == PlotOrientation.HORIZONTAL) {                shape = ShapeUtilities.createTranslatedShape(                    shape, transY1, transX1                );            }            else if (orientation == PlotOrientation.VERTICAL) {                shape = ShapeUtilities.createTranslatedShape(                    shape, transX1, transY1                );            }            if (shape.intersects(dataArea)) {                if (getItemShapeFilled(series, item)) {                    g2.fill(shape);                }                else {                    g2.draw(shape);                }            }            entityArea = shape;        }        if (getPlotImages()) {            Image image = getImage(plot, series, item, transX1, transY1);            if (image != null) {                Point hotspot = getImageHotspot(                    plot, series, item, transX1, transY1, image                );                g2.drawImage(                    image, (int) (transX1 - hotspot.getX()),                     (int) (transY1 - hotspot.getY()), null                );                entityArea = new Rectangle2D.Double(                    transX1 - hotspot.getX(), transY1 - hotspot.getY(),                    image.getWidth(null), image.getHeight(null)                );            }        }        // draw the item label if there is one...        if (isItemLabelVisible(series, item)) {            double xx = transX1;            double yy = transY1;            if (orientation == PlotOrientation.HORIZONTAL) {                xx = transY1;                yy = transX1;            }                      drawItemLabel(                g2, orientation, dataset, series, item, xx, yy, (y1 < 0.0)            );        }        updateCrosshairValues(            crosshairState, x1, y1, transX1, transY1, orientation        );        // add an entity for the item...        if (entities != null) {            addEntity(                entities, entityArea, dataset, series, item, transX1, transY1            );        }    }    /**     * Tests this renderer for equality with another 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 StandardXYItemRenderer)) {            return false;        }        if (!super.equals(obj)) {            return false;        }        StandardXYItemRenderer that = (StandardXYItemRenderer) obj;        if (this.plotShapes != that.plotShapes) {            return false;        }        if (this.plotLines != that.plotLines) {            return false;        }        if (this.plotImages != that.plotImages) {            return false;        }        if (this.plotDiscontinuous != that.plotDiscontinuous) {            return false;        }        if (this.gapThresholdType != that.gapThresholdType) {            return false;        }        if (this.gapThreshold != that.gapThreshold) {            return false;        }        if (!ObjectUtilities.equal(this.shapesFilled, that.shapesFilled)) {            return false;        }        if (!ShapeUtilities.equal(this.legendLine, that.legendLine)) {            return false;           }        return true;    }    /**     * Returns a clone of the renderer.     *     * @return A clone.     *     * @throws CloneNotSupportedException  if the renderer cannot be cloned.     */    public Object clone() throws CloneNotSupportedException {        return super.clone();    }    ////////////////////////////////////////////////////////////////////////////    // PROTECTED METHODS    // These provide the opportunity to subclass the standard renderer and     // create custom effects.    ////////////////////////////////////////////////////////////////////////////    /**     * Returns the image used to draw a single data item.     *     * @param plot  the plot (can be used to obtain standard color information      *              etc).     * @param series  the series index.     * @param item  the item index.     * @param x  the x value of the item.     * @param y  the y value of the item.     *     * @return The image.     */    protected Image getImage(Plot plot, int series, int item,                              double x, double y) {        // should this be added to the plot as well ?        // return plot.getShape(series, item, x, y, scale);        // or should this be left to the user - like this:        return null;    }    /**     * Returns the hotspot of the image used to draw a single data item.     * The hotspot is the point relative to the top left of the image     * that should indicate the data item. The default is the center of the     * image.     *     * @param plot  the plot (can be used to obtain standard color information      *              etc).     * @param image  the image (can be used to get size information about the      *               image)     * @param series  the series index     * @param item  the item index     * @param x  the x value of the item     * @param y  the y value of the item     *     * @return The hotspot used to draw the data item.     */    protected Point getImageHotspot(Plot plot, int series, int item,                                    double x, double y, Image image) {        int height = image.getHeight(null);        int width = image.getWidth(null);        return new Point(width / 2, height / 2);    }        /**     * 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.legendLine = SerialUtilities.readShape(stream);    }        /**     * 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.writeShape(this.legendLine, stream);    }}

⌨️ 快捷键说明

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