Barchart Function in PHP

A PHP function to output simple HTML/CSS bar charts.

About This Website

SyntaxSeed is a collection of free and Open Source applications, themes, plugins and PHP scripts available for your next project.

This site is owned and operated by Avinus Web Services: www.avinus.com. Contact us for your next project or web hosting needs.


This function creates a simple HTML/CSS bar chart from 2 arrays of data.
Source Code: + Expand <?php
// Usage:
makeBarChart(Array("J""F""M""A""M"), Array(500011200145801300017400), 200000"DOLLARS"); 
?>

<?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 && !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);
    }
}
        
?>