$vmax) { $vmax = intval($val); # You could add a +1 here for the same reason. }; $k++ ; # Increments +k ; }; }; $c++; }; if($style == 'stackedbar') { $vmin = 0 ; $vmax = $vmax * 2 ; } ; #there are probably better ways.. but this seems to work well. if($style == 'stackedline') { $vmin = 0 ; $vmax = $vmax * 2 ; } ; #it stacks data into the center of the chart well. #--------------------- $ycolor = imagecolorallocate($image, 0, 0, 127); #set the color for the Y values and Y text. if ($base == 'F') { $hres = intval($c/($width/25)) +1; $v = $vmax-$vmin; $vres = intval($v/($height/25)) +1; # create a grid $hval = 0; for ($i = 1;$i < intval($width/25);$i++) { imageline($image, $i*25, 0, $i*25, $height, $grey); } for ($i = 1;$i < intval($width);$i++) { if ($i > 1) { imagettftext($image, 10, -45, ($i*25) -35, $height+10, $green, $font, $labels[$i*$hres]); #labels at bottom of graph of values. #-45 gives it a slant. }; $hval = $hval+$hres; } #vertical grid markers. $vval = $vmin; for ($i = 1;$i < intval($height/25) +2;$i++) { imagettftext($image, 8, 0, $width+5, $height-(($i*25) -35), $ycolor, $font, $vval); #right at left side scale $vval = $vval+$vres; } imagettftext($image, 10, 0, $width-10, $height+30, $ycolor, $font, $what); for ($i = 1;$i < intval($height/25);$i++) { #add +1 for bottom line imageline($image, 0, $i*25, $width, $i*25, $grey); } $base = 'T'; #keeps from drawing these for each data set. }; # end base = true ; $c = $c-1; $vr = 25/$vres; $hr = 25/$hres; imagesetthickness($image, 4); #sets line thickness $k = 0 ; #----------------------------------------------------------------LINE/DATA/SET DRAWING foreach($values as $set) { if($k == 0) { $linecolor = imagecolorallocate($image, 0, 0, 255); } elseif ($k == 1) { $linecolor = imagecolorallocate($image, 0, 255, 0); } elseif ($k == 3) { $linecolor = imagecolorallocate($image, 255, 0, 255); } else { $linecolor = imagecolorallocate($image, 0, 191, 191 + ($k*10) ); } ; #This gets done for each data set. for ($i = 0;$i < $c;$i++) { $v1 = $height-(($set[$i]-$vmin) *$vr); $v2 = $height-(($set[$i+1]-$vmin) *$vr); if ($style == 'bar') { if ($hr < 1) { imageline($image, $i*$hr, $v1, ($i+1) *$hr, $v2, $linecolor); #Makes it a line if horiz res < 1 # imagefilledrectangle($image, ($i*$hr), $v1, ($i*$hr) + $k, $height, $linecolor); #or a bad bar } else { imagefilledrectangle($image, ($i*$hr), $v1, ($i*$hr) +($hr/2) +$k, $height, $linecolor); } ; } elseif ($style == 'stackedbar') { if($hv1[$i] == '') { $hv1[$i] = 0 ; } ; imagefilledrectangle($image, ($i*$hr), $v1-$hv1[$i], ($i*$hr) +($hr/2), $height-$hv1[$i], $linecolor); $hv1[$i] = ($height-$v1) + $hv1[$i] ; } elseif ($style == 'stackedline') { if($hv1[$i] == '') { $hv1[$i] = 0 ; } ; if($hv2[$i] == '') { $hv2[$i] = 0 ; } ; # imagefilledrectangle($image, ($i*$hr), $v1-$hv1[$i], ($i*$hr) +($hr/2), $height-$hv1[$i], $linecolor); imageline($image, $i*$hr, $v1-$hv1[$i], ($i+1) *$hr, $v2-$hv2[$i], $linecolor); $hv1[$i] = ($height-$v1) + $hv1[$i] ; $hv2[$i] = ($height-$v2) + $hv2[$i] ; } else { imageline($image, $i*$hr, $v1, ($i+1) *$hr, $v2, $linecolor); }; } # add some words for debugging # $text = "$what - $keys[$k] - min: $vmin max: $vmax $vres/$hres c: $c"; # imagettftext($image, 10, 0, 20, $height+30+($k*12), $linecolor, $font, $text); imagettftext($image, 10, 0, $width+30, ($height)-($k*12), $linecolor, $font, $keys[$k] ); $k++ ; }; #---------------------------------------------------------------END LINE/DATA/SET DRAWING #print_r($values) ; header("Content-type: image/png"); header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past imagepng($image); imagedestroy($image); }; function glconnect() { $db = mysql_connect('localhost','root', ''); #Put the Host, Login, Password to your MySQL Server here. mysql_select_db('mydatabase', $db); #Put the name of your database here. return $db; }; // end function glconnect ; #An example query # $query1 = "select date_format(billingtimestamp,'%m/%d/%y') as label, format(sum(t1dfra)/1000,1) as Tier1,format(sum(t2dfra)/1000,1) as Tier2,format(sum(t3dfra)/1000,1) as Tier3,format(sum(t4dfra)/1000,1) as Tier4 from meterreads group by year(billingtimestamp),month(billingtimestamp) order by billingtimestamp limit 100"; #More Generic Example. Field field should be the label, the rest numeric fields you want to graph $query1 = "select date_format(mydate,'%m/%d/%y') as label, numfield1, numfield2,numfield3 ,format(sum(t4dfra)/1000,1) as Tier4 from mydatatable group by year(billingtimestamp),month(billingtimestamp) order by billingtimestamp limit 100"; #THIS IS WHAT MAKES IT WORK! geekgauge3(800, 200, $query1, 'kWh', 'stackedbar'); #valid values: stackedbar,stackedline,bar,line ; #The idea is a graph widget that can create auto-scaling graphs of data in MySQL.. #Quickly, as a PNG. for embedding in websites. especially low tech browser ones. # Want to see it work? # 1. Setup glconnect() above to connect to a mysql table # 2. Put in a query that has a 'label' field and numeric values.. 1 to 5 work well. # This is released as Ego-Ware. If you like it, send a nice e-mail to the geeks here at geeklabs.com # No, this is not the final version, but it was at a nice stopping place to share it. # Have fun. --Mike-- ?>