Takes an array or data, an array of options and produces a graph image of the data array. Has options for colors, size of image, transparancy, titles (x, y, graph). Could use some improvements though.
By : ahzzmandius
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;
}
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Counters Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Learn field-tested SOA principles, methodology, technology and implementation from the global SOA market leader - in a new e-book by an IBM SOA expert. Written by IBM Certified SOA Solution Designer Bobby Woolf, "Exploring IBM SOA Technology & Practice" is the ultimate insider's guide to SOA - a PDF e-book packed cover to cover with IBM's specific advice on how to make your SOA implementation a success. FREE! Go There Now!
|
|
|
|
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download the latest trial version of IBM Data Studio V1.1 at no cost. IBM Data Studio is a comprehensive data management solution that helps you effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life cycle utilizing a consistent and integrated user interface. Unlike other client-side data management solutions that focus on only one aspect of the application lifecycle or database administration, Data Studio complements the Rational Software Delivery platform, providing unparalleled flexibility for a heterogeneous data server environment across platforms. FREE! Go There Now!
|
|
|
|
Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code. FREE! Go There Now!
|
|
|
|
As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security. FREE! Go There Now!
|
|
|
|
Whether you are creating new applications or modifying existing ones, managing integration of new components with traditional z/OS elements is a critical part of building and deploying modern applications. Listen to this webcast to see how IBM can help you optimize your development process using an IDE like Rational Developer for System z that integrates with management tools, such as ClearCase to manage your application development on mainframes. FREE! Go There Now!
|
|
|
|
Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, where he will overview Rational’s new offerings and programs to help customers accelerate software innovation on System z. He will discuss how these solutions help organizations extend their core business processes toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
|
|
|
|
In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
|
|
|
|
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |