PHP to draw a bar chart

Source: Internet
Author: User
Tags strlen

Let's take a look at the effect chart first:

Let's look at how PHP implements the drawing function.

<?php
/***
* @project Bar Graph Program
* @license GPL
* @package
* @file grapbar.php
* @date 2007-4-3
* @version 1.0
* @last Modified

* Define Column chart (column chart) class
*
* Note that before using, make sure that the font path exists and allow access, and if so, check the Open_basedir entry in the php.ini configuration, if this path is not available, add it, or set up a program containing
*
* Intelligent Histogram program for reports, etc.
*
***/

Define ("Default_font_path", "C:/windows/fonts/simhei.ttf");
Class Singlebar
{
Private $_x;
Private $_y;
Private $_h;
Public $_l = 50;
Private $_w = null;
Private $_srcpoints = Array ();
Private $_points = Array ();

Public function __construct ($x, $y, $h, $l =, $w = null)
{
$this->_x = $x;
$this->_y = $y;
$this->_h = $h;
$this->_l = $l;
$this->_w = $w;
$this->_srcpoints = $this->getsrcpoints ();
$this->_points = $this->getpoints ();
}

Public Function getsrcpoints ()
{
Return Array (
Array ($this->_x, $this->_y),
Array ($this->_x+ $this->_l, $this->_y),
Array ($this->_x+ (1.35* $this->_l), $this->_y-(0.35* $this->_l)),
Array ($this->_x+ (0.35* $this->_l), $this->_y-(0.35* $this->_l)),
Array ($this->_x, $this->_y+ $this->_h),
Array ($this->_x+ $this->_l, $this->_y+ $this->_h),
Array ($this->_x+ (1.35* $this->_l), $this->_y+ $this->_h-(0.35* $this->_l))
);
}

Public Function getpoints ()
{
$points = Array ();
foreach ($this->_srcpoints as $key => $val)
{
$points [] = $val [0];
$points [] = $val [1];
}
return $points;
}

Public Function gettoppoints ()
{
Return Array_slice ($this->_points, 0, 8); Top coordinates
}

Public Function getbelowpoints ()
{
Return Array_merge (Array_slice ($this->_points, 0, 2), Array_slice ($this->_points, 8, 4), Array_slice ($this-> _points, 2, 2)); Lower coordinates
}

Public Function getrightsidepoints ()
{
Return Array_merge (Array_slice ($this->_points, 2, 2), Array_slice ($this->_points, 4), Array_slice ($this- >_points, 4, 2)); Right coordinate
}

Public function Draw ($image, $topColor, $belowColor, $sideColor, $borderColor = null, $type = ' left ')
{
if (Is_null ($borderColor))
{
$borderColor = 0XCCCCCC;
}

$top _rgb = $this->getrgb ($topColor);
$below _rgb = $this->getrgb ($belowColor);
$side _rgb = $this->getrgb ($sideColor);
$top _color = imagecolorallocate ($image, $top _rgb[' R '], $top _rgb[' G '], $top _rgb[' B ']);
$below _color = imagecolorallocate ($image, $below _rgb[' R '], $below _rgb[' G '], $below _rgb[' B ']);
$side _color = imagecolorallocate ($image, $side _rgb[' R '], $side _rgb[' G '], $side _rgb[' B ']);

Imagefilledpolygon ($image, $this->gettoppoints (), 4, $top _color); Draw the top side
Imagepolygon ($image, $this->gettoppoints (), 4, $borderColor); Draw the top side edge

Imagefilledpolygon ($image, $this->getbelowpoints (), 4, $below _color); Picture below
Imagepolygon ($image, $this->getbelowpoints (), 4, $borderColor); Draw the line below

if ($type = = ' Left ')
{
Imagefilledpolygon ($image, $this->getrightsidepoints (), 4, $side _color); Draw right Side
Imagepolygon ($image, $this->getrightsidepoints (), 4, $borderColor); Draw side edges
}
}

Public Function getRGB ($color)
{
$ar = Array ();
$color = Hexdec ($color);
$ar [' R '] = ($color >>16) & 0xFF;
$ar [' G '] = ($color >>8) & 0xFF;
$ar [' B '] = ($color) & 0xFF;
return $ar;
}
}

Class Bar
{
Private $_w;
Private $_h;
Private $_bgcolor = "FFFFFF";
Private $_barheights = Array ();
Private $_bartexts = Array ();
Private $_barcolors = Array ();
Public $_title;
Public $_paddingtop = 30;
Public $_paddingbottom = 100;
Public $_paddingleft = 45;
Public $_paddingright = 2;
Public $_barl = 50;
Public $image;

Public function __construct ($imgW, $imgH, $barHeights, $barTexts = null, $barColors = NULL)
{
$this->_w = $imgW;
$this->_h = $imgH;
$this->_barheights = $barHeights;
$this->_bartexts = $barTexts;
$this->_barcolors = $barColors;
$this->_paddingbottom = $this->resetpaddingbottom ();
$this->_h = $this->resetheight ();
$this->image = Imagecreatetruecolor ($this->_w, $this->_h);
}

Public Function Stroke ()
{
$this-&GT;DRAWBG ();
$this->drawbars ();
$this->drawtitle ();
$this->drawlables ();
Ob_start ();
Header ("Content-type:image/png");
Imagepng ($this->image);
Header ("Content-type:"). Image_type_to_mime_type (Imagetype_jpeg));
Imagejpeg ($this->image);
Imagedestroy ($this->image);
}

Public Function DRAWBG ()
{
$img _w = $this->_w;
$img _h = $this->_h;
$paddingTop = $this->_paddingtop;
$paddingBottom = $this->_paddingbottom;
$paddingLeft = $this->_paddingleft;
$paddingRight = $this->_paddingright;
$rgb = $this->getrgb ($this->_bgcolor);
$BG = Imagecolorallocate ($this->image, $rgb [' R '], $rgb [' G '], $rgb [' B ']);
Imagefilledrectangle ($this->image, 0, 0, $img _w, $img _h, $BG);
$side _BG = Imagecolorallocatealpha ($this->image, 220, 220, 220, 75);
$side _bg2 = imagecolorallocate ($this->image, 220, 220, 220);
$border _color = imagecolorallocate ($this->image, 190, 190, 190);
$line _color = imagecolorallocate ($this->image, 236, 236, 236);
$dial _color = imagecolorallocate ($this->image, 131, 131, 131);

$x 1 = $paddingLeft;
$y 1 = $paddingTop;
$x 2 = $img _w-$paddingRight;
$y 2 = $img _h-$paddingBottom;
Imagerectangle ($this->image, $x 1, $y 1, $x 2, $y 2, $border _color);
Imagefilledpolygon ($this->image, Array ($x 1-5, $y 1+10, $x 1-5, $y 2+10, $x 1, $y 2, $x 1, $y 1), 4, $side _BG);
Imagepolygon ($this->image, Array ($x 1-5, $y 1+10, $x 1-5, $y 2+10, $x 1, $y 2, $x 1, $y 1), 4, $border _color);
Imagefilledpolygon ($this->image, Array ($x 1-5, $y 2+10, $x 2-5, $y 2+10, $x 2, $y 2, $x 1, $y 2), 4, $side _BG);
Imagepolygon ($this->image, Array ($x 1-5, $y 2+10, $x 2-5, $y 2+10, $x 2, $y 2, $x 1, $y 2), 4, $border _color);
Imageline ($this->image, $x 1, $y 2, $x 2, $y 2, $side _bg2);

$total _h = $img _h-$paddingTop-$paddingBottom;
$every _h = $total _H/11;
For ($i =1 $i <=10; $i + +)
{
Imageline ($this->image, $x 1, $y 1+ ($every _h* $i), $x 2, $y 1+ ($every _h* $i), $line _color); Draw Cable
}

$max _h = max ($this->_barheights);
For ($i =1 $i <=10; $i + +)
{
$value = $max _h-(($max _H/10) * ($i-1));
$value = Strval ($value);
$str _w = strlen ($value) *5;
Imageline ($this->image, $x 1-5-3, $y 1+10+ ($every _h* $i), $x 1-3+1, $y 1+10+ ($every _h* $i), $dial _color); Draw a tick mark line
Imagestring ($this->image, 3, $x 1-5-3-$str _w-1, $y 1+10+ ($every _h* $i) -5, $value, 0x000000);
}
}


Public Function drawbars ()
{
$counts = count ($this->_barheights);
if (Empty ($this->_barcolors))
{
$color = $this->setcolor ();
$this->_barcolors = array_slice ($color, 0, $counts);
Shuffle ($this->_barcolors);
}
$every _w = ($this->_w-$this->_paddingleft-$this->_paddingright)/$counts; Each section of the width
$barL = $every _w;
$barL = ($barL > $this->_barl*1.35+6)? $this->_barl: $barL/1.35-6;
$max _h = max ($this->_barheights);
$ruler _h = $this->_h-$this->_paddingtop-$this->_paddingbottom; Ruler Total Height
$stander _h = $ruler _h-($ruler _H/11); Ruler 10-Equal height
$i = 0;
foreach ($this->_barheights as $val)
{
$h = ($stander _h/$max _h) * $VAL;
$x = $this->_paddingleft + ($every _w* $i) + (($every _w-($barL *1.35))/2);
$y = $this->_h-$this->_paddingbottom + 10-$h;
$t _color = $this->_barcolors[$i];
$b _color = $this->_barcolors[$i];
$s _color = $this->_barcolors[$i];


$rgb = $this->getrgb ($this->_barcolors[$i]);
$R = $rgb [' R '] * 0.7;
$G = $rgb [' G '] * 0.7;
$B = $rgb [' B '] * 0.7;

$c 1 = $R > 0? Dechex ($R): ' 00 ';
$c 2 = $G > 0? Dechex ($G): ' 00 ';
$c 3 = $B > 0? Dechex ($B): ' 00 ';

$t _color = $b _color;
$s _color = $c 1. $c 2. $c 3;

$SingleBar = new Singlebar ($x, $y, $h, $barL);
$SingleBar->draw ($this->image, $t _color, $b _color, $s _color);
$i + +;
}
}

Public Function Drawtitle ()
{
if (Empty ($this->_title))
{
Return
}
$font = 5;
$font _w = Imagefontwidth ($font);
$len = strlen ($this->_title);
$x = ($this->_w + $this->_paddingleft-$this->_paddingright)/2;
$x-= ($len * $font _w)/2;
$y = ($this->_paddingtop-$font _w)/2 + 12;
Imagestring ($this->image, $font, $x, $y, $title, 0x000000);
Imagettftext ($this->image, 0, $x, $y, 0x000000, Default_font_path, $this->_title);
}

Public Function Drawlables ()
{
$x 1 = $this->_paddingleft-5;
$y 1 = $this->_h-$this->_paddingbottom + 20;
$x 2 = $this->_w-$this->_paddingright;
$y 2 = $this->_h-10;
Imagefilledrectangle ($this->image, $x 1, $y 1, $x 2, $y 2, 0XFFFFFF);
Imagerectangle ($this->image, $x 1, $y 1, $x 2, $y 2, 0x000000);
$space = 5;
$x = $x 1 + 3;
$y = $y 1 + 3;
foreach ($this->_bartexts as $key => $val)
{
$color = $this->_barcolors[$key];
$rgb = $this->getrgb ($color);
$BG = Imagecolorallocate ($this->image, $rgb [' R '], $rgb [' G '], $rgb [' B ']);
Imagefilledrectangle ($this->image, $x, $y, $x +12, $y +12, $BG); Draw a 12*12 Rectangle
Imagerectangle ($this->image, $x, $y, $x +12, $y +12, 0x000000); Rectangular box for painting 12*12
Imagettftext ($this->image, 0, $x +12+3, $y +12, 0x000000, Default_font_path, $val);
$x + + $space + (strlen ($val) *8) + $space;
if ($x + (strlen ($val) *8) >= $this->_w-$this->_paddingleft-$this->_paddingright)
{
$x = $x 1 + 3;
$y = $y + 12 + 3;
}
}
}

Public Function Resetpaddingbottom ()
{
$ruler _w = $this->_w-$this->_paddingleft-$this->_paddingright;
$label _w = $this->getlabletotalwidth ();
$lines = ceil ($label _w/$ruler _w);
$h = $lines + (3 * ($lines + 1)) + 30;
return $h;
}

Public Function Resetheight ()
{
$padding _bottom = $this->resetpaddingbottom ();
if ($this->_h-$padding _bottom < 222)
{
Return 222 + $padding _bottom;
}
return $this->_h;
}


Public Function Getlabletotalwidth ()
{
$counts = count ($this->_bartexts);
$space = 5;
$total _len = 0;
foreach ($this->_bartexts as $val)
{
$total _len + + strlen ($val);
}

$tx _w = ($total _len * 9) + ((+ 3 + $space) * $counts);
return $TX _w;
}

Public Function SETBG ($color)
{
$this->_bgcolor = $color;
}

Public Function Settitle ($title)
{
$this->_title = $title;
}

Public Function SetColor ()
{
$ar = Array (' FF ', ', ', ', ', ', ', ', ' cc ');
$color = Array ();
For ($i =0 $i <6; $i + +)
{
For ($j =0 $j <6; $j + +)
{
For ($k =0 $k <6; $k + +)
{
$color [] = $ar [$i]. $ar [$j]. $ar [$k];
}
}
}

$color 2 = array ();
For ($i =1 $i <216; $i + + 4)
{
$color 2[] = $color [$i];
}

return $color 2;
}

Public Function getRGB ($color)
{
$ar = Array ();
$color = Hexdec ($color);
$ar [' R '] = ($color >>16) & 0xFF;
$ar [' G '] = ($color >>8) & 0xFF;
$ar [' B '] = ($color) & 0xFF;
return $ar;
}
}

/***/
$bar = new Bar (+, $, 360), Array (' AAAAA ', ' bbbbb ', ' ccccc ', ' ddddd ', ' eeeeee '), ' FFFFFF ', ' ggggggg ', ' hhhhhhhhh ');
$bar->settitle (' Create a perfect bar chart! ');
$bar->stroke ();
/***/
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.