<?php


function sendGetPage () {
  
//too many ' and " in heredoc?
  
$serverNamePhpSelf = $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];

  global
$data, $sorted, $scaled, $scalesize, $negatives, $thickness, $showdata, $bars;

  if (!isset(
$sorted) || $sorted=="yes")
    
$sortedYeschecked = "checked";
  elseif (
$sorted =="no")
    
$sortedNochecked = "checked";

  if (!isset(
$scaled) || $scaled=="no")
    
$scaledNochecked = "checked";
  elseif (
$scaled =="yes")
    
$scaledYeschecked = "checked";

  if (!isset(
$scalesize))
    
$scalesize = 500;

  if (!isset(
$negatives) || $negatives=="normal")
    
$negativesNormalchecked = "checked";
  elseif (
$negatives=="red")
    
$negativesRedchecked = "checked";
  elseif (
$negatives =="zero")
    
$negativesZerochecked = "checked";
  elseif (
$negatives =="skip")
    
$negativesSkipchecked = "checked";

  if (!isset(
$showdata) || $showdata=="no")
    
$showdataNochecked = "checked";
  elseif (
$showdata =="yes")
    
$showdataYeschecked = "checked";

  if (!isset(
$bars) || $bars=="vertical")
    
$barsVerticalchecked = "checked";
  elseif (
$bars =="horizontal")
    
$barsHorizontalchecked = "checked";

  if (!isset(
$thickness))
    
$thickness = 10;

echo <<< endOfGetPage
<html>
<head>
<title>PHP Data histogram</title>
</head>
<body>
<form name="histogramForm"
action="http://$serverNamePhpSelf";
method="post">
Enter/paste space or comma separated data
<input type="text" size="80"  name="data" value="$data">
<br>
Sort?
<input name="sorted" type="radio" value="yes" $sortedYeschecked>Yes
<input name="sorted" type="radio" value="no" $sortedNochecked>No
<br>
Scale?
<input name="scaled" type="radio" value="yes" $scaledYeschecked>Yes
<input name="scaled" type="radio" value="no" $scaledNochecked>No
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Scale size
<input name="scalesize" type="input" size="3" maxlength="4" value="$scalesize">
<br>
Negatives?
<input name="negatives" type="radio" value="normal" $negativesNormalchecked>Normal
<input name="negatives" type="radio" value="skip" $negativesSkipchecked>Skip
<input name="negatives" type="radio" value="zero" $negativesZerochecked>Zero length
<input name="negatives" type="radio" value="red" $negativesRedchecked>Red positive
<br>
Show data too?
<input name="showdata" type="radio" value="yes" $showdataYeschecked>Yes
<input name="showdata" type="radio" value="no" $showdataNochecked>No
<br>
Bars?
<input name="bars" type="radio" value="horizontal" $barsHorizontalchecked>Horizontal
<input name="bars" type="radio" value="vertical" $barsVerticalchecked>Vertical
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  Bar thickness:
<input name="thickness" type="input" size="2" maxlength="2" value="$thickness">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" value="Now">
</form>
endOfGetPage;
}

//initial page load.  display form
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
  
bail("");   
}



$data = $_POST['data'];
$sorted = $_POST['sorted'];
$scaled = $_POST['scaled'];
$scalesize = $_POST['scalesize'];
$negatives = $_POST['negatives'];
$thickness = $_POST['thickness'];
$showdata = $_POST['showdata'];
$bars = $_POST['bars'];

if (!
is_numeric($thickness) || $thickness<1)
     
$thickness = 10;
if (!
is_numeric($scalesize) || $scalesize<1)
     
$scalesize = 500;

if (!
strpos($data,",")) {     //no commas, assume is space separated
  
$data_array = explode(" ",$data);
}
else {
  
$data_array = explode(",",$data);
}
$cleandata = array();
foreach (
$data_array as $datum) {
  if (!
is_numeric($datum))
    echo
"bad datum: $datum<br>\n";
  else
    
$cleandata[] = $datum;
}
if (
$sorted == "yes")
     
sort($cleandata);

$mindatum = min($cleandata);
$maxdatum = max($cleandata);

if (
$negatives != "normal") {   //skip, at zero, or on positive
  
if ($scaled == "yes")  
    
$scalefactor = $scalesize / $maxdatum;
}
else if (
$scaled == "yes") {   //normal
  
$scalefactor = $scalesize / (-$mindatum+$maxdatum);
  
$zerooffset = -$mindatum * $scalefactor;
}
else  
//normal, no scaling
  
$zerooffset = -$mindatum;


sendGetPage();
echo
"<hr>\n";

//make a horizontal scale image
if ($bars=="horizontal" && $showdata=="no") {
  if (
$negatives != "normal") {   //skip, at zero, or on positive.  scale starts at 0
    
$scaleImagestart = 0;
    if (
$scaled == "yes")
      
$scaleImageLength = $scalefactor * $maxdatum;
    else
      
$scaleImageLength = $maxdatum;
  }
  else {
    
$scaleImagestart = $mindatum;
    if (
$scaled == "yes")
      
$scaleImageLength = $scalefactor * ($maxdatum - $mindatum);
    else
      
$scaleImageLength = $maxdatum - $mindatum;
  }
  
//$im = ImageCreateTrueColor($scaleImageLength+10, 10);
  
$im = ImageCreate($scaleImageLength+10, 12);   //fonts don't work in TrueColor??
  
ImageAlphaBlending($im, false);
  
ImageFilledRectangle($im, 0, 0, $scaleImageLength+10, 10, ImageColorAllocate($im, 255, 255, 255));
  
$blue = ImageColorAllocate($im, 0, 0, 255);
  
ImageLine($im, 3, 1, $scaleImageLength+3, 1, $blue);
  if (
$negatives!="normal" || $mindatum>=0) {
    
//ImageLine($im, 3, 1, 3, 5, $blue);
    
ImageTTFText($im, 10, 0, 1, 11, $blue, 'courb', "0");
  }
  else {
    
//ImageLine($im, $zerooffset+3, 1, $zerooffset+3, 5, $blue);
    
if ($mindatum < 0)
      
ImageTTFText($im, 10, 0, 1, 11, $blue, 'courb', $mindatum);
    
ImageTTFText($im, 10, 0, $zerooffset-2, 11, $blue, 'courb', "0");
  }
  
ImageTTFText($im, 10, 0, $scaleImageLength-5, 11, $blue, 'courb', $maxdatum);
  
ImagePNG($im,"histogramhorizscale.png");
  echo
"<img src=\"histogramhorizscale.png\">\n";
}   


//echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n";
echo "<table>\n";
if (
$bars == "horizontal") {
  foreach (
$cleandata as $datum) {
    echo
"<tr>";
    if (
$showdata=="yes" && !($negatives=="skip" && $datum<0))
      echo
"<th>$datum</th>";
    if (
$scaled == "yes")
      
$datum *= $scalefactor;
    if (
$negatives != "normal") {   //zero is at left border
      
if ($datum >= 0)
    echo
"<td><img src=\"histogramblack.png\" width=\"".$datum."\" height=\"$thickness\"></td></tr>";
      else if (
$negatives == "red")
    echo
"<td><img src=\"histogramred.png\" width=\"".-$datum."\" height=\"$thickness\"></td></tr>";
      else if (
$negatives == "zero")
    echo
"<td></td></tr>";
    }
    else {    
//normal.    zero is floatable...
      
if ($datum >= 0) {
    echo
"<td><img src=\"histogramwhite.png\" width=\"".$zerooffset."\" height=\"$thickness\"></td>";
    echo
"<td><img src=\"histogramblack.png\" width=\"".$datum."\" height=\"$thickness\"></td></tr>";
      }
      else {
    
//2 images in cell:
    
echo "<td><img src=\"histogramwhite.png\" width=\"".($zerooffset+$datum)."\" height=\"$thickness\">";
    echo
"<img src=\"histogramred.png\" width=\"".-$datum."\" height=\"$thickness\"></td>";
    echo
"<td></td></tr>";
      }
    }
  }
}
else {   
//vertical bars
  
echo "<tr>";
  if (
$showdata=="yes" && !($negatives=="skip" && $datum<0)) {
    foreach (
$cleandata as $datum)
      echo
"<th>$datum</th>";
    echo
"</tr><tr>\n";
  }
  if (
$negatives != "normal") {
    foreach (
$cleandata as $datum) {
      if (
$scaled == "yes")
    
$datum *= $scalefactor;
      if (
$datum >= 0)
    echo
"<td valign=\"bottom\"><img src=\"histogramblack.png\" height=\"".$datum."\" width=\"$thickness\"></td>";
      else if (
$negatives == "red")
    echo
"<td valign=\"bottom\"><img src=\"histogramred.png\" height=\"".-$datum."\" width=\"$thickness\"></td>";
      else if (
$negatives == "zero")
    echo
"<td></td>";
    }
  }
  else {    
//normal.    zero is floatable...
    //positives row:
    
foreach ($cleandata as $datum) {
      if (
$scaled == "yes")
    
$datum *= $scalefactor;
      if (
$datum >= 0)
    echo
"<td valign=\"bottom\"><img src=\"histogramblack.png\" height=\"".$datum."\" width=\"$thickness\"></td>";
      else
    echo
"<td></td>";
    }
    echo
"</tr><tr>\n";
    
//negatives row:
    
foreach ($cleandata as $datum) {
      if (
$scaled == "yes")
    
$datum *= $scalefactor;
      if (
$datum >= 0)
    echo
"<td></td>";
      else
    echo
"<td  valign=\"top\"><img src=\"histogramred.png\" height=\"".-$datum."\" width=\"$thickness\"></td>";
    }
  }
}





echo
"</table>\n";

echo
"</body>\n</html>";





function
bail ($reason) {
  echo
$reason;
  
sendGetPage();
  echo
"</body>\n</html>";
  exit();
}

?>