Google Search

Sunday, May 29, 2016

Create JPEG Image Graph from CSV File

import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import com.opencsv.CSVReader;

public class LineChart
{


   public static void main( String[ ] args ) throws Exception
   {
  DefaultCategoryDataset line_chart_dataset = new DefaultCategoryDataset();
  String strFile = "F:\\Java\\Sample.csv";
  int  ColCount=getCols("F:\\Java\\Sample.csv");
 
  FileReader fr;
       fr = new FileReader("F:\\Java\\Sample.csv");
     
     
     
       String[] Headers = getHeaders("F:\\Java\\Sample.csv");
       System.out.println(Headers[0]);
 //Creation of DataSet Starts Here
       for (int k =0; k<ColCount;k++){
   

     CSVReader reader = new CSVReader(new FileReader(strFile));
   
     String [] nextLine;
     int lineNumber = 0;
     nextLine = reader.readNext();
   
   
 

     while ((nextLine = reader.readNext()) != null) {
       lineNumber++;
     
       line_chart_dataset.addValue( Double.parseDouble((nextLine[k])) , Headers[k] , "X" +lineNumber );
   

     }//End of While Loop
   
  }//End of For Loop - k
     
     
      // Chart Creation Starts here
      JFreeChart lineChartObject = ChartFactory.createLineChart(
         "Ececution Stats","Execution Number",
         "Value",
         line_chart_dataset,PlotOrientation.VERTICAL,
         true,true,false);

      int width = 1280; /* Width of the image */
      int height = 480; /* Height of the image */
      File lineChart = new File( "F:\\Java\\LineChart.jpeg" );
      ChartUtilities.saveChartAsJPEG(lineChart ,lineChartObject, width ,height);
   }
 
 
 
   public static int getCols(String xFileLocation) throws FileNotFoundException{

String InputLine = "";
int Rows = 0;
int Cols=0;
String[] InArray = null;

//String xFileLocation = "F:\\Java\\Sample.csv";
Scanner scanIn = new Scanner (new BufferedReader(new FileReader(xFileLocation)));
scanIn.useDelimiter(",");

while(scanIn.hasNextLine()){
InputLine = scanIn.nextLine();
InArray = InputLine.split(",");
Rows++;
Cols = InArray.length;

}

return Cols;
//System.out.println("Count of Rows is : " +Rows +" Count of Cols is "  + Cols);

}

public static int getRows(String xFileLocation) throws FileNotFoundException{

String InputLine = "";
int Rows = 0;
int Cols=0;
String[] InArray = null;

//String xFileLocation = "F:\\Java\\Sample.csv";
Scanner scanIn = new Scanner (new BufferedReader(new FileReader(xFileLocation)));
scanIn.useDelimiter(",");

while(scanIn.hasNextLine()){
InputLine = scanIn.nextLine();
InArray = InputLine.split(",");
Rows++;
Cols = InArray.length;

}

return Rows;
//System.out.println("Count of Rows is : " +Rows +" Count of Cols is "  + Cols);

}

public static String[] getHeaders(String xFileLocation) throws IOException{

FileReader fr;
    fr = new FileReader("F:\\Java\\Sample.csv");
   
   
    BufferedReader br = new BufferedReader(fr);
    String line = br.readLine();
    StringTokenizer st = new StringTokenizer(line, ",");
    String[] Headers = new String[getCols("F:\\Java\\Sample.csv")];
    int i = 0;
    final DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
    //String xLabel = st.nextToken();
   // System.out.println(xLabel);
    while(st.hasMoreTokens()){
    Headers[i] = st.nextToken();
 
   // System.out.println(Headers[i]);
    i=i+1;
    }

return Headers;


}

 
}