Re: Graphing - bar charts

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Bar graphs are very easy to use with PHP. It's just a table columns of varying height.

I wrote this a long time ago. To be honest it isn't very good code and I think I got the ideas from someone else.
Regardless it should give you an idea.
You basically pass in an array of dollar amounts and it outputs a bar graph.
You can also manually adjust the scale of the graph by varying the values $minval and $maxval.
The function "bar" makes a single bar of the graph.
Use it at your own risk though. It is pretty poor PHP but it did the trick for me!


------------------------------------------------------------------------
//graph looks best when values are between 50 and 250
function bargraph ($bararray, $xminval, $xmaxval){

function bar($height,$val, $minval, $maxval){
   $n_bars=count($bararray);
   $newmargin = $maxval - $minval;
   $newdifference = $height - $minval;
   $newchange = $newdifference / $newmargin;
   $newheight = 200 * $newchange + 25;
       echo ("<td valign=bottom>" .
      "<table bgcolor=#aa2222 height=" . $newheight .
      " cellpadding=0 cellspacing=0>");
      echo("<tr><td align=center valign=bottom>");
      echo ("<font color=#00000 size=-1 >");
      echo ("$");
      printf("%.2f", $val);
      echo ("</font>");
      echo ("</td></tr></table></td>");
   }

   $n_bars=count($bararray);
   $border=0;
   echo("<table bgcolor=#a0a0a0 height=200 width=" .
   $n_bars*$width . " border=" . $border . " >");

   if (!$xminval) $minval= min ($bararray);
   else $minval = $xminval;

   if (!$xmaxval)$maxval= max ($bararray);
   else $maxval = $xmaxval;

   for ($x = 0; $x<count($bararray); $x++){
   bar($bararray[$x], $bararray[$x], $minval, $maxval);
   }

   echo("</table>");
}
--------------------------------------------------------------------------

- Matt



John W. Holmes wrote:

From: "Craig Hoffman" <choffman@xxxxxxxxxxx>



I am looking for an open source and simple PHP script that will graph
(bar) a few MySQL fields. Does anyone have any recommendations?



The easiest way is to just have an image that you dynamically vary with width of

<img src="dot.jpg" height="10" width="$width">

Or take a look at JPGraph, which offers a lot of features.

---John Holmes...




[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux