Java 如何在jfreechart中删除框须图中的异常值(小圆)

Java 如何在jfreechart中删除框须图中的异常值(小圆),java,jfreechart,Java,Jfreechart,我使用下面的代码生成方框图。但取决于某些值,它显示的是一个小圆圈,没有画出胡须。我需要给你看胡须,而不是小圆圈。是否可以隐藏圆圈并显示胡须 private void createBoxPlotChart(OutputStream out, Object data) throws Exception { ChartRenderingInfo info = new ChartRenderingInfo(); ByteArrayOutputSt

我使用下面的代码生成方框图。但取决于某些值,它显示的是一个小圆圈,没有画出胡须。我需要给你看胡须,而不是小圆圈。是否可以隐藏圆圈并显示胡须

private void createBoxPlotChart(OutputStream out, Object data)
            throws Exception {

        ChartRenderingInfo info = new ChartRenderingInfo();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] ptsImageBytes = null;

        DefaultBoxAndWhiskerCategoryDataset boxAndWhiskerCategoryDS = createBoxAndWhiskerCategoryDataSet();

        JFreeChart chart = ChartFactory.createBoxAndWhiskerChart(selectedFlight
                + " - Time on Hold", "Arrival Time (Days)",
                "Hold Time (Mins)", boxAndWhiskerCategoryDS, true); 


        NumberAxis rangeAxis = (NumberAxis) chart.getCategoryPlot()
                .getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());        
        rangeAxis.setAutoRangeIncludesZero(true);


        CategoryAxis domainAxis = chart.getCategoryPlot().getDomainAxis();

        BoxAndWhiskerRenderer renderer = (BoxAndWhiskerRenderer) chart.getCategoryPlot().getRenderer();
        //renderer.setFillBox(false);        
        renderer.setMeanVisible(false);
        renderer.setUseOutlinePaintForWhiskers(false);  
        renderer.setMedianVisible(false);             


        if(boxAndWhiskerCategoryDS.getRowCount()>8){
            domainAxis.setCategoryLabelPositions(CategoryLabelPositions
                    .createUpRotationLabelPositions(Math.PI / 5.0));
        }       

        chart.getCategoryPlot().setBackgroundPaint(Color.white);
        chart.getCategoryPlot().setDomainGridlinePaint(Color.gray);
        chart.getCategoryPlot().setRangeGridlinePaint(Color.gray);
        chart.getCategoryPlot().setOutlineVisible(false);

        BufferedImage pageImage = chart.createBufferedImage(CHART_WIDTH,
                CHART_HEIGHT, BufferedImage.TYPE_INT_BGR, info);
        ImageIO.write(pageImage, IMAGE_TYPE, baos);
        baos.flush();
        if (null != baos) {
            ptsImageBytes = baos.toByteArray();
        }

        ImageIO.write(pageImage, IMAGE_TYPE, out);

    }

    private DefaultBoxAndWhiskerCategoryDataset createBoxAndWhiskerCategoryDataSet()
            throws ParseException {     


        DefaultBoxAndWhiskerCategoryDataset dataset = new DefaultBoxAndWhiskerCategoryDataset();

        for (String graph : graphList) {            

            final List<Double> list = new ArrayList<Double>();
            for (ereportDto ereportDto : holdTimeRptList) {
                if (ereportDto != null && ereportDto.getHoldTime() != 0
                        && ereportDto.getGraphTime() != null
                        && ereportDto.getGraphTime().equalsIgnoreCase(graph)) {                 

                    double medianVal=ereportDto.getGraphHoldEntry()+((ereportDto.getGraphHoldExit()-ereportDto.getGraphHoldEntry())/2);

                    list.add(new Double(ereportDto.getGraphTimeAt120NM()));                             
                    list.add(new Double(ereportDto.getGraphHoldEntry()));
                    list.add(new Double(medianVal));                    
                    list.add(new Double(ereportDto.getGraphHoldExit()));                    
                    list.add(new Double(ereportDto.getGraphLandedTime()));              

                }
            }
            dataset.add(list, graph, graph);
        }



        return dataset;

    }
private void createBoxPlotChart(输出流输出,对象数据)
抛出异常{
ChartRenderingInfo=新建ChartRenderingInfo();
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
字节[]ptsImageBytes=null;
DefaultBoxAndWhiskerCategoryDataset boxAndWhiskerCategoryDS=createBoxAndWhiskerCategoryDataSet();
JFreeChart chart=ChartFactory.CreateBox和WhiskChart(selectedFlight
+“-等待时间”、“到达时间(天)”,
“保持时间(分钟)”,BoxandWhiskerCategorids,true);
NumberAxis rangeAxis=(NumberAxis)chart.getCategoryPlot()
.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(真);
CategoryAxis domainAxis=chart.getCategoryPlot().getDomainAxis();
BoxAndWhiskerRenderer渲染器=(BoxAndWhiskerRenderer)chart.getCategoryPlot().getRenderer();
//.setFillBox(false);
渲染器.setMeanVisible(false);
渲染器.setUseOutlineFor胡须(false);
renderer.setMedianVisible(false);
if(boxAndWhiskerCategoryDS.getRowCount()>8){
domainAxis.setCategoryLabelPositions(CategoryLabelPositions
.createUpRotationLabelPositions(Math.PI/5.0));
}       
chart.getCategoryPlot().setBackgroundPaint(颜色:白色);
chart.getCategoryPlot().setDomainGridlinePaint(Color.gray);
chart.getCategoryPlot().setRangeGridlinePaint(Color.gray);
chart.getCategoryPlot().setOutlineVisible(false);
BuffereImage pageImage=图表。CreateBuffereImage(图表宽度,
图表高度、缓冲区图像类型、内部背景、信息);
ImageIO.write(页面图像、图像类型、BAS);
paos.flush();
如果(null!=BAS){
ptsImageBytes=baos.toByteArray();
}
ImageIO.write(页面图像、图像类型、输出);
}
私有DefaultBoxAndWhiskerCategoryDataset createBoxAndWhiskerCategoryDataSet()的
抛出异常{
DefaultBoxAndWhiskerCategoryDataset=新的DefaultBoxAndWhiskerCategoryDataset();
对于(字符串图:graphList){
最终列表=新的ArrayList();
对于(ereportDto ereportDto:holdTimeRptList){
如果(ereportDto!=null&&ereportDto.getHoldTime()!=0
&&ereportDto.getGraphTime()!=null
&&ereportDto.getGraphTime().equalsIgnoreCase(图形)){
double medianVal=ereportDto.getGraphHoldEntry()+((ereportDto.getGraphHoldExit()-ereportDto.getGraphHoldEntry())/2);
添加(新的Double(ereportDto.getGraphTimeAt120NM());
添加(新的Double(ereportDto.getGraphHoldEntry());
添加(新的Double(medianVal));
添加(新的Double(ereportDto.getGraphHoldExit());
add(新的Double(ereportDto.getGraphLandedTime());
}
}
添加(列表、图形、图形);
}
返回数据集;
}

制作了一个自定义扩展,可以设置异常值的半径。将其设置为0,它们将消失

package de.vontrostorff.kivsproject.util;

import lombok.Getter;
import lombok.Setter;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.Outlier;
import org.jfree.chart.renderer.OutlierList;
import org.jfree.chart.renderer.OutlierListCollection;
import org.jfree.chart.renderer.category.BoxAndWhiskerRenderer;
import org.jfree.chart.renderer.category.CategoryItemRendererState;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.statistics.BoxAndWhiskerCategoryDataset;

import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class CustomBoxAndWhiskerRenderer extends BoxAndWhiskerRenderer {

    private Double outlierRadius;

    public Double getOutlierRadius() {
        return outlierRadius;
    }

    public void setOutlierRadius(Double outlierRadius) {
        this.outlierRadius = outlierRadius;
    }

    @Override
    public void drawVerticalItem(Graphics2D g2, CategoryItemRendererState state,
                                 Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis,
                                 ValueAxis rangeAxis, CategoryDataset dataset, int row, int column) {

        BoxAndWhiskerCategoryDataset bawDataset
                = (BoxAndWhiskerCategoryDataset) dataset;

        double categoryEnd = domainAxis.getCategoryEnd(column,
                getColumnCount(), dataArea, plot.getDomainAxisEdge());
        double categoryStart = domainAxis.getCategoryStart(column,
                getColumnCount(), dataArea, plot.getDomainAxisEdge());
        double categoryWidth = categoryEnd - categoryStart;

        double xx = categoryStart;
        int seriesCount = getRowCount();
        int categoryCount = getColumnCount();

        if (seriesCount > 1) {
            double seriesGap = dataArea.getWidth() * getItemMargin()
                    / (categoryCount * (seriesCount - 1));
            double usedWidth = (state.getBarWidth() * seriesCount)
                    + (seriesGap * (seriesCount - 1));
            // offset the start of the boxes if the total width used is smaller
            // than the category width
            double offset = (categoryWidth - usedWidth) / 2;
            xx = xx + offset + (row * (state.getBarWidth() + seriesGap));
        }
        else {
            // offset the start of the box if the box width is smaller than the
            // category width
            double offset = (categoryWidth - state.getBarWidth()) / 2;
            xx = xx + offset;
        }

        double yyAverage;
        double yyOutlier;

        Paint itemPaint = getItemPaint(row, column);
        g2.setPaint(itemPaint);
        Stroke s = getItemStroke(row, column);
        g2.setStroke(s);

        double aRadius = 0;                 // average radius

        RectangleEdge location = plot.getRangeAxisEdge();

        Number yQ1 = bawDataset.getQ1Value(row, column);
        Number yQ3 = bawDataset.getQ3Value(row, column);
        Number yMax = bawDataset.getMaxRegularValue(row, column);
        Number yMin = bawDataset.getMinRegularValue(row, column);
        Shape box = null;
        if (yQ1 != null && yQ3 != null && yMax != null && yMin != null) {

            double yyQ1 = rangeAxis.valueToJava2D(yQ1.doubleValue(), dataArea,
                    location);
            double yyQ3 = rangeAxis.valueToJava2D(yQ3.doubleValue(), dataArea,
                    location);
            double yyMax = rangeAxis.valueToJava2D(yMax.doubleValue(),
                    dataArea, location);
            double yyMin = rangeAxis.valueToJava2D(yMin.doubleValue(),
                    dataArea, location);
            double xxmid = xx + state.getBarWidth() / 2.0;
            double halfW = (state.getBarWidth() / 2.0) * getWhiskerWidth();

            // draw the body...
            box = new Rectangle2D.Double(xx, Math.min(yyQ1, yyQ3),
                    state.getBarWidth(), Math.abs(yyQ1 - yyQ3));
            if (getFillBox()) {
                g2.fill(box);
            }

            Paint outlinePaint = getItemOutlinePaint(row, column);
            if (getUseOutlinePaintForWhiskers()) {
                g2.setPaint(outlinePaint);
            }
            // draw the upper shadow...
            g2.draw(new Line2D.Double(xxmid, yyMax, xxmid, yyQ3));
            g2.draw(new Line2D.Double(xxmid - halfW, yyMax, xxmid + halfW, yyMax));

            // draw the lower shadow...
            g2.draw(new Line2D.Double(xxmid, yyMin, xxmid, yyQ1));
            g2.draw(new Line2D.Double(xxmid - halfW, yyMin, xxmid + halfW, yyMin));

            g2.setStroke(getItemOutlineStroke(row, column));
            g2.setPaint(outlinePaint);
            g2.draw(box);
        }

        g2.setPaint(getArtifactPaint());

        // draw mean - SPECIAL AIMS REQUIREMENT...
        if (isMeanVisible()) {
            Number yMean = bawDataset.getMeanValue(row, column);
            if (yMean != null) {
                yyAverage = rangeAxis.valueToJava2D(yMean.doubleValue(),
                        dataArea, location);
                aRadius = state.getBarWidth() / 4;
                // here we check that the average marker will in fact be
                // visible before drawing it...
                if ((yyAverage > (dataArea.getMinY() - aRadius))
                        && (yyAverage < (dataArea.getMaxY() + aRadius))) {
                    Ellipse2D.Double avgEllipse = new Ellipse2D.Double(
                            xx + aRadius, yyAverage - aRadius, aRadius * 2,
                            aRadius * 2);
                    g2.fill(avgEllipse);
                    g2.draw(avgEllipse);
                }
            }
        }

        // draw median...
        if (isMedianVisible()) {
            Number yMedian = bawDataset.getMedianValue(row, column);
            if (yMedian != null) {
                double yyMedian = rangeAxis.valueToJava2D(
                        yMedian.doubleValue(), dataArea, location);
                g2.draw(new Line2D.Double(xx, yyMedian,
                        xx + state.getBarWidth(), yyMedian));
            }
        }

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

        g2.setPaint(itemPaint);

        // draw outliers
        double oRadius = outlierRadius == null? state.getBarWidth()/3: outlierRadius;    // outlier radius
        java.util.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
        List yOutliers = bawDataset.getOutliers(row, column);
        if (yOutliers != null) {
            for (int i = 0; i < yOutliers.size(); i++) {
                double outlier = ((Number) yOutliers.get(i)).doubleValue();
                Number minOutlier = bawDataset.getMinOutlier(row, column);
                Number maxOutlier = bawDataset.getMaxOutlier(row, column);
                Number minRegular = bawDataset.getMinRegularValue(row, column);
                Number maxRegular = bawDataset.getMaxRegularValue(row, column);
                if (outlier > maxOutlier.doubleValue()) {
                    outlierListCollection.setHighFarOut(true);
                }
                else if (outlier < minOutlier.doubleValue()) {
                    outlierListCollection.setLowFarOut(true);
                }
                else if (outlier > maxRegular.doubleValue()) {
                    yyOutlier = rangeAxis.valueToJava2D(outlier, dataArea,
                            location);
                    outliers.add(new Outlier(xx + state.getBarWidth() / 2.0,
                            yyOutlier, oRadius));
                }
                else if (outlier < minRegular.doubleValue()) {
                    yyOutlier = rangeAxis.valueToJava2D(outlier, dataArea,
                            location);
                    outliers.add(new Outlier(xx + state.getBarWidth() / 2.0,
                            yyOutlier, oRadius));
                }
                Collections.sort(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, state.getBarWidth(), oRadius,
                            g2);
                }
                else {
                    drawEllipse(point, oRadius, g2);
                }
            }

            // draw farout indicators
            if (outlierListCollection.isHighFarOut()) {
                drawHighFarOut(aRadius / 2.0, g2,
                        xx + state.getBarWidth() / 2.0, maxAxisValue);
            }

            if (outlierListCollection.isLowFarOut()) {
                drawLowFarOut(aRadius / 2.0, g2,
                        xx + state.getBarWidth() / 2.0, minAxisValue);
            }
        }
        // collect entity and tool tip information...
        if (state.getInfo() != null && box != null) {
            EntityCollection entities = state.getEntityCollection();
            if (entities != null) {
                addItemEntity(entities, dataset, row, column, box);
            }
        }

    }


    /**
     * 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 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 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));
    }

}
package de.vontrostorff.kivsproject.util;
进口龙目吸气剂;
进口龙目织机;
导入org.jfree.chart.axis.CategoryAxis;
导入org.jfree.chart.axis.ValueAxis;
导入org.jfree.chart.entity.EntityCollection;
导入org.jfree.chart.plot.CategoryPlot;
导入org.jfree.chart.renderer.Outlier;
导入org.jfree.chart.renderer.OutlierList;
导入org.jfree.chart.renderer.OutlierListCollection;
导入org.jfree.chart.renderer.category.BoxAndWhiskerRenderer;
导入org.jfree.chart.renderer.category.CategoryItemRenderState;
导入org.jfree.chart.ui.RectangleEdge;
导入org.jfree.data.category.CategoryDataset;
导入org.jfree.data.statistics.boxAndWhiskerCategory数据集;
导入java.awt.*;
导入java.awt.geom.Ellipse2D;
导入java.awt.geom.Line2D;
导入java.awt.geom.Point2D;
导入java.awt.geom.Rectangle2D;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Iterator;
导入java.util.List;
公共类CustomBoxAndWhiskerRender扩展了BoxAndWhiskerRender{
私有双离群半径;
公共双getOutlierRadius(){
返回半径;
}
公共无效集outlierRadius(双outlierRadius){
此参数为:outlierRadius=outlierRadius;
}
@凌驾
公共void drawVerticalItem(图形2D g2,类别项目渲染器状态,
矩形2D数据区,类别地块图,类别轴域轴,
ValueAxis rangeAxis,CategoryDataset数据集,int行,int列){
BoxandWhiskerCategory数据集bawDataset
=(BoxAndWhiskerCategory数据集)数据集;
double categoryEnd=domainAxis.getCategoryEnd(列,
getColumnCount(),dataArea,plot.getDomainAxisEdge();
double categoryStart=domainAxis.getCategoryStart(列,
getColumnCount(),dataArea,plot.getDomainAxisEdge();
双categoryWidth=categoryEnd-categoryStart;
双xx=类别开始;
int seriescont=getRowCount();
int categoryCount=getColumnCount();
如果(系列计数>1){
double seriesGap=dataArea.getWidth()*getItemMargin()
/(类别计数*(系列计数-1));
做