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

📄 boxandwhiskerrenderer.java

📁 JFreeChartweb图表
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @param domainAxis  the domain axis.
     * @param rangeAxis  the range axis.
     * @param dataset  the dataset.
     * @param row  the row index (zero-based).
     * @param column  the column index (zero-based).
     */
    public void drawVerticalItem(Graphics2D g2, 
                                 Rectangle2D dataArea,
                                 CategoryPlot plot, 
                                 CategoryAxis domainAxis, 
                                 ValueAxis rangeAxis,
                                 CategoryDataset dataset, 
                                 int row, 
                                 int column) {

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

        BoxAndWhiskerCategoryDataset bawDataset = (DefaultBoxAndWhiskerCategoryDataset) dataset;
        
        Number yMean = bawDataset.getMeanValue(row, column);
        Number yMedian = bawDataset.getMedianValue(row, column);
        Number yQ1 = bawDataset.getQ1Value(row, column);
        Number yQ3 = bawDataset.getQ3Value(row, column);
        Number yMax = bawDataset.getMaxRegularValue(row, column);
        Number yMin = bawDataset.getMinRegularValue(row, column);
        List yOutliers = bawDataset.getOutliers(row, column);

        double xx = domainAxis.getCategoryStart(
            column, getColumnCount(), dataArea, plot.getDomainAxisEdge());
        int seriesCount = getRowCount();
        int categoryCount = getColumnCount();
        if (seriesCount > 1) {
            double seriesGap = dataArea.getWidth() * getItemMargin()
                               / (categoryCount * (seriesCount - 1));
            xx = xx + row * (getBoxWidth() + seriesGap);
        }
        else {
            xx = xx + row * getBoxWidth();
        }

        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 = 0.0;
        if (yMean != null) {
            yyAverage = rangeAxis.translateValueToJava2D(yMean.doubleValue(), dataArea, location);
        }
        double yyQ1 = rangeAxis.translateValueToJava2D(yQ1.doubleValue(), dataArea, location);
        double yyQ3 = rangeAxis.translateValueToJava2D(yQ3.doubleValue(), dataArea, location);
        double yyOutlier;

        Paint p = this.getItemPaint(row, column);
        if (p != null) {
            g2.setPaint(p);
        }
        Stroke s = getItemStroke(row, column);
        g2.setStroke(s);

        double aRadius = 0;                 // average radius

        // draw the upper shadow...
        if ((yyMax < yyQ1) && (yyMax < yyQ3)) {          // 0,0 = top left...
            g2.draw(new Line2D.Double(xx + this.boxWidth / 2, yyMax, 
                                      xx + this.boxWidth / 2, Math.max(yyQ1, yyQ3)));
            g2.draw(new Line2D.Double(xx, yyMax, 
                                      xx + this.boxWidth, yyMax));
        }

        // draw the lower shadow...
        if ((yyMin > yyQ1) && (yyMin > yyQ3)) {
            g2.draw(new Line2D.Double(xx + this.boxWidth / 2, yyMin, 
                                      xx + this.boxWidth / 2, Math.min(yyQ1, yyQ3)));
            g2.draw(new Line2D.Double(xx, yyMin, 
                                      xx + this.boxWidth, yyMin));
        }

        // draw the body...
        Shape body = new Rectangle2D.Double(xx, Math.min(yyQ1, yyQ3), 
                                            this.boxWidth, Math.abs(yyQ1 - yyQ3));
        g2.fill(body);
        g2.draw(body);

        // draw mean - SPECIAL AIMS REQUIREMENT...
        g2.setPaint(artifactPaint);
        if (yMean != null) {
            aRadius = this.boxWidth / 4;
            Ellipse2D.Double avgEllipse = new Ellipse2D.Double(xx + aRadius, yyAverage - aRadius, 
                                                               aRadius * 2, aRadius * 2);
            g2.fill(avgEllipse);
            g2.draw(avgEllipse);
        }

        // draw median...
        g2.draw(new Line2D.Double(xx, yyMedian, xx + this.boxWidth, yyMedian));

        // draw yOutliers...
        double maxAxisValue = rangeAxis.translateValueToJava2D(rangeAxis.getUpperBound(), 
                                                               dataArea, location) + aRadius;
        double minAxisValue = rangeAxis.translateValueToJava2D(rangeAxis.getLowerBound(), 
                                                               dataArea, location) - aRadius;

        g2.setPaint(p);

        // draw outliers
        double oRadius = this.boxWidth / 3;    // outlier radius
        List outliers = new ArrayList();
        OutlierListCollection outlierListCollection = new OutlierListCollection();

        // From outlier array sort out which are outliers and put these into a list
        // If there are any farouts, set the flag on the OutlierListCollection
        for (int i = 0; i < yOutliers.size(); i++) {
            double outlier = ((Number) yOutliers.get(i)).doubleValue();
            if (outlier > bawDataset.getMaxOutlier(row, column).doubleValue()) {
                outlierListCollection.setHighFarOut(true);
            } 
            else if (outlier < bawDataset.getMinOutlier(row, column).doubleValue()) {
                outlierListCollection.setLowFarOut(true);
            }
            else if (outlier > bawDataset.getMaxRegularValue(row, column).doubleValue()) {
                yyOutlier = rangeAxis.translateValueToJava2D(outlier, dataArea, location);
                outliers.add(new Outlier(xx + this.boxWidth / 2.0, yyOutlier, oRadius));
            }
            else if (outlier < bawDataset.getMinRegularValue(row, column).doubleValue()) {
                yyOutlier = rangeAxis.translateValueToJava2D(outlier, dataArea, location);
                outliers.add(new Outlier(xx + this.boxWidth / 2.0, yyOutlier, oRadius));
            }
            Collections.sort((ArrayList) outliers);
        }

        // Process outliers. Each outlier is either added to the appropriate outlier list
        // or a new outlier list is made
        for (Iterator iterator = outliers.iterator(); iterator.hasNext();) {
            Outlier outlier = (Outlier) iterator.next();
            outlierListCollection.add(outlier);
        }

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

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

        // draw farout indicators
        if (outlierListCollection.isHighFarOut()) {
            drawHighFarOut(aRadius / 2.0, g2, xx + this.boxWidth / 2.0, maxAxisValue);
        }
        
        if (outlierListCollection.isLowFarOut()) {
            drawLowFarOut(aRadius / 2.0, g2, xx + this.boxWidth / 2.0, minAxisValue);
        }

        // 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 a dot to represent an outlier. 
     * 
     * @param point  the location.
     * @param oRadius  the radius.
     * @param g2  the graphics device.
     */
    private void drawEllipse(Point2D point, double oRadius, Graphics2D g2) {
        Ellipse2D dot = new Ellipse2D.Double(point.getX() + oRadius / 2, point.getY(),
                                             oRadius, oRadius);
        g2.draw(dot);
    }

    /**
     * Draws two dots to represent the average value of more than one outlier.
     * 
     * @param point  the location
     * @param boxWidth  the box width.
     * @param oRadius  the radius.
     * @param g2  the graphics device.
     */
    private void drawMultipleEllipse(Point2D point, double boxWidth, double oRadius, 
                                     Graphics2D g2)  {
                                         
        Ellipse2D dot1 = new Ellipse2D.Double(point.getX() - (boxWidth / 2) + oRadius, point.getY(),
                                              oRadius, oRadius);
        Ellipse2D dot2 = new Ellipse2D.Double(point.getX() + (boxWidth / 2), point.getY(),
                                              oRadius, oRadius);
        g2.draw(dot1);
        g2.draw(dot2);
    }

    /**
     * Draws a triangle to indicate the presence of far-out values.
     * 
     * @param aRadius  the radius.
     * @param g2  the graphics device.
     * @param xx  the x coordinate.
     * @param m  the y coordinate.
     */
    private void drawHighFarOut(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));
    }

    /**
     * Draws a triangle to indicate the presence of far-out values.
     * 
     * @param aRadius  the radius.
     * @param g2  the graphics device.
     * @param xx  the x coordinate.
     * @param m  the y coordinate.
     */
    private void drawLowFarOut(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));
    }
}

⌨️ 快捷键说明

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