Simple Bar Chart Function

Single Function To Create HTML/CSS Charts

This function creates a simple HTML/CSS bar chart from 2 arrays of data.



J


F


M


A


M


 

$20,000
$0
 

Usage:

<?php
makeBarChart(Array("J", "F", "M", "A", "M"), Array(5000, 11200, 14580, 13000, 17400), 20000, 0, "DOLLARS"); 
?>

Code:

<?php
/* Function makeBarChart
* $xAxis - Array of string labels for the bars that appear along the bottom axis.
* $yAxis - Array of numeric values for the height of the bars. Treated as float values.
* $maxRange - Highest possible yAxis value. Top of the chart.
* $minRange - Lowest possible yAxis value. Bottom of the chart. Defaults to 0.
* $yFormat - Flag to apply special formatting to the yAxis values (like DOLLARS, COMMA).
* $pxHeight - Max height of the bars in pixels. yAxis values are converted to a percent of this height.
*
* Copyright (c) 2009 - Sherri Wheeler (Start.ofitall.com).
* Free for any use. Provided "as is" with no warranty.
*/
function makeBarChart($xAxis, $yAxis, $maxRange, $minRange=0, $yFormat="", $pxHeight=200){
	
	$chart = "";
	$maxRange = floatval($maxRange);
	
	if( $maxRange > 0 && !empty($xAxis) && !empty($yAxis) && count($xAxis)==count($yAxis) ){
			
		$maxRangeLabel = $maxRange;
		$minRangeLabel = $minRange;
		
		// Apply Label formatting:
		if($yFormat == "DOLLARS"){			
			$maxRangeLabel = "$".number_format($maxRangeLabel);
			$minRangeLabel = "$".number_format($minRangeLabel);
		}elseif($yFormat == "COMMA"){			
			$maxRangeLabel = number_format($maxRangeLabel);
			$minRangeLabel = number_format($minRangeLabel);			
		}
					
		for($i = 0; $i<count($xAxis); $i  ){
			if( !empty($xAxis[$i]) && !empty($yAxis[$i]) ){			
			
				$amount = floatval($yAxis[$i]);
			
				if( ($amount >= 0) && ($amount < $maxRange) && ($amount > $minRange) ){
			
					$maxRange = floatval($maxRange);
					$minRange = floatval($minRange);
					$label = strval($xAxis[$i]);
					$barHover = $amount;	
					
					// Apply Label formatting:
					if($yFormat == "DOLLARS"){
						$barHover = "$".number_format($amount);
					}elseif($yFormat == "COMMA"){
						$barHover = number_format($amount);		
					}
					
					if(empty($label)){
						$label='&nbsp;';
					}			
				
					$height = round( ($amount-$minRange)/($maxRange-$minRange)*$pxHeight );
					$chart .= "<td rowspan='2' valign='bottom' align='center'><br clear='all' /><table title='".$barHover."' style='display:inline;' cellpadding='1' cellspacing='0'><tr><td height='".$height."' width='10' bgcolor='#D9ECB0' valign='bottom'></td></tr></table><br clear='all' /><span style='font-size:8px;'><b>".$label."</b></span></td>";
				}
			}
		}
		
		$chart .= "<td rowspan='2' valign='bottom' align='center'><br clear='all' /><table style='display:inline;' cellpadding='0' cellspacing='0'><tr><td height='".$pxHeight."' bgcolor='#000000' width='1' valign='bottom'></td></tr></table><br clear='all' /><span style='font-size:8px;'>&nbsp;</span></td>";
		$chart = "<table border='0' cellspacing='2'><tr>".$chart."<td valign='top'><br /><span style='font-size:10px;'><b>".$maxRangeLabel."</b></span></td></tr><tr><td valign='bottom'><span style='font-size:10px;'><b>".$minRangeLabel."</b><br />&nbsp;</span></td></tr></table>";
	
		echo($chart);
	}
}
		
?>