Home Tutorial Java Jfreechart Create Histogram using Jfreechart

 
 

Share on Google+Share on Google+
Create Histogram using Jfreechart
Posted on: October 22, 2009 at 12:00 AM
Advertisement
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.io.*;
import java.util.Random;
import org.jfree.chart.*;
import org.jfree.data.statistics.*;
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();
       dataset.setType(HistogramType.RELATIVE_FREQUENCY);
       dataset.addSeries("Histogram",value,number);
       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) {}
         }
   }
 }
Advertisement

Related Tags for Create Histogram using Jfreechart:


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.

Posted on: October 22, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com