//The complete free software package may be dowloaded from http://tyaga.org/kit/.
<?php
if (!is_numeric($per_2)) {
function in_period($date,$per_1,$per_2) {
list($year,$month,$day) = explode("-",$date);
//if ($year>=$per_1['year'] AND $year>=$per_2['year']) { //plan to support multiyear eval in the future
if ($month>=$per_1['month'] AND $month<=$per_2['month']) {
if ($day>=$per_1['day'] AND $day<=$per_2['day']) {
return true;
}
}
//}
}
}
else {
function in_period($date,$per_1,$per_2) {
if (substr($date,0,$per_2)== $per_1) { //echo "true";
return true;
}
else { //echo "false";
return false;
}
}
}
function valid_amt_units($amount,$units,$native_units) {
if (is_numeric($amount)) { return true;
/*if ($units == $native_units OR $units == $native_units ."s" OR $units == substr($native_units,0,-1)) { echo "valid amt_units";
return true;
}
else { echo "invalid amt units";
return false;
} */
}
else {return false;}
}
function graph_array($array,$width="",$height="",$chart_x_pad="",$chart_y_pad="",$force_y_min="",$force_y_max="",$decimals="") {
//print_r($array); echo "<br />";
reset($array);
//the following are starting values that will be changed as needed in the next foreach loop
$min_date=substr(key($array),5); $min_x_value=day_num(key($array)); //echo "<br />min_x ". $min_x_value;
end($array);
$max_date=substr(key($array),5); $max_x_value=day_num(key($array)); //echo "<br />max_x ". $max_x_value;
$min_y_value=min($array);
$max_y_value=max($array);
foreach($array as $key=>$value) { //echo "<br />day_num=". $key ." ". $value;
$converted_key = day_num($key);
if ($converted_key>$max_x_value) {$max_x_value=$converted_key; }
else if ($converted_key<$min_x_value) {$min_x_value=$converted_key; }
}
if (is_numeric($force_y_min)) {$min_y_value=$force_y_min;}
if (is_numeric($force_y_max)) {$max_y_value=$force_y_max;}
if (!$width) {$width=300;}
if (!$height) {$height=300;}
if (!$chart_x_pad) {$chart_x_pad=10;}
if (!$chart_y_pad) {$chart_y_pad=10;}
if (!$decimal) {$decimal=0;}
$x_scale_factor = $width/($max_x_value-$min_x_value); //echo "<br />x-scale =". $x_scale_factor;
$y_scale_factor = $height/($max_y_value-$min_y_value); //echo "<br />y-scale =". $y_scale_factor;
foreach ($array as $key => $value) { //echo "<br />key, value: ". $key ." ". $value;
$poly_string = $poly_string ." ". ($x_scale_factor*(day_num($key) - $min_x_value)+$chart_x_pad) .",". ($height+$chart_y_pad-$y_scale_factor*($value-$min_y_value));
}
$grid100_y = $height+$chart_y_pad-$y_scale_factor*($max_y_value-$min_y_value);
$grid75_y = $height+$chart_y_pad-$y_scale_factor*(0.75*$max_y_value-$min_y_value);
$grid50_y = $height+$chart_y_pad-$y_scale_factor*(0.5*$max_y_value-$min_y_value);
$grid25_y = $height+$chart_y_pad-$y_scale_factor*(0.25*$max_y_value-$min_y_value);
$grid0_y = $height+$chart_y_pad-$y_scale_factor*(0-$min_y_value);
return "<svg version=\"1.1\" baseProfile=\"full\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:ev=\"http://www.w3.org/2001/xml-events\">
<rect x=\"$chart_x_pad\" y=\"$chart_y_pad\" width=\"$width\" height=\"$height\" style=\"fill:blue;stroke:pink;stroke-width:1;fill-opacity:0.1;stroke-opacity:0.9\" />
<polyline points=\"". $chart_x_pad .",". $grid75_y ." ". ($chart_x_pad+$width) .",". $grid75_y ."\" style='fill:none;stroke:grey;stroke-width:1'/>
<polyline points=\"". $chart_x_pad .",". $grid50_y ." ". ($chart_x_pad+$width) .",". $grid50_y ."\" style='fill:none;stroke:grey;stroke-width:1'/>
<polyline points=\"". $chart_x_pad .",". $grid25_y ." ". ($chart_x_pad+$width) .",". $grid25_y ."\" style='fill:none;stroke:grey;stroke-width:1'/>
<polyline points=\"". $chart_x_pad .",". $grid0_y ." ". ($chart_x_pad+$width) .",". $grid0_y ."\" style='fill:none;stroke:grey;stroke-width:1'/>
<polyline points=\"". $poly_string ."\" style='fill:none;stroke:red;stroke-width:2'/>
<rect x=\"". ($chart_x_pad-1) ."\" y=\"-1\" width=\"$width\" height=\"$chart_y_pad\" style=\"fill:white;stroke:none;stroke-width:1;fill-opacity:1;stroke-opacity:0.9\" />
<rect x=\"$chart_x_pad\" y=\"". ($chart_y_pad+$height) ."\" width=\"$width\" height=\"". ($chart_y_pad+$height) ."\" style=\"fill:white;stroke:none;stroke-width:1;fill-opacity:1;stroke-opacity:0.9\" />
". graph_label($max_y_value,$decimals,$chart_x_pad,$grid100_y) ."
". graph_label(0.75*$max_y_value,$decimals,$chart_x_pad,$grid75_y) ."
". graph_label(0.5*$max_y_value,$decimals,$chart_x_pad,$grid50_y) ."
". graph_label(0.25*$max_y_value,$decimals,$chart_x_pad,$grid25_y) ."
". graph_label(0,$decimals,$chart_x_pad,$grid0_y) ."
". graph_label($min_y_value,$decimals,$chart_x_pad,$chart_y_pad+$height) ."
". graph_label($min_date,$decimals,$chart_x_pad+10,$chart_y_pad+$height+20) ."
". graph_label($max_date,$decimals,($chart_x_pad+$width),$chart_y_pad+$height+20) ."
</svg>";
}
function graph_label($label,$decimals,$x_pad,$y) { //echo "<br />". $label;
if (is_numeric($label)) {
if ($label==0) {
$offset = ($decimals==0)?25:40;
$x_val = $x_pad + 10*$decimals - $offset;
}
else if ($label>0){
if ($label<1) {
$x_val = $x_pad -10*(floor(log10($label)) + $decimals) - 30;
}
else {
$x_val = $x_pad -10*(floor(log10($label)) + $decimals) - 20;
}
}
else {
$x_val = $x_pad -10*(floor(log10(-1*$label)) + $decimals) - 25;
}
$label = number_format($label,$decimals);
}
else {
$x_val = $x_pad -10*(strlen($label)) + 30;
}
return "<text style=\"fill:black; stroke:none\" x=\"". $x_val ."\" y=\"". ($y+5) ."\">". $label ."</text>";
}
function day_num($date) {
list($yy,$mm,$dd)=explode("-",$date);
switch($mm){
case 1: $offset=0; if ($dd>31) {$dd=31;} break;
case 2: $offset=31;
if ($dd>28) { if (is_int(($yy-2000)/4)) {$dd=29;} else {$dd=28;} } break;
case 3: $offset=58; if ($dd>31) {$dd=31;} break;
case 4: $offset=90; if ($dd>30) {$dd=30;} break;
case 5: $offset=120; if ($dd>31) {$dd=31;} break;
case 6: $offset=151; if ($dd>30) {$dd=30;} break;
case 7: $offset=181; if ($dd>31) {$dd=31;} break;
case 8: $offset=212; if ($dd>31) {$dd=31;} break;
case 9: $offset=243; if ($dd>30) {$dd=30;} break;
case 10:$offset=273; if ($dd>31) {$dd=31;} break;
case 11:$offset=304; if ($dd>30) {$dd=30;} break;
case 12: $offset=334; if ($dd>31) {$dd=3;} break;
}
if ($mm<3 AND is_int(($yy-2000)/4)) {$offset=$offset+1;}
return $offset+$dd;
}
?>
1