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

📄 boxandwhiskerrenderer.java

📁 大家打开看看啊, 很有用的东西
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if ((yyMin < yyQ1Median) && (yyMin < yyQ3Median)) {
            g2.draw(new Line2D.Double(yyMin, xx, Math.min(yyQ1Median, yyQ3Median), xx));
        }


        // draw the body
        Shape body = null;
        if (yyQ1Median < yyQ3Median) {
            body = new Rectangle2D.Double(yyQ1Median, xx - thisCandleWidth / 2,
                                          yyQ3Median - yyQ1Median, thisCandleWidth);
        }
        else {
            body = new Rectangle2D.Double(yyQ3Median, xx - thisCandleWidth / 2,
                                          yyQ1Median - yyQ3Median, thisCandleWidth);
            if (paint != null) {
                g2.setPaint(paint);
                g2.fill(body);
            }
            g2.draw(body);
        }

        // add an entity for the item...
        if (entities != null) {
            String tip = null;
            if (getToolTipGenerator() != null) {
                tip = getToolTipGenerator().generateToolTip(dataset, series, item);
            }
            String url = null;
            if (getURLGenerator() != null) {
                url = getURLGenerator().generateURL(dataset, series, item);
            }
            XYItemEntity entity = new XYItemEntity(body, dataset, series, item, tip, url);
            entities.addEntity(entity);
        }

    }

    /**
     * Draws the visual representation of a single data item.
     *
     * @param g2  the graphics device.
     * @param dataArea  the area within which the plot is being drawn.
     * @param info  collects info about the drawing.
     * @param plot  the plot (can be used to obtain standard color information etc).
     * @param domainAxis  the domain axis.
     * @param rangeAxis  the range axis.
     * @param dataset  the dataset.
     * @param series  the series index (zero-based).
     * @param item  the item index (zero-based).
     * @param crosshairInfo  information about crosshairs on a plot.
     * @param pass  the pass index.
     */
    public void drawVerticalItem(Graphics2D g2, Rectangle2D dataArea,
                                 ChartRenderingInfo info,
                                 XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis,
                                 XYDataset dataset, int series, int item,
                                 CrosshairInfo crosshairInfo,
                                 int pass) {   

        // setup for collecting optional entity info...
        EntityCollection entities = null;
        if (info != null) {
            entities = info.getEntityCollection();
        }

        BoxAndWhiskerDataset boxAndWhiskerData = (BoxAndWhiskerDataset) dataset;
        //System.err.print("\n\n" +series + "|" + item + " : " + boxAndWhiskerData.makeString(series, item));
        Collection outliers = new ArrayList();

        Number x = boxAndWhiskerData.getXValue(series, item);
        Number yMax = boxAndWhiskerData.getMaxValue(series, item);
        Number yMin = boxAndWhiskerData.getMinValue(series, item);
        Number yMedian = boxAndWhiskerData.getMedianValue(series, item);
        Number yAverage = boxAndWhiskerData.getAverageValue(series, item);
        Number yQ1Median = boxAndWhiskerData.getQ1MedianValue(series, item);
        Number yQ3Median = boxAndWhiskerData.getQ3MedianValue(series, item);
        Number[] yOutliers = boxAndWhiskerData.getOutliersArray(series, item);

        double xx = domainAxis.translateValueToJava2D(x.doubleValue(), dataArea,
                                                      plot.getDomainAxisEdge());

        RectangleEdge location = plot.getRangeAxisEdge();
        double yyMax = rangeAxis.translateValueToJava2D(yMax.doubleValue(), dataArea, location);
        double yyMin = rangeAxis.translateValueToJava2D(yMin.doubleValue(), dataArea, location);
        double yyMedian = rangeAxis.translateValueToJava2D(yMedian.doubleValue(), dataArea, location);
        double yyAverage = rangeAxis.translateValueToJava2D(yAverage.doubleValue(), dataArea, location);
        double yyQ1Median = rangeAxis.translateValueToJava2D(yQ1Median.doubleValue(), dataArea, location);    // should be low range
        double yyQ3Median = rangeAxis.translateValueToJava2D(yQ3Median.doubleValue(), dataArea, location);    // should be high range
        double[] yyOutliers = new double[yOutliers.length];

        // populate yyOutliers
        for (int i = 0; i < yyOutliers.length; i++) {
            yyOutliers[i] = rangeAxis.translateValueToJava2D(yOutliers[i].doubleValue(), dataArea, location);
        }

        double exactCandleWidth = boxWidth;
        double thisCandleWidth = boxWidth;
        if (boxWidth <= 0.0) {
            int itemCount = boxAndWhiskerData.getItemCount(series);
            exactCandleWidth = (dataArea.getMaxX() - dataArea.getMinX()) / itemCount * 4.5 / 7;
            if (exactCandleWidth < 1) {
                exactCandleWidth = 1;
            }
            thisCandleWidth = exactCandleWidth;
            if (thisCandleWidth < 3) {
                thisCandleWidth = 3;
            }
        }

        Paint p = this.getPaint();
        if (p != null) {
            g2.setPaint(p);
        }
        Stroke s = getItemStroke(series, item);

        g2.setStroke(s);

        // draw the upper shadow
        if ((yyMax < yyQ1Median) && (yyMax < yyQ3Median)) {
            g2.draw(new Line2D.Double(xx, yyMax, xx,
                Math.min(yyQ1Median, yyQ3Median)));
            g2.draw(new Line2D.Double(xx - thisCandleWidth / 2,
                    yyMax, xx + thisCandleWidth / 2, yyMax));
        }

        // draw the lower shadow
        if ((yyMin > yyQ1Median) && (yyMin > yyQ3Median)) {
            g2.draw(new Line2D.Double(xx, yyMin, xx,
                Math.max(yyQ1Median, yyQ3Median)));
            g2.draw(new Line2D.Double(xx - thisCandleWidth / 2,
                    yyMin, xx + thisCandleWidth / 2, yyMin));
        }


        // draw the body
        Shape body = null;
        if (yyQ1Median > yyQ3Median) {
            body = new Rectangle2D.Double(xx - thisCandleWidth / 2, yyQ3Median,
                                          thisCandleWidth, yyQ1Median - yyQ3Median);
        }
        else {
            body = new Rectangle2D.Double(xx - thisCandleWidth / 2, yyQ1Median,
                                          thisCandleWidth, yyQ3Median - yyQ1Median);
            g2.fill(body);
            g2.draw(body);
        }

        // draw median
        g2.setPaint(Color.black);
        g2.draw(new Line2D.Double(xx - thisCandleWidth / 2, yyMedian, xx + thisCandleWidth / 2, yyMedian));

        // draw average - SPECIAL AIMS REQUIREMENT
        double aRadius = thisCandleWidth/4;
        Ellipse2D.Double avgEllipse = new Ellipse2D.Double(xx - aRadius, yyAverage - aRadius, aRadius * 2, aRadius * 2);
        g2.fill(avgEllipse);
        g2.draw(avgEllipse);

        // draw yOutliers
        double oRadius = thisCandleWidth/3;
        double m = rangeAxis.translateValueToJava2D(rangeAxis.getMaximumAxisValue(), dataArea, location) + aRadius;
        OutlierListCollection collection = new OutlierListCollection();

        for (int i = 0; i < yyOutliers.length; i++) {
            if (yyOutliers[i] > (yyMedian + ((yyQ1Median - yyQ3Median) * 1.2))) {    // hard-coded value is a SPECIFIC AIMS REQUIEMENT
                outliers.add(new Outlier(xx, yyOutliers[i], oRadius));
            } else {    // draw extreme outliers symbol
                collection.setFarOut(true);
            }
            Collections.sort((ArrayList)outliers);
        }

        // process outliers
        for (Iterator iterator = outliers.iterator(); iterator.hasNext();) {
            Outlier outlier = (Outlier) iterator.next();
            //System.err.print("\noutlier = " + outlier.toString());
            collection.add(outlier);
        }

        for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
            OutlierList list = (OutlierList) iterator.next();
            Outlier outlier = list.getAveragedOutlier();
            Point2D point = outlier.getPoint();

            if (list.isMultiple()) {
                drawMultipleEllipse(point, thisCandleWidth, oRadius, g2);
            } else {
                drawEllipse(point, oRadius, g2);
            }
        }

        if (collection.isFarOut()) {
            drawFarOut(aRadius, g2, xx, m);
        }


        g2.setPaint(p);


        // add an entity for the item...
        if (entities != null) {
            String tip = null;
            if (getToolTipGenerator() != null) {
                tip = getToolTipGenerator().generateToolTip(dataset, series, item);
            }
            String url = null;
            if (getURLGenerator() != null) {
                url = getURLGenerator().generateURL(dataset, series, item);
            }
            XYItemEntity entity = new XYItemEntity(body, dataset, series, item, tip, url);
            entities.addEntity(entity);
        }

    }

    private void drawEllipse(Point2D point, double oRadius, Graphics2D g2) {
        Ellipse2D.Double dot = new Ellipse2D.Double(point.getX() + oRadius/2, point.getY(), oRadius, oRadius);
        g2.draw(dot);
    }

    private void drawMultipleEllipse(Point2D point, double thisCandleWidth, double oRadius, Graphics2D g2) {
        Ellipse2D.Double dot1 = new Ellipse2D.Double(point.getX() - (thisCandleWidth/2) + oRadius, point.getY(), oRadius, oRadius);
        Ellipse2D.Double dot2 = new Ellipse2D.Double(point.getX() + (thisCandleWidth/2), point.getY(), oRadius, oRadius);
        g2.draw(dot1);
        g2.draw(dot2);
    }

    private void drawFarOut(double aRadius, Graphics2D g2, double xx, double m) {
        double side = aRadius * 2;
        g2.draw(new Line2D.Double(xx - side, m + side, xx + side, m + side));
        g2.draw(new Line2D.Double(xx - side, m + side, xx, m));
        g2.draw(new Line2D.Double(xx + side, m + side, xx, m));
    }

    /**
     * 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 BoxAndWhiskerRenderer) {
            BoxAndWhiskerRenderer renderer = (BoxAndWhiskerRenderer) obj;
            boolean result = super.equals(obj);
            result = result && (this.boxWidth == renderer.getBoxWidth());
            result = result && (this.paint.equals(renderer.getPaint()));
            return result;
        }

        return false;

    }

    /**
     * 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.paint, 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.paint = SerialUtilities.readPaint(stream);

    }


}

⌨️ 快捷键说明

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