Sebastian Zeki
Sebastian Zeki

Reputation: 6874

Create a simple boxplot in JFreeChart

I'm having trouble getting my dataset into the correct format to create a Box and Whisker chart using JFreeChart:

I've seen several examples which create Arrays within a list but I have only two lists so I thought I could add them to a the dataset. However I just get a blank chart (as in I can see the chart but there's no data on it). There are no errors just nothing plotted. Can someone tell me how I get the data in the correct shape? The dataset contains a multidimensional array constructed as follows:

 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);

Upvotes: 2

Views: 735

Answers (1)

Catalina Island
Catalina Island

Reputation: 7136

The dataset's add() method expects a List<E> of values, not a List<List<E>>. Looking at this example, try something like this:

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

Upvotes: 2

Related Questions