Share Pastebin
Guest
Public paste!

Cryzabey

By: a guest | Sep 1st, 2009 | Syntax: None | Size: 9.63 KB | Hits: 61 | Expires: Never
Copy text to clipboard
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Bar Graph</title>
  6. </head>
  7.  
  8. <body>
  9. <?php
  10. // Creating Globals
  11. $barLengths = array(); // Array that holds barLengths
  12. $graphMaxPx; // Pixel length of the x-axis
  13. $totalTicks = 0; // Number of tick marks
  14.  
  15. /* Main()
  16. This is the main function. While it may utilize smaller functions in order to complete
  17. it's main objective, it is ultimately the ring master of this show [:
  18. */
  19.  
  20. function getBargraph($bars, $tickIncrement, $tickLength, $bar_thickness, $title, $separator, $currencySymbol, $titleFont, $xAxisFont, $addTicks)
  21. {
  22. // Pulling Globals
  23. global $barData;
  24.  
  25. // Setting Defaults
  26. $barsDefault = 5;
  27. $separatorDefault = ",";
  28. $currencySymbolDefalut = "$";
  29.        
  30.  
  31. /*
  32. I think we'll start by making sure the right data-types were declared.
  33. If they weren't we'll either kick out a default or an error
  34. */
  35.  
  36. // The Number of Bars
  37.         // Make sure it's a number.
  38.         if(gettype($bars) != "integer"){
  39.                 echo "Error: The Number of Bars must be an integer.<br />";
  40.         // Verify that it's a reasonably small number.
  41.         }
  42.         if($bars > 5){
  43.                 $bars = $barsDefault;
  44.         // Make sure the number is positive.
  45.         }else if ($bars <= 0){
  46.                 echo "Error: It's rather silly to have 0 or less bars in a bar graph.<br />";
  47.         }
  48. // Tick Increment
  49.         if(gettype($tickIncrement) != "integer"){
  50.                 echo "Error: The Tick Increment must be an integer (0 being auto-determine)<br />";
  51.         }
  52. // Tick Length (Distance Between Ticks)
  53.         if(gettype($tickLength) != "integer"){
  54.                 echo "Error: The Tick Length must be an integer.<br />";
  55.         }
  56. // Bar Thickness
  57.         if(gettype($bar_thickness) != "integer"){
  58.                 echo "Error: The Bar Thickness must be an integer.<br />";
  59.         }
  60. // The Title
  61.         if(gettype($title) != "string"){
  62.                 echo "Error: The Title must be a string.<br />";
  63.         }
  64. // The [Currency] Seperator
  65.         // Changing null to a blank string
  66.         if($seperator == "Null" || $seperator == "null" || $seperator == "NULL"){
  67.                 $seperator = "";
  68.         // Error handling for $separator.
  69.         }else if(gettype($separator) != "string"){
  70.                 if($separator != "," || $separator != "." || $separator != ""){
  71.                         $separator = $separatorDefault;
  72.                 }
  73.         }
  74. // Currency Symbol
  75.         if(gettype($currencySymbol) != "string"){
  76.                 echo "Epic Fail!<br />";
  77.         }
  78.  
  79.  
  80. /*
  81. Assesing ratios:
  82. this part of the script determines the sizes of the various bars in the graph.
  83. */
  84.  
  85.         $values = array(1 => $barData["value"][1], 2 => $barData["value"][2], 3 => $barData["value"][3], 4 => $barData["value"][4], 5 => $barData["value"][5]);
  86.        
  87.         assess_ratios($values, $bars, $tickIncrement, $tickLength, $addTicks);
  88.        
  89.  
  90. /*
  91. Ahhh! The best part of the script. The end [; Here we're going to wrap things up by importing some global variables that were initialized in the last function and start piecing together the output using the data that's been collected, modified, and made usefull earlier in the script.
  92. */
  93.  
  94. // Pulling Some Globals
  95.         global $barLengths; // Array that holds barLengths
  96.         global $graphMaxPx; // Pixel length of the x-axis
  97.         global $totalTicks; // Number of tick marks
  98.  
  99. // Some minor graphic related tweaks
  100.         $tickLength -= 6; // To cover for the images width
  101.        
  102. // Kicking out XML
  103.         // Graph Title
  104.         echo "<!--- The Graph's Title --->\n";
  105.         echo "<font style='" . $titleFont . "'>" . $title . "</font>\n";
  106.         echo "<div style='background: url(images/spacer.gif); background-repeat: repeat; height: 10px; max-width: 100px;'></div>\n";
  107.         // Individual Bars
  108.         for ($counter = 1; $counter < ($bars + 1); $counter += 1)
  109.         {
  110.                 bar_create($barLengths, $counter, $bar_thickness, $currencySymbol, $separator);
  111.         }
  112.         // X-axis Bar
  113.         echo "<!--- X-Axis Display Code --->\n";
  114.         echo "<div style='background: url(images/bar.jpg) repeat-x; width: " . $graphMaxPx . "px;' />\n";
  115.         // Tick Marks
  116.         echo "<img style='margin-bottom: 4px;' src='images/bar2.jpg' />\n";
  117.         for($counter = 0; $counter < ($totalTicks); $counter += 1)
  118.         {
  119.                 echo "<img style='margin-bottom: 4px; margin-left: " . $tickLength . "px;' src='images/bar2.jpg' />\n";
  120.         }
  121.         // Increments
  122.         echo "<div style='" . $xAxisFont . " width: " . ($graphMaxPx + 50) . "'>
  123.         <div style='display: inline; float: left; padding-bottom: 5px;'>" . $currencySymbol . "0</div>
  124.         <div style='display: inline; float: left; padding-bottom: 5px; margin-left: " . ($tickLength - 18) . "px;'>" . $currencySymbol . "" . number_format($tickIncrement, 0, "", $separator) . "</div>\n";
  125.         for($counter = 2; $counter < ($totalTicks + 1); $counter ++)
  126.         {
  127.                 echo "<div style='display: inline; float: left; padding-bottom: 5px; margin-left: " . ($tickLength - 20) . "px;'>" . $currencySymbol . "" . number_format(($tickIncrement*$counter), 0, "", $separator) . "</div>\n";
  128.         }
  129.         echo "</div>";
  130. }
  131.  
  132.  
  133. /*
  134.  
  135. */
  136.  
  137. function assess_ratios($values, $bars, $tickIncrement, $tickLength, $addTicks)
  138. {
  139.  
  140.         // Initializing Variables
  141.         $biggestValueIndex = 0; // The array position of the largest bar
  142.         $biggestValue = 0; // The value of the largest bar
  143.        
  144.         // Checking for the highest salary
  145.         for ($counter = 1; $counter < ($bars + 1); $counter += 1)
  146.         {
  147.                 if($values[$counter] > $values[$counter + 1]){
  148.                         if($biggestValueIndex > 0){
  149.                                 if($values[$counter] > $values[$biggestValueIndex]){
  150.                                         $biggestValueIndex = $counter;
  151.                                         $biggestValue = $values[$counter];
  152.                                 }
  153.                         }else{
  154.                                 $biggestValueIndex = $counter;
  155.                                 $biggestValue = $values[$counter];
  156.                         }
  157.                 }
  158.         }
  159.        
  160.        
  161.         // Initializing Variables
  162.         global $graphMaxPx; // Pixel Length x-axis
  163.         global $totalTicks; // Number of tick marks
  164.         $whileCheck = "on"; // Handler for loop
  165.         $graphMax; // Highest value of Graph
  166.         $barMaxPx; // Longest Bars Length in pixels
  167.        
  168.         // Determining Number of ticks needed
  169.         while ($whileCheck == "on" ) {
  170.                 $graphMax = $totalTicks*$tickIncrement;
  171.                 if($graphMax < $values[$biggestValueIndex]){
  172.                         $totalTicks += 1;
  173.                 }else{
  174.                         $graphMax = $totalTicks*$tickIncrement;
  175.                         $barMaxPx = ($biggestValue/$graphMax)*(($totalTicks)*$tickLength);
  176.                         $totalTicks += $addTicks;
  177.                         $graphMaxPx = ($totalTicks*$tickLength) + 2;
  178.                         $whileCheck = "off";
  179.                 }
  180.         }
  181.        
  182.         //Initializing Variables
  183.         global $barLengths; // Array that holds barLengths
  184.        
  185.         // Assigning Pixel Lengths for Bars
  186.         for ($counter = 1; $counter < ($bars + 1); $counter +=1)
  187.         {
  188.                 $barLengths[$counter] = (($values[$counter] / $graphMax)*$barMaxPx);
  189.         }
  190. }
  191.  
  192.  
  193. /* Individual bar creation
  194. This function works to create a single bar of the graph.
  195. */
  196.  
  197. function bar_create($barLengths, $currentBar, $bar_thickness, $currencySymbol, $separator)
  198. {
  199.  
  200. global $barData;
  201.  
  202. // Current Bar Comment
  203. echo "<!--- Bar " . $currentBar . "'s Display Code --->\n";
  204. // Text Style and Bar Background
  205. echo "<div style='display:block; height: " . $bar_thickness . "px;'>
  206.         <div style='
  207.         background-" . $barData["color"][$currentBar] . ";
  208.         background-repeat: repeat;
  209.         height: " . $bar_thickness . "px;
  210.         width: " . $barLengths[$currentBar] . "px;
  211.         float: left;
  212.         line-height: " . $bar_thickness . "px;'>
  213.         <div style='padding-left: 15px;
  214.         " . $barData["font"][$currentBar] . "'>\n\t" .
  215.                 "" . $barData["label"][$currentBar] . "
  216.         </div>
  217.         </div>
  218.         <div style='padding-top: 9px;'>
  219.                 &nbsp;&nbsp;&nbsp; " . $currencySymbol . "" . number_format($barData["value"][$currentBar], 0, "", $separator) . "
  220.         </div>
  221. </div>\n";
  222. // Spacer
  223. echo "<img src='images/spacer.gif' height='2px' />";
  224. echo "<div style='background: url(images/spacer.gif); background-repeat: repeat; height: 2px; width: " . $barLengths[$currentBar] . "px;'></div>\n";
  225. }
  226. ?>
  227.  
  228.  
  229. <?php
  230.  
  231. /* Creating Bar Arrays
  232. Info for the various bars being used.
  233. Simply leave a field as 0, Null, or delete it altogether if you don't intend to use that many bars.
  234. */
  235.  
  236. $barData = array ( "value" => array (  1 => 100000,
  237.                                        2 => 78000,
  238.                                        3 => 50000,
  239.                                                                            4 => 25000,
  240.                                                                            5 => 20000
  241.                                      ),
  242.                   "label"  => array (  1 => "Job One",
  243.                                        2 => "Job Two",
  244.                                        3 => "Job Three",
  245.                                        4 => "Job Four",
  246.                                        5 => "Job Five"
  247.                                      ),
  248.                                   "color"  => array (  1 => "image: url(images/r33.gif)",
  249.                                                                            2 => "image: url(images/g33.gif)",
  250.                                                                            3 => "image: url(images/b33.gif)",
  251.                                                                            4 => "image: url(images/o33.gif)",
  252.                                                                            5 => "color: #9900ff"
  253.                                                                          ),
  254.                   "font"   => array (  1 => "color: #000000; font-size: 12px; font-style: normal; font-weight: bold;",
  255.                                                                        2 => "color: #000000; font-size: 12px; font-style: normal; font-weight: bold;",
  256.                                                                            3 => "color: #000000; font-size: 12px; font-style: normal; font-weight: bold;",
  257.                                                                            4 => "color: #000000; font-size: 12px; font-style: normal; font-weight: bold;",
  258.                                                                            5 => "color: #000000; font-size: 12px; font-style: normal; font-weight: bold;"
  259.                                      )
  260.                 );
  261.  
  262. /*  Parameters asked for in the getBargraph(); Function
  263.  
  264.         # of bars -- X-Axis Increments ($) -- Pixels Between Ticks
  265.         Thickness of the Bars -- Title of Graph -- Seperator (. , or Null)
  266.         Currency Symbol (Text or Null) -- Title -- X-Axis Font
  267.         Additional Tick Marks (#)
  268. */
  269. ?>
  270.  
  271.  
  272. <?php
  273. getBargraph(5, 20000, 100, 33, "The Title [:", ",", "$", "color: #000000; font-size: 22px; font-style: normal; font-weight: bold; margin-left: 25px;", "color: #000000; font-size: 8px; font-style: normal; font-weight: normal;", 2);
  274. ?>
  275.  
  276. </body>
  277. </html>