Saturday, September 13, 2014

JFree Chart for Test Case Execution Status


import java.io.File;
import java.io.IOException;
import org.jfree.chart.*;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;


 public void pieChartReport()

 {

  // Creating a simple pie chart with 
DefaultPieDataset dataset = new DefaultPieDataset();

   dataset.setValue("PASSED", new Integer(60));
   dataset.setValue("FAILED", new Integer(25));
   dataset.setValue("SKIPPED", new Integer(10));
   dataset.setValue("NOTRUN", new Integer(5));


  JFreeChart piechart = ChartFactory.createPieChart("Test Cases Execution  Status", dataset, true, true, false);
PiePlot plot = (PiePlot) piechart.getPlot();

PieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator("{0} = {2}", new DecimalFormat("0"),new DecimalFormat("0.00%"));

       
        plot.setLabelGenerator(generator);
        plot.setBackgroundPaint(Color.WHITE);
        plot.setSectionPaint("PASSED", Color.GREEN);
        plot.setSectionPaint("FAILED", Color.RED);
        plot.setSectionPaint("SKIPPED", Color.BLUE);
        plot.setSectionPaint("NOTRUN", Color.YELLOW);

   try {

   ChartUtilities.saveChartAsJPEG(new File(

"D:\\simplePiechart.jpg"), 1, piechart, 640, 480);

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }
 
Note: Required JFreeChart and JCommon jars.
 

No comments:

Post a Comment