Counters Code
  Home arrow Counters Code arrow Graph Maker Function
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
COUNTERS CODE

Graph Maker Function
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 7
    2004-01-07

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Download IBM Data Studio V1.1

    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!


    NEW! Download IBM Rational Developer for System z

    Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects.
    FREE! Go There Now!


    NEW! Evaluate Rational Host Access Transformation Services (HATS) Toolkit V7.1

    Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications.
    FREE! Go There Now!


    NEW! IBM Rational AppScan Standard Edition V7.7

    Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7.
    FREE! Go There Now!


    NEW! Rational Talks to You: Manage RUP-based CMMI initiatives

    Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered!
    FREE! Go There Now!


    NEW! Rational Testing eKits

    Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing.
    FREE! Go There Now!


    NEW! Run your first CICS application on a PC using TXSeries for Windows

    Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1.
    FREE! Go There Now!


    NEW! Test terminal-based applications with Rational Functional Tester

    Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily.
    FREE! Go There Now!


    NEW! Understanding Web application security challenges

    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!


    NEW! Webcast: Striking the right balance between manual and automated testing

    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!

    COUNTERS CODE ARTICLES

    - TG Who's Online
    - Text Based Counter that formats output
    - Counter & visitor statistics
    - Server Uptime Statistics
    - Chris Dingman's Hit Tracker Script
    - Graph Maker Function
    - Simple userOnline class
    - Adv. Log file generator
    - Logwriter
    - time_left()
    - Basic Statistics
    - Easy Counter
    - countCodeLines
    - MySQL Counter
    - bandwidthmeter





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek