I want to draw a simple boxplot(Box and whisker plot) with some data from the user (1 boxplot not many) and I'm having problem with the DefaultBoxAndWhiskerCategoryDataset Variable in jfreechart. it seems that whatever data I enter just vanishes. the plot is always a triangle instead of a boxplot
package boxplot;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.BoxAndWhiskerToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BoxAndWhiskerRenderer;
import org.jfree.data.statistics.BoxAndWhiskerCategoryDataset;
import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class Boxplot extends ApplicationFrame{
@SuppressWarnings("deprecation")
public Boxplot(String title) {
super(title);
final BoxAndWhiskerCategoryDataset dataset = createDataset();
final CategoryAxis xAxis = new CategoryAxis("");
final NumberAxis yAxis = new NumberAxis("Value");
yAxis.setAutoRangeIncludesZero(false);
final BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
renderer.setFillBox(false);
renderer.setToolTipGenerator(new BoxAndWhiskerToolTipGenerator());
final CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
final JFreeChart chart = new JFreeChart(
"Box-and-Whisker Demo",
plot
);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(450, 270));
setContentPane(chartPanel);
}
public static void main(String[] args) {
final Boxplot plot = new Boxplot("");
plot.pack();
RefineryUtilities.centerFrameOnScreen(plot);
plot.setVisible(true);
}
private static DefaultBoxAndWhiskerCategoryDataset createDataset() {
System.out.print("Input the data (use space after every input)");
double[] inputData = getInputData();
ArrayList<Double> inputDataList = new ArrayList<Double>();
for (int i=0;i<100;i++)
inputDataList.add(i, inputData[i]);
final DefaultBoxAndWhiskerCategoryDataset dataset
= new DefaultBoxAndWhiskerCategoryDataset();
dataset.add(inputDataList, "1", "2");
return dataset;
}
private static double[] getInputData() {
Scanner scanner = new Scanner(System.in);
double[] data = new double[100];
Arrays.fill(data, -1);
int index =0;
do
{
double temp = scanner.nextDouble();
if (temp==-1)
break;
data[index++]= temp;
}while (scanner.hasNext());
scanner.close();
return data;
}
}