This class creates a PNG image bar graph
for inclusion in HTML pages. The graph can display one or more data sets in user defined colors.
The font size, font color background color, spacing between sets of bars, labels, and tick marks are all set by the user. You can also design this bar chart by adding some css styles as per your website colour combinations.
Below is the PHP class to generate bar chart using php and mysql. Create a simpleBar.class.php
page and add below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <?php // SimpleBar Class // Use Under GNU License Agreement class SimpleBar { private $xgutter = 20; // left/right margin private $ygutter = 20; // top/bottom margin private $bottomspace = 30; // gap at the bottom private $internalgap = 20; // space between bars private $cells = array(); // labels/amounts for bar chart private $totalwidth; // width of the image private $totalheight; // height of the image private $font; // the font to use function __construct( $width, $height, $font ) { $this->totalwidth = $width; $this->totalheight = $height; $this->font = $font; } function addBar( $label, $amount ) { $this->cells[ $label ] = $amount; } private function _getTextSize( $cellwidth ) { $textsize = (int)($this->bottomspace); if ( $cellwidth < 10 ) { $cellwidth = 10; } foreach ( $this->cells as $key=>$val ) { while ( true ) { $box = ImageTTFbBox( $textsize, 0, $this->font, $key ); $textwidth = abs( $box[2] ); if ( $textwidth < $cellwidth ) { break; } $textsize--; } } return $textsize; } function draw() { $image = imagecreate( $this->totalwidth, $this->totalheight ); $red = ImageColorAllocate($image, 230, 230, 230); $blue = ImageColorAllocate($image, 200, 100, 100 ); $black = ImageColorAllocate($image, 0, 100, 255 ); $max = max( $this->cells ); $total = count( $this->cells ); $graphCanX = ( $this->totalwidth - $this->xgutter*2 ); $graphCanY = ( $this->totalheight - $this->ygutter*2 - $this->bottomspace ); $posX = $this->xgutter; $posY = $this->totalheight - $this->ygutter - $this->bottomspace; $cellwidth = (int)(( $graphCanX - ( $this->internalgap * ( $total-1 ) )) / $total) ; $textsize = $this->_getTextSize( $cellwidth ); foreach ( $this->cells as $key=>$val ) { $cellheight = (int)(($val/$max) * $graphCanY); $center = (int)($posX+($cellwidth/2)); imagefilledrectangle( $image, $posX, ($posY-$cellheight), ($posX+$cellwidth), $posY, $blue ); $box = imageTTFbBox( $textsize, 0, $this->font, $key ); $tw = $box[2]; ImageTTFText( $image, $textsize, 0, ($center-($tw/2)), ($this->totalheight-$this->ygutter), $black, $this->font, $key ); $posX += ( $cellwidth + $this->internalgap); } imagepng( $image ); } } ?> |
Create simple-bar-chart.php page and copy paste below code to generate bar chart from mysql data. It will add your mysql database table’s data and generate bar chart.
1 2 3 4 5 6 7 8 9 10 11 12 | <?php header("Content-type: image/png"); include("SimpleBar.class.php"); $graph = new SimpleBar( 500, 300, "verdana.ttf" ); $graph->addBar( "USA", 200 ); $graph->addBar( "India", 400 ); $graph->addBar( "UK", 240 ); $graph->addBar( "Australia", 170 ); $graph->addBar( "UAE", 270 ); $graph->draw(); ?> |
Do you like & share this article with your friends, and follow us on Facebook and Twitter for more cool WordPress tutorials.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: dynamic bar chart, dynamic chart, dynamic charts, generate bar chart from mysql, how to create bar chart in mysql, how to generate bar chart using php and mysql, HTML 5, Jquery, mysql dynamic bar chart, PHP