- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Bar Graph</title>
- </head>
- <body>
- <?php
- // Creating Globals
- $barLengths = array(); // Array that holds barLengths
- $graphMaxPx; // Pixel length of the x-axis
- $totalTicks = 0; // Number of tick marks
- /* Main()
- This is the main function. While it may utilize smaller functions in order to complete
- it's main objective, it is ultimately the ring master of this show [:
- */
- function getBargraph($bars, $tickIncrement, $tickLength, $bar_thickness, $title, $separator, $currencySymbol, $titleFont, $xAxisFont, $addTicks)
- {
- // Pulling Globals
- global $barData;
- // Setting Defaults
- $barsDefault = 5;
- $separatorDefault = ",";
- $currencySymbolDefalut = "$";
- /*
- I think we'll start by making sure the right data-types were declared.
- If they weren't we'll either kick out a default or an error
- */
- // The Number of Bars
- // Make sure it's a number.
- if(gettype($bars) != "integer"){
- echo "Error: The Number of Bars must be an integer.<br />";
- // Verify that it's a reasonably small number.
- }
- if($bars > 5){
- $bars = $barsDefault;
- // Make sure the number is positive.
- }else if ($bars <= 0){
- echo "Error: It's rather silly to have 0 or less bars in a bar graph.<br />";
- }
- // Tick Increment
- if(gettype($tickIncrement) != "integer"){
- echo "Error: The Tick Increment must be an integer (0 being auto-determine)<br />";
- }
- // Tick Length (Distance Between Ticks)
- if(gettype($tickLength) != "integer"){
- echo "Error: The Tick Length must be an integer.<br />";
- }
- // Bar Thickness
- if(gettype($bar_thickness) != "integer"){
- echo "Error: The Bar Thickness must be an integer.<br />";
- }
- // The Title
- if(gettype($title) != "string"){
- echo "Error: The Title must be a string.<br />";
- }
- // The [Currency] Seperator
- // Changing null to a blank string
- if($seperator == "Null" || $seperator == "null" || $seperator == "NULL"){
- $seperator = "";
- // Error handling for $separator.
- }else if(gettype($separator) != "string"){
- if($separator != "," || $separator != "." || $separator != ""){
- $separator = $separatorDefault;
- }
- }
- // Currency Symbol
- if(gettype($currencySymbol) != "string"){
- echo "Epic Fail!<br />";
- }
- /*
- Assesing ratios:
- this part of the script determines the sizes of the various bars in the graph.
- */
- $values = array(1 => $barData["value"][1], 2 => $barData["value"][2], 3 => $barData["value"][3], 4 => $barData["value"][4], 5 => $barData["value"][5]);
- assess_ratios($values, $bars, $tickIncrement, $tickLength, $addTicks);
- /*
- 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.
- */
- // Pulling Some Globals
- global $barLengths; // Array that holds barLengths
- global $graphMaxPx; // Pixel length of the x-axis
- global $totalTicks; // Number of tick marks
- // Some minor graphic related tweaks
- $tickLength -= 6; // To cover for the images width
- // Kicking out XML
- // Graph Title
- echo "<!--- The Graph's Title --->\n";
- echo "<font style='" . $titleFont . "'>" . $title . "</font>\n";
- echo "<div style='background: url(images/spacer.gif); background-repeat: repeat; height: 10px; max-width: 100px;'></div>\n";
- // Individual Bars
- for ($counter = 1; $counter < ($bars + 1); $counter += 1)
- {
- bar_create($barLengths, $counter, $bar_thickness, $currencySymbol, $separator);
- }
- // X-axis Bar
- echo "<!--- X-Axis Display Code --->\n";
- echo "<div style='background: url(images/bar.jpg) repeat-x; width: " . $graphMaxPx . "px;' />\n";
- // Tick Marks
- echo "<img style='margin-bottom: 4px;' src='images/bar2.jpg' />\n";
- for($counter = 0; $counter < ($totalTicks); $counter += 1)
- {
- echo "<img style='margin-bottom: 4px; margin-left: " . $tickLength . "px;' src='images/bar2.jpg' />\n";
- }
- // Increments
- echo "<div style='" . $xAxisFont . " width: " . ($graphMaxPx + 50) . "'>
- <div style='display: inline; float: left; padding-bottom: 5px;'>" . $currencySymbol . "0</div>
- <div style='display: inline; float: left; padding-bottom: 5px; margin-left: " . ($tickLength - 18) . "px;'>" . $currencySymbol . "" . number_format($tickIncrement, 0, "", $separator) . "</div>\n";
- for($counter = 2; $counter < ($totalTicks + 1); $counter ++)
- {
- echo "<div style='display: inline; float: left; padding-bottom: 5px; margin-left: " . ($tickLength - 20) . "px;'>" . $currencySymbol . "" . number_format(($tickIncrement*$counter), 0, "", $separator) . "</div>\n";
- }
- echo "</div>";
- }
- /*
- */
- function assess_ratios($values, $bars, $tickIncrement, $tickLength, $addTicks)
- {
- // Initializing Variables
- $biggestValueIndex = 0; // The array position of the largest bar
- $biggestValue = 0; // The value of the largest bar
- // Checking for the highest salary
- for ($counter = 1; $counter < ($bars + 1); $counter += 1)
- {
- if($values[$counter] > $values[$counter + 1]){
- if($biggestValueIndex > 0){
- if($values[$counter] > $values[$biggestValueIndex]){
- $biggestValueIndex = $counter;
- $biggestValue = $values[$counter];
- }
- }else{
- $biggestValueIndex = $counter;
- $biggestValue = $values[$counter];
- }
- }
- }
- // Initializing Variables
- global $graphMaxPx; // Pixel Length x-axis
- global $totalTicks; // Number of tick marks
- $whileCheck = "on"; // Handler for loop
- $graphMax; // Highest value of Graph
- $barMaxPx; // Longest Bars Length in pixels
- // Determining Number of ticks needed
- while ($whileCheck == "on" ) {
- $graphMax = $totalTicks*$tickIncrement;
- if($graphMax < $values[$biggestValueIndex]){
- $totalTicks += 1;
- }else{
- $graphMax = $totalTicks*$tickIncrement;
- $barMaxPx = ($biggestValue/$graphMax)*(($totalTicks)*$tickLength);
- $totalTicks += $addTicks;
- $graphMaxPx = ($totalTicks*$tickLength) + 2;
- $whileCheck = "off";
- }
- }
- //Initializing Variables
- global $barLengths; // Array that holds barLengths
- // Assigning Pixel Lengths for Bars
- for ($counter = 1; $counter < ($bars + 1); $counter +=1)
- {
- $barLengths[$counter] = (($values[$counter] / $graphMax)*$barMaxPx);
- }
- }
- /* Individual bar creation
- This function works to create a single bar of the graph.
- */
- function bar_create($barLengths, $currentBar, $bar_thickness, $currencySymbol, $separator)
- {
- global $barData;
- // Current Bar Comment
- echo "<!--- Bar " . $currentBar . "'s Display Code --->\n";
- // Text Style and Bar Background
- echo "<div style='display:block; height: " . $bar_thickness . "px;'>
- <div style='
- background-" . $barData["color"][$currentBar] . ";
- background-repeat: repeat;
- height: " . $bar_thickness . "px;
- width: " . $barLengths[$currentBar] . "px;
- float: left;
- line-height: " . $bar_thickness . "px;'>
- <div style='padding-left: 15px;
- " . $barData["font"][$currentBar] . "'>\n\t" .
- "" . $barData["label"][$currentBar] . "
- </div>
- </div>
- <div style='padding-top: 9px;'>
- " . $currencySymbol . "" . number_format($barData["value"][$currentBar], 0, "", $separator) . "
- </div>
- </div>\n";
- // Spacer
- echo "<img src='images/spacer.gif' height='2px' />";
- echo "<div style='background: url(images/spacer.gif); background-repeat: repeat; height: 2px; width: " . $barLengths[$currentBar] . "px;'></div>\n";
- }
- ?>
- <?php
- /* Creating Bar Arrays
- Info for the various bars being used.
- Simply leave a field as 0, Null, or delete it altogether if you don't intend to use that many bars.
- */
- $barData = array ( "value" => array ( 1 => 100000,
- 2 => 78000,
- 3 => 50000,
- 4 => 25000,
- 5 => 20000
- ),
- "label" => array ( 1 => "Job One",
- 2 => "Job Two",
- 3 => "Job Three",
- 4 => "Job Four",
- 5 => "Job Five"
- ),
- "color" => array ( 1 => "image: url(images/r33.gif)",
- 2 => "image: url(images/g33.gif)",
- 3 => "image: url(images/b33.gif)",
- 4 => "image: url(images/o33.gif)",
- 5 => "color: #9900ff"
- ),
- "font" => array ( 1 => "color: #000000; font-size: 12px; font-style: normal; font-weight: bold;",
- 2 => "color: #000000; font-size: 12px; font-style: normal; font-weight: bold;",
- 3 => "color: #000000; font-size: 12px; font-style: normal; font-weight: bold;",
- 4 => "color: #000000; font-size: 12px; font-style: normal; font-weight: bold;",
- 5 => "color: #000000; font-size: 12px; font-style: normal; font-weight: bold;"
- )
- );
- /* Parameters asked for in the getBargraph(); Function
- # of bars -- X-Axis Increments ($) -- Pixels Between Ticks
- Thickness of the Bars -- Title of Graph -- Seperator (. , or Null)
- Currency Symbol (Text or Null) -- Title -- X-Axis Font
- Additional Tick Marks (#)
- */
- ?>
- <?php
- 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);
- ?>
- </body>
- </html>
