A Dynamic Bar Chart : Bar Chart « Chart « PHP

Home
PHP
1.Chart
2.Class
3.Components
4.Cookie Session
5.Data Structure
6.Data Type
7.Date
8.Design Patterns
9.Development
10.DNS
11.Email
12.File Directory
13.Form
14.Functions
15.Graphics Image
16.HTML
17.Language Basics
18.Login Authentication
19.Math
20.MySQL Database
21.Network
22.Operator
23.PDF
24.Reflection
25.Statement
26.String
27.Utility Function
28.Web Services SOAP WSDL
29.XML
PHP » Chart » Bar Chart 
A Dynamic Bar Chart

<?php
header("Content-type: image/gif");
$cells = array liked=>200, hated=>400, indifferent=>900 );
$max = max$cells );
$total = count $cells );
$totalwidth = 300;
$totalheight = 200;
$xgutter = 20// left/right margin
$ygutter = 20// top/bottom margin
$internalgap = 10// space between cells
$bottomspace = 30// gap at the bottom (in addition to margin)
$font = "ARIALBD.TTF";
 $graphCanX = $totalwidth - $xgutter*);
$graphCanY = $totalheight - $ygutter*- $bottomspace );// starting draw position x - axis
$posX = $xgutter; // starting draw pos - y -  axis
$posY = $totalheight - $ygutter - $bottomspace;
$cellwidth = (int) (( $graphCanX - $internalgap * $total-) )) / $total;
 $textsize = (int)($bottomspace);
// adjust font size
while list$key, $val = each $cells ) )
    {
     while )
        {
        $box = ImageTTFbBox$textsize, 0, $font, $key );
        $textWidth = abs$box[2] );
        if $textWidth < $cellwidth )
            break;
        $textsize--;
        }
    }
$image = imagecreate$totalwidth, $totalheight );
$red = ImageColorAllocate($image, 25500);
$blue = ImageColorAllocate($image, 00255 );
$black = ImageColorAllocate($image, 00);
$grey = ImageColorAllocate($image, 100100100 );
 reset ($cells);
 while list$key, $val = each $cells ) )
     {
     $cellheight = (int) (($val/$max* $graphCanY);
     $center = (int)($posX+($cellwidth/2));
    imagefilledrectangle$image, $posX, ($posY-$cellheight)($posX+$cellwidth), $posY, $blue );
     $box = ImageTTFbBox$textsize, 0, $font, $key );
     $tw = $box[2];
     ImageTTFText(    $image, $textsize, 0($center-($tw/2)),
             ($totalheight-$ygutter), $black, $font, $key );
     $posX += $cellwidth + $internalgap);
     }
 imagegif$image );
 ?>
<!--/*
Code Revised from 



Sam's Teach Yourself Php, Mysql and Apache in 24 Hours 
(Sams Teach Yourself S.) (Paperback)
by Julie C. Meloni 

# Paperback: 616 pages
# Publisher: Sams (1 Jan 2004)
# Language English
# ISBN: 0672326205

-->


           
       
Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.