<?php
$charttxt = "";
$icsv = 0;
$chartlab = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$charttype = "string";
$chartdelim = "";
$chartpattern = "";
$dq = '"';
$oq = "'";
$ifile = "intensity_chart.csv";
if (!isset($_GET['infile'])) {
  $ifile = $_GET['infile'];
}
if (isset($ifile)) {
  if (file_exists($ifile)) {
    $file_handle = fopen($ifile, "r");
    while (($buffer = fgets($file_handle, 4096)) !== false) {
         
        $xtd = 0;
        if (isset($_GET['delim'])) {
          $csvcolumns = split($_GET['delim'], $buffer);
        } else {
          $csvcolumns = split(",", $buffer);
        }
        if ($icsv == 0) { 
          for ($xtd=0; $xtd < sizeof($csvcolumns); $xtd++) { 
            if (strlen($charttxt) == 0) $charttxt = 'google.visualization.Query.setResponse({"version":"0.6","status":"ok","sig":"","table":{"cols":[';
            if (strlen($charttxt) == 0) $charttxt = 'google.visualization.Query.setResponse({"version":"0.6","status":"ok","sig":"565811536","table":{"cols":[';
            $charttxt = $charttxt . $chartdelim . '{"id":"' . substr($chartlab,$xtd,1); 
            $chartdelim = ','; 
            $charttxt = $charttxt . '","label":"';        
            $charttxt = $charttxt . str_replace($dq,"",str_replace($oq,"",str_replace("\n","",$csvcolumns[$xtd]))); 
            $charttxt = $charttxt . '","type":"';         
            $charttxt = $charttxt . $charttype;
            $charttype = 'number';
            $charttxt = $charttxt . '","pattern":"';     
            $charttxt = $charttxt . $chartpattern . '"}';
            $chartpattern = '#0.###############';
           } 
           $charttxt = $charttxt . '],"rows":[';  
           $chartdelim = ''; 
        } else if (str_replace($dq,"",str_replace($oq,"",$csvcolumns[0])) != "") {
           $charttxt = $charttxt . $chartdelim . '{"c":[{"v":"';    
           $chartdelim = ']},';
           $charttxt = $charttxt . str_replace($dq,"",str_replace($oq,"",$csvcolumns[0]));
           for ($xtd=1; $xtd < sizeof($csvcolumns); $xtd++) { 
            $charttxt = $charttxt . '"},{"v":';  
            $charttxt = $charttxt . str_replace("\n","",$csvcolumns[$xtd]);
            $charttxt = $charttxt . ',"f":"';   
            $charttxt = $charttxt . str_replace("\n","",$csvcolumns[$xtd]);
           } 
           $charttxt = $charttxt . '"}';
        }

        $icsv = $icsv + 1; 
    }
    fclose($file_handle);
    if (strlen($charttxt) > 0) $charttxt = $charttxt . ']}]}});';  
  }
}
echo $charttxt;


?>

