xydifferencerenderer.java

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

JAVA
961
字号
        boolean endsNegative = (y1A >= y1B);        if (orientation == PlotOrientation.HORIZONTAL) {            startsNegative = (y0B >= y0A);            endsNegative = (y1B >= y1A);        }                if (startsNegative) {  // starts negative            if (endsNegative) {                // all negative - return null                result = null;            }            else {                // changed from negative to positive                float[] p = getIntersection(x0, y0A, x1, y1A, x0, y0B, x1, y1B);                GeneralPath area = new GeneralPath();                if (orientation == PlotOrientation.HORIZONTAL) {                    area.moveTo(y1A, x1);                    area.lineTo(p[1], p[0]);                    area.lineTo(y1B, x1);                    area.closePath();                }                else if (orientation == PlotOrientation.VERTICAL) {                    area.moveTo(x1, y1A);                    area.lineTo(p[0], p[1]);                    area.lineTo(x1, y1B);                    area.closePath();                }                result = area;            }        }        else {  // starts positive            if (endsNegative) {                // changed from positive to negative                float[] p = getIntersection(x0, y0A, x1, y1A, x0, y0B, x1, y1B);                GeneralPath area = new GeneralPath();                if (orientation == PlotOrientation.HORIZONTAL) {                    area.moveTo(y0A, x0);                    area.lineTo(p[1], p[0]);                    area.lineTo(y0B, x0);                    area.closePath();                }                else if (orientation == PlotOrientation.VERTICAL) {                    area.moveTo(x0, y0A);                    area.lineTo(p[0], p[1]);                    area.lineTo(x0, y0B);                    area.closePath();                }                result = area;            }            else {                GeneralPath area = new GeneralPath();                if (orientation == PlotOrientation.HORIZONTAL) {                    area.moveTo(y0A, x0);                    area.lineTo(y1A, x1);                    area.lineTo(y1B, x1);                    area.lineTo(y0B, x0);                    area.closePath();                }                else if (orientation == PlotOrientation.VERTICAL) {                    area.moveTo(x0, y0A);                    area.lineTo(x1, y1A);                    area.lineTo(x1, y1B);                    area.lineTo(x0, y0B);                    area.closePath();                }                result = area;            }        }        return result;    }    /**     * Returns the negative area for a cross-over section.     *      * @param x0  x coordinate.     * @param y0A  y coordinate A.     * @param y0B  y coordinate B.     * @param x1  x coordinate.     * @param y1A  y coordinate A.     * @param y1B  y coordinate B.     * @param orientation  the plot orientation.     *      * @return The negative area.     */    protected Shape getNegativeArea(float x0, float y0A, float y0B,                                     float x1, float y1A, float y1B,                                    PlotOrientation orientation) {        Shape result = null;        boolean startsNegative = (y0A >= y0B);        boolean endsNegative = (y1A >= y1B);        if (orientation == PlotOrientation.HORIZONTAL) {            startsNegative = (y0B >= y0A);            endsNegative = (y1B >= y1A);        }        if (startsNegative) {  // starts negative            if (endsNegative) {  // all negative                GeneralPath area = new GeneralPath();                if (orientation == PlotOrientation.HORIZONTAL) {                    area.moveTo(y0A, x0);                    area.lineTo(y1A, x1);                    area.lineTo(y1B, x1);                    area.lineTo(y0B, x0);                    area.closePath();                }                else if (orientation == PlotOrientation.VERTICAL) {                    area.moveTo(x0, y0A);                    area.lineTo(x1, y1A);                    area.lineTo(x1, y1B);                    area.lineTo(x0, y0B);                    area.closePath();                }                result = area;            }            else {  // changed from negative to positive                float[] p = getIntersection(x0, y0A, x1, y1A, x0, y0B, x1, y1B);                GeneralPath area = new GeneralPath();                if (orientation == PlotOrientation.HORIZONTAL) {                    area.moveTo(y0A, x0);                    area.lineTo(p[1], p[0]);                    area.lineTo(y0B, x0);                    area.closePath();                }                else if (orientation == PlotOrientation.VERTICAL) {                    area.moveTo(x0, y0A);                    area.lineTo(p[0], p[1]);                    area.lineTo(x0, y0B);                    area.closePath();                }                result = area;            }        }        else {            if (endsNegative) {                // changed from positive to negative                float[] p = getIntersection(x0, y0A, x1, y1A, x0, y0B, x1, y1B);                GeneralPath area = new GeneralPath();                if (orientation == PlotOrientation.HORIZONTAL) {                    area.moveTo(y1A, x1);                    area.lineTo(p[1], p[0]);                    area.lineTo(y1B, x1);                    area.closePath();                }                else if (orientation == PlotOrientation.VERTICAL) {                    area.moveTo(x1, y1A);                    area.lineTo(p[0], p[1]);                    area.lineTo(x1, y1B);                    area.closePath();                }                result = area;            }            else {                // all negative - return null            }        }        return result;    }    /**     * Returns the intersection point of two lines.     *      * @param x1  x1     * @param y1  y1     * @param x2  x2     * @param y2  y2     * @param x3  x3     * @param y3  y3     * @param x4  x4     * @param y4  y4     *      * @return The intersection point.     */    private float[] getIntersection(float x1, float y1, float x2, float y2,                                    float x3, float y3, float x4, float y4) {        float n = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);        float d = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);        float u = n / d;        float[] result = new float[2];        result[0] = x1 + u * (x2 - x1);        result[1] = y1 + u * (y2 - y1);        return result;    }        /**     * Returns a default legend item for the specified series.  Subclasses      * should override this method to generate customised items.     *     * @param datasetIndex  the dataset index (zero-based).     * @param series  the series index (zero-based).     *     * @return A legend item for the series.     */    public LegendItem getLegendItem(int datasetIndex, int series) {        LegendItem result = null;        XYPlot p = getPlot();        if (p != null) {            XYDataset dataset = p.getDataset(datasetIndex);            if (dataset != null) {                if (getItemVisible(series, 0)) {                    String label = getLegendItemLabelGenerator().generateLabel(                        dataset, series                    );                    String description = label;                    String toolTipText = null;                    if (getLegendItemToolTipGenerator() != null) {                        toolTipText = getLegendItemToolTipGenerator().generateLabel(                            dataset, series                        );                    }                    String urlText = null;                    if (getLegendItemURLGenerator() != null) {                        urlText = getLegendItemURLGenerator().generateLabel(                            dataset, series                        );                    }                    Paint paint = getSeriesPaint(series);                    Stroke stroke = getSeriesStroke(series);                    // TODO:  the following hard-coded line needs generalising                    Line2D line = new Line2D.Double(-7.0, 0.0, 7.0, 0.0);                    result = new LegendItem(                        label, description, toolTipText, urlText,                         line, stroke, paint                    );                }            }        }        return result;    }    /**     * Tests this renderer for equality with an arbitrary 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 XYDifferenceRenderer)) {            return false;           }        if (!super.equals(obj)) {            return false;           }        XYDifferenceRenderer that = (XYDifferenceRenderer) obj;        if (!ObjectUtilities.equal(this.positivePaint, that.positivePaint)) {            return false;           }        if (!ObjectUtilities.equal(this.negativePaint, that.negativePaint)) {            return false;           }        if (this.shapesVisible != that.shapesVisible) {            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();    }    /**     * 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.writePaint(this.positivePaint, stream);        SerialUtilities.writePaint(this.negativePaint, stream);        SerialUtilities.writeShape(this.legendLine, stream);    }    /**     * 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.positivePaint = SerialUtilities.readPaint(stream);        this.negativePaint = SerialUtilities.readPaint(stream);        this.legendLine = SerialUtilities.readShape(stream);    }}

⌨️ 快捷键说明

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