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! |
Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance. FREE! Go There Now!
|
|
|
|
Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle. FREE! Go There Now!
|
|
|
|
This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management. FREE! Go There Now!
|
|
|
|
You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
|
|
|
|
In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
|
|
|
|
The discipline of assembling and delivering software is maturing beyond standard developer-centric compile/test software builds. The end-to-end software development lifecycle is emerging as the new focus moves “Beyond the Build.” Join this on demand webcast to learn about methods for streamlining software delivery and key capabilities of the IBM Rational Build Forge framework for automating build and release management in environments of any size. FREE! Go There Now!
|
|
|
|
Join this webcast to learn how IBM Rational's Functional Testing solution enables you to implement automation your way, at your pace, with your existing staff. In this webcast, you’ll learn how you can eliminate redundancy of manual test scripts, reduce errors, and increase test coverage through test automation. After this presentation you will understand how IBM Rational Functional Testing solution can streamline your manual testing and make test automation easily attainable. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |