Friday, March 19, 2010

Add Graphs by Using JFree Chart

Recently i want to add graphs in PMS application. There are too many ways to do this.One of them is JFree Chart.Steps for this are following..

Step1: Download Demo Application of JfreeChart demo and get library jar.

Step2: Add following lines in your code and import files which is required.

JFreeChart chart = ChartFactory.createPieChart("Pie Chart created by Deepak Pandey", pieDataset, true, true,true);
BufferedImage bi = chart.createBufferedImage(500, 500);
chart.setBackgroundPaint(new Color(173, 230, 163));
BufferedImage buf = chart.createBufferedImage(500, 500, null);
try {
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buf);
param.setQuality(0.75f, true);
encoder.encode(buf, param);

} catch (Exception e) {
System.out.println("Exception in JSP"+e);
}

Step3: Run you application

You can create this chart on a frame.
Create JFrame Object. and then add JFreeChart object and make this JFrame Object visible.

More Information of this JFreeChart,access following link
http://www.jfree.org

Thanks!!!

No comments:

Post a Comment