Share on Google+Share on Google+

Create Histogram using Jfreechart

In this section, you will learn how to create histogram using jfreechart library.

Create Histogram using Jfreechart

In this section, you will learn how to create histogram using jfreechart.

To create a Histogram, we have used the class  HistogramDataset. Then method addSeries()  allows to add the series of ten randomly generated numbers to the dataset, using the specified number of bins. The PlotOrientation.VERTICAL sets the vertical orientation of the Histogram. ChartFactory.createHistogram() have created the Histogram. Using the class ChartUtilities, we have converted the Histogram to image foramt(PNG). The method saveChartAsJPEG(new File("histogram.PNG") saves the chart to a file specified in PNG format.

Here is the code:

import java.util.Random;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;

 public class HistogramExample {
       public static void main(String[] args) {
       double[] value = new double[100];
       Random generator = new Random();
       for (int i=1; i < 100; i++) {
       value[i= generator.nextDouble();
           int number = 10;
       HistogramDataset dataset = new HistogramDataset();
       String plotTitle = "Histogram"
       String xaxis = "number";
       String yaxis = "value"
       PlotOrientation orientation = PlotOrientation.VERTICAL; 
       boolean show = false
       boolean toolTips = false;
       boolean urls = false
       JFreeChart chart = ChartFactory.createHistogramplotTitle, xaxis, yaxis, 
                dataset, orientation, show, toolTips, urls
       int width = 500;
       int height = 300
        try {
        ChartUtilities.saveChartAsPNG(new File("histogram.PNG"), chart, width, height);
        catch (IOException e) {}



Posted on: October 22, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.