Tuesday, November 20, 2012

TOP N-Analysis using JFreeChart

This post will narrate about plotting a Top-N analysis chart using JFreeChart.


Top-N analysis is used in many occasions like "No of defects pending" per tester or "outstanding components with more defects". When i tried to implement the same in JFreeChart i faced lot of issues, so that of sharing my learning to JFreeChart users. Here i have used bar chart with horizontal orientation. The percentage bar is made invisible from user view, by drawing the same in white color. I have also suppressed legends from view.

//set of headers to be included
import java.awt.Color;
import java.awt.Font;
import java.io.*;
import java.text.DecimalFormat;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.ui.ApplicationFrame;
import java.io.File; 
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
  
public class HorizonBarChart extends ApplicationFrame
{
         @SuppressWarnings("deprecation")                 
         //Constructor
         HorizonBarChart(final String title,String ipfilename,String Datasheetname,String opfilename)
         {
           super(title);                                             
           DefaultCategoryDataset dataset  = createDataset(ipfilename,Datasheetname);          
           JFreeChart chart = ChartFactory.createBarChart( title, 
                    "", 
                   "# of defect pending", 
                   dataset, 
                   PlotOrientation.HORIZONTAL,  //Sets chart orientation as Horizontal
                   false, 
                   false, 
                  false );                         
           final CategoryPlot plot = chart.getCategoryPlot();
         //Customization of chart
        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        plot.setBackgroundPaint(null);
        plot.setOutlineVisible(false);             
        plot.getRenderer().setSeriesPaint(0, new Color(0, 0, 255));   

        //This line ensure the other bar is not visible to user at all, since they are drawn  in white color
        plot.getRenderer().setSeriesPaint(1, new Color(255, 255, 255));  
  
       //Only numbers are displayed in range axis
        final NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();     
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());         
        //sets font & margin on domain axis
        final CategoryAxis domainAxis = plot.getDomainAxis();     
        domainAxis.setCategoryMargin(0.2);      
        Font ft = new Font("SansSerif",Font.BOLD|Font.ROMAN_BASELINE, 12 ) ;
        domainAxis.setTickLabelFont(ft);
        domainAxis.setMaximumCategoryLabelWidthRatio(1);          
       
        BarRenderer renderer = ( BarRenderer)plot.getRenderer();
        renderer.setItemMargin(0.3);       
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBaseItemLabelsVisible(true);
      
          // " % " is shown next to series value
          CategoryItemLabelGenerator categoryLabel = new StandardCategoryItemLabelGenerator("{2}",
          new DecimalFormat("0%"));     
          renderer.setSeriesItemLabelGenerator(1,categoryLabel);                               
          //series value are shown next to bars
          renderer.setSeriesItemLabelsVisible(0, true);
          renderer.setSeriesItemLabelsVisible(1, true);
                               
          Font rft = new Font("SansSerif",Font.BOLD|Font.ROMAN_BASELINE, 12 ) ;
          renderer.setSeriesItemLabelFont(0, rft);
          renderer.setSeriesItemLabelFont(1, rft);               
          //Chart generation       
           try {
              ChartUtilities.saveChartAsJPEG(new File(opfilename), chart,800, 700);      
               } catch (IOException e) {
              System.out.println("Problem in creating chart.");
              e.printStackTrace();
              }//End of try
           }                     
           //Main function 
           public static void main(String args[]) throws Exception       
               {   new VerticalBarChart("chart title","No such file","Data_2","No such file");  }
           private static DefaultCategoryDataset createDataset(String filename,String sheetname) {
                 DefaultCategoryDataset dataset1= new DefaultCategoryDataset();
                  Double x=0.0; 
                  String z=null;   
                  try
                               {                   
                                   FileInputStream fs =new FileInputStream(filename);        
                                    HSSFWorkbook wb = new HSSFWorkbook(fs);  
                                     HSSFSheet sheet = wb.getSheet(sheetname);      
                                     //The below section illustrates how to insert the values in dataset.
                                     //Please add your appropriate logic here.
                                     //This section is just to give an abstract view about dataset values
                                                 HSSFCell cell1 = sheet.getRow(r).getCell(col);
                                                 x=(double)cell1.getNumericCellValue();                                               
                                                 if(col==row.getLastCellNum()-1)
                                                     {
                                                          //To set the value in terms of percentage                                
                                                           x=x/100;  
                                                     }
                                                HSSFCell cell3 = sheet.getRow(r).getCell(row.getLastCellNum());
                                                 z=cell3.toString();                                               
                                                dataset1.addValue(x,"y-String to be constant",z);
                                        }
                  }//try       
                 catch(Exception e){
                                  e.printStackTrace();}
                 return dataset1;
              }
}

Input for this chart sample, is cited below.Happy Analysis!!

No comments:

Post a Comment