الشركة الأفضل لبرمجة المواقع والتطبيقات و عرض أفضل سرفيرس في العالم العربي
Statistics and counters: Graph Maker FunctionGraph Maker Function
function GraphRows($rows, $opts)
{
# options
#
# box_x_size: width of values area
# box_y_size: height of the values area
# x_size: total width of the graph
# y_size: total height of the graph
# x_val: array element of record to assign as x_value
# y_val: array element to assign as y_value
# x_label: label for the Bottom of the graph
# y_label: label for the left side
# font: font number to use from system
# font_file: file to use as the font
# show_x_val: true == show X value of each bar with the y_label
# show_y_val: true == show Y value labels
# title: string for top of graph
# bar_color: color array to use for bars
# box_color: color to use for the box edging and value box.
# label_color: color for the labels
# val_color: color to to use for the values
# title_color: color to use for the title.
# bg_color: color for the background.
# bg_trans: make the background transparant.
# img_format: can be "png", "gif", or "jpg".
$defaults = array("show_x_val" => 1,
"show_y_val" => 1,
"show_y_lines" => 1,
"font" => 1,
"x_val" => NULL,
"y_val" => NULL,
"title" => NULL,
"x_label" => NULL,
"y_label" => NULL,
"x_title" => NULL,
"y_title" => NULL,
"x_slant" => NULL,
"font_file" => NULL,
"x_size" => NULL,
"y_size" => NULL,
"box_x_size" => 300,
"box_y_size" => 200,
"right_margin" => 2,
"fg_color" => array(1,1,1),
"bg_color" => array(232, 226, 202),
"bg_trans" => NULL,
"bar_color" => array(128, 128, 128),
"box_color" => NULL,
"val_color" => NULL,
"title_color" => NULL,
"return" => NULL,
"img_format" => "png");
foreach($defaults as $opt => $val)
{
if(!array_Key_exists($opt, $opts))
{
$opts["$opt"] = $val;
}
}
if($opts["font_file"])
{
$opts["font"] =imageloadfont($opts["font_file"]);
}
$font_y = imagefontheight($opts["font"]);
$font_x = imagefontwidth($opts["font"]);
#the boxed area with be 2 from the top and right, 30 from the left, and 12 from the bottom.
# the border will be 1px wide.
$max_x = 0;
$max_y = 0;
$longest_x = 0;
$longest_y = 0;
$x_mod = 1.3;
$y_mod = 1.3;
# find the longest x and y value str lengths for the label margin size.
foreach($rows as $idx => $row)
{
$y_val = $row;
$x_val = $idx;
if(is_array($row))
{
foreach($row as $idx => $val)
{
$y_val = $val;
break;
}
if($opts["y_val"])
{
if(array_key_exists($opts["y_val"], $row))
{
$y_val = $row[$opts["y_val"]];
}
}
if($opts["x_val"])
{
if(array_key_exists($opts["x_val"], $row))
{
$x_val = $row[$opts["x_val"]];
}
}
}
if($y_val > $max_y)
{
$max_y = $y_val;
}
if($x_val > $max_x)
{
$max_x = $x_val;
}
$y_len = strlen($y_val);
$x_len = strlen($x_val);
if($y_len > $longest_y)
{
$longest_y = $y_len;
}
if($x_len > $longest_x)
{
$longest_x = $x_len;
}
}
$left_margin = 0;
$bottom_margin = 0;
$top_margin = $font_y * ($y_mod/2);
if($opts["title"] != NULL)
{
$top_margin = $font_y * $y_mod + $font_y * ($y_mod/2);
}
if($opts["show_x_val"])
{
$bottom_margin += $font_y * $y_mod;
if($opts["x_slant"])
{
$bottom_margin += $font_x * $longest_x - $font_y * $y_mod;
}
}
if($opts["show_y_in_x"])
{
$bottom_margin += $font_y * $y_mod;
}
if($opts["x_title"])
{
$bottom_margin += $font_y * $y_mod;
}
if($opts["show_y_val"])
{
$left_margin += $font_x * $x_mod * $longest_y;
}
if($opts["y_title"])
{
$t_margin = $font_y * $y_mod + $font_y * ($y_mod/2);
$l_margin = $font_x * $x_mod * strlen($opts["y_title"]);
if($t_margin > $top_margin)
{
$top_margin = $t_margin;
}
if($l_margin > $left_margin)
{
$left_margin = $l_margin;
}
}
# we have to calculate the x and y image and box sizes depending on which is present.
# img sizing overrides the box size;
if($opts["x_size"])
{
$opts["box_x_size"] = $img_x - $left_margin - 2 - $opts["right_margin"] ;
}
else
{
$opts["x_size"] = $left_margin + $opts["box_x_size"] + 2 + $opts["right_margin"];
}
if($opts["y_size"])
{
$opts["box_y_size"] = $img_y - $top_margin - 2 - $bottom_margin;
}
else
{
$opts["y_size"] = $top_margin + 2 + $opts["box_y_size"] + $bottom_margin;
}
$box_top = $top_margin+1;
$box_bottom = $box_top + 2 + $opts["box_y_size"];
$box_left = $left_margin+1;
$box_right = $box_left + $opts["box_x_size"];
$img = imagecreate($opts["x_size"]+1, $opts["y_size"]+1);
list($r, $g, $b) = $opts["bg_color"];
$bg_color = imagecolorallocate($img, $r, $g, $b);
if($opts["bg_trans"])
{
imagecolortransparent($img, $bg_color);
}
list($r, $g, $b) = $opts["fg_color"];
$fg_color = imagecolorallocate($img, $r, $g, $b);
if(is_array($opts["val_color"]))
{
list($r, $g, $b) = $opts["val_color"];
}
$box_color = $fg_color;
if(is_array($opts["box_color"]))
{
list($r, $g, $b) = $opts["box_color"];
$box_color = imagecolorallocate($img, $r, $g, $b);
}
$bar_color = $fg_color;
if(is_array($opts["bar_color"]))
{
list($r, $g, $b) = $opts["bar_color"];
$bar_color = imagecolorallocate($img, $r, $g, $b);
}
$title_color = $fg_color;
if(is_array($opts["title_color"]))
{
list($r, $g, $b) = $opts["title_color"];
$title_color = imagecolorallocate($img, $r, $g, $b);
}
$val_color = $fg_color;
if(is_array($opts["val_color"]))
{
list($r, $g, $b) = $opts["val_color"];
$val_color = imagecolorallocate($img, $r, $g, $b);
}
imagerectangle($img, 0, 0, $opts["x_size"], $opts["y_size"], $box_color);
imagerectangle($img, $box_left, $box_top, $box_right, $box_bottom, $box_color);
$num_records = count($rows);
# lets find the next item up.
# find the maximal marker.
$finder = $max_y;
$multiple = 1;
while($finder > 10)
{
$finder = $finder/10;
$multiple *= 10;
}
$oldfinder =$finder;
$finder = round($finder);
if($finder < $oldfinder)
{
$finder++;
}
# ok, so we have UP to 10 marks.
$num_marks = $finder;
$max_y = $finder * $multiple;
$xspan = ($opts["box_x_size"] - $num_records -1)/$num_records;
$yspan = $opts["box_y_size"]/$num_marks;
for($x=$num_marks;$x>=0;$x--)
{
if($opts["show_y_lines"])
{
imageline($img,
$box_left, ($x*$yspan)+$top_margin+1,
$box_right, ($x*$yspan)+$top_margin+1,
$box_color);
}
if($opts["show_y_val"])
{
$text = ($num_marks - $x)*$multiple;
imagestring($img, $opts["font"],
$left_margin - strlen($text)*$x_mod*$font_x,
($x*$yspan)+$top_margin-($font_y/2)+2,
$text, $box_color);
}
}
if($opts["title"])
{
$txtlen = strlen($opts["title"]) * $font_x;
$left = $opts["box_x_size"]/2 - $txtlen/2 + $left_margin +1;
$top = $font_y * $y_mod / 3;
imagestring($img, $opts["font"], $left, $top, $opts["title"], $title_color);
}
if($opts["y_title"])
{
$txtlen = strlen($opts["y_title"]) * $font_x;
$left = $left_margin / 2 - $txtlen / 2;
$top = $font_y * $y_mod / 3;
imagestring($img, $opts["font"], $left, $top, $opts["y_title"], $title_color);
}
if($opts["x_title"])
{
$left = $left_margin;
$top = $opts["box_y_size"] + $top_margin + $font_y * $y_mod / 3;
if($opts["show_x_val"])
{
$top += $font_y * $y_mod;
}
if($opts["show_y_in_x"])
{
$top += $font_y * $y_mod;
}
imagestring($img, $opts["font"], $left, $top, $opts["x_title"], $title_color);
}
$x_pos = 0;
foreach($rows as $idx => $row)
{
$y_value = $row;
$x_value = $idx;
if(is_array($row))
{
if($opts["y_val"] && array_key_exists($opts["y_val"], $row))
{
$y_value = $row[$opts["y_val"]];
}
else
{
foreach($row as $var => $val)
{
$y_value = $val;
break;
}
}
}
if($opts["x_val"])
{
$x_value = $row[$opts["x_val"]];
}
if(!is_numeric($x_value))
{
$x_value = $x_pos;
}
$top = $box_bottom - $y_value * $yspan / $multiple;
$left = $box_left + $xspan * $x_value + 1 + $x_value;
$right = $left + $xspan - 1;
imagefilledrectangle($img, $left, $top, $right, $box_bottom, $bar_color);
$y_txt = $x_pos + 1;
if($opts["x_val"])
{
if(array_key_exists($opts["x_val"], $row))
{
$y_txt = $row[$opts["x_val"]];
}
}
$top = $box_bottom + $font_y * $y_mod / 5;
$txt_left = $left + $xspan / 2 - strlen($y_txt) * $font_x / 2 + 2;
imagestring($img, $opts["font"], $txt_left, $top, $y_txt, $val_color);
if($opts["show_y_in_x"])
{
$top += $font_y * $y_mod;
$txt_left = $left + $xspan / 2 - strlen($y_value) * $font_x / 2 + 2;
imagestring($img, $opts["font"], $txt_left, $top, $y_value, $val_color);
}
$x_pos++;
}
if($opts["return"])
{
return($img);
}
switch(strtolower($opts["img_format"]))
{
case "png":
Header("Content-Type: image/png");
ImagePNG($img);
break;
case "jpg":
case "jpeg":
Header("Content-Type: image/jpeg");
ImageJPEG($img);
break;
case "gif":
Header("Content-Type: image/gif");
ImageGIF($img);
break;
}
ImageDestroy($img);
return;
}
|