Visualize a boxplot graph with JFreeChart in Java

in boxplot •  7 years ago 

This small visualization is about boxplot graphs. A boxplot represents in just five numbers (Min, Max, Q25, Q75 and Median) every kind of interval of observations. If you have difficoulty remembering what a boxplot represents then have a look at boxplot article of Wikipedia

For this visualization we will use a Java library that is JFreeChart. JFreeChart is an open source library and it is free to use. Import it to your Java project and lets make the Java code that will visualize the boxplot graphics

Basically in this code we will :

1 - generate an interval of values to use it for the boxplot representation

2 - make the methods for representing the graph itself

The Java code for the boxplot graphic is ready to use and you can copy paste it in your Eclipse IDE after importing the jfreechart.jar file in your project

public class MyBoxPlot extends ApplicationFrame { // the MyBoxPlot library will extend the ApplicationFrame class form JFreeChart library

public MyBoxPlot(String titel) { // build the constructor of the class
super(titel); // call the constructor of ApplicationFrame

final BoxAndWhiskerXYDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);

final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(700, 400));
setContentPane(chartPanel);

}

private BoxAndWhiskerXYDataset createDataset() { // this method will generate the interval of random values to be represented as boxplots
final int ENTITY_COUNT = 14;

  DefaultBoxAndWhiskerXYDataset dataset = new 
          DefaultBoxAndWhiskerXYDataset("My BoxPlot Graphics");

  for (int i = 0; i  ENTITY_COUNT; i++) {
      Date date = DateUtilities.createDate(2003, 7, i + 1, 12, 0);
      List values = new ArrayList();
      for (int j = 0; j  10; j++) {
          values.add(new Double(10.0 + Math.random() * 10.0));
          values.add(new Double(13.0 + Math.random() * 4.0));
      }
      dataset.add(date, 
              BoxAndWhiskerCalculator.calculateBoxAndWhiskerStatistics(values));
  }
  return dataset;

}

private JFreeChart createChart(final BoxAndWhiskerXYDataset dataset) { // this method creates the chart itself
JFreeChart chart = ChartFactory.createBoxAndWhiskerChart(
"Box and Whisker Chart", "Time", "Value", dataset, true);
chart.setBackgroundPaint(new Color(249, 231, 236));

  return chart;

}

public static void main(final String[] args) { // finaly we visualize it using the main method by

  final MyBoxPlot demo = new MyBoxPlot(""); // creating the MyBoxPlot object  called demo  
  demo.pack();      // call the method pack() in it
  RefineryUtilities.centerFrameOnScreen(demo);  // center it in the screen 
  demo.setVisible(true);   // and make it visible 

}
}

And the result is as follows

Schermata 2017-11-03 alle 08.20.55.png

If you liked this guide follow datatreemap and stay tuned for more guides on programming and data visualization.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hi! I am a robot. I just upvoted you! Readers might be interested in similar content by the same author:
https://steemit.com/visualization/@datatreemap/visualize-a-boxplot-graph-with-jfreechart-in-java