在JFreeChart中创建一个简单的boxplot

时间:2016-10-08 11:34:16

标签: java jfreechart

我无法使用JFreeChart将数据集转换为正确的格式来创建Box和Whisker图表:

我见过几个在列表中创建数组但是我只有两个列表的例子,所以我想我可以将它们添加到数据集中。但是我得到一张空白图表(如我所见,但是没有数据)。没有错误,没有任何标记。有人能告诉我如何获得正确的数据吗?  数据集包含一个多维数组,构造如下:

 final DefaultBoxAndWhiskerCategoryDataset dataset = new DefaultBoxAndWhiskerCategoryDataset();
               List<List<Double>> Overall = new ArrayList<List<Double>>();
               double[] weights_LArr = new double[weights_L.size()];
               double[] weights_RArr = new double[weights_R.size()];
               List<Double> listL = new ArrayList<Double>();
               List<Double> listR = new ArrayList<Double>();

                              for ( int i = 0; i < weights_LArr.length; i++) {
                                weights_LArr[i] = weights_L.get(i);                
                                listL.add(weights_L.get(i));  
                              }



                            for ( int i = 0; i < weights_RArr.length; i++) {
                                weights_RArr[i] = weights_R.get(i); 
                                listR.add(weights_R.get(i));    
                              }        

                    Overall.add(listL);
                    Overall.add(listR);
                    dataset.add(Overall, "Type ", " Number ");



final BoxAndWhiskerCategoryDataset datasetBW = dataset;

         final CategoryAxis xAxis = new CategoryAxis("Type");
         final NumberAxis yAxis = new NumberAxis("Value");
         yAxis.setAutoRangeIncludesZero(false);
         final BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
         renderer.setFillBox(false);
         final CategoryPlot plot = new CategoryPlot(datasetBW, xAxis, yAxis, renderer);
         final JFreeChart chart = new JFreeChart(
                            "Box-and-Whisker Demo",
                            new Font("SansSerif", Font.BOLD, 14),
                            plot,
                            true
                        );
        final ChartPanel chartPanel = new ChartPanel(chart);
                     // ChartPanel chartpanel = new ChartPanel(chart);
                    chartPanel.setDomainZoomable(true);

                    JPanel jPanel4 = new JPanel();
                    jPanel4.setLayout(new BorderLayout());
                    jPanel4.add(chartPanel, BorderLayout.NORTH);

                    JFrame frame = new JFrame();
                    frame.add(jPanel4);
                    frame.pack();
                    frame.setVisible(true);

1 个答案:

答案 0 :(得分:2)

数据集的add()方法需要List<E>个值,而不是List<List<E>>。看看这个example,尝试这样的事情:

dataset.add(listL, "Weights", "Left");
dataset.add(listR, "Weights", "Right");