PHP bar chart implementation code

Source: Internet
Author: User

If you have any questions, you can add QQ: 460634320 for discussion.
:

Copy codeThe Code is as follows:
<? Php
Function createImage ($ data, $ twidth, $ tspace, $ height ){
Header ("Content-Type: image/jpeg ");
$ Dataname = array ();
$ Datavalue = array (); // values in data
$ I = 0;
$ J = 0;
$ K = 0;
$ Num = sizeof ($ data );

Foreach ($ data as $ key => $ val ){
$ Dataname [] = $ key;
$ Datavalue [] = $ val;
}

$ Width = $ num * ($ twidth + $ tspace) + 20; // obtain the image width.
$ Im = imagecreate ($ width, $ height); // create an image

$ Bgcolor = imagecolorallocate ($ im, 255,255,255); // background color
$ Jcolor = imagecolorallocate ($ im, 255,255, 0); // specifies the background color of the rectangle.
$ Acolor = imagecolorallocate ($ im, 0); // line color

Imageline ($ im, 25, $ height-20, $ width-5, $ height-20, $ acolor); // X axis
Imageline ($ im, 25, $ height-20, 25, 2, $ acolor); // y axis
While ($ I <$ num ){
Imagefilledrectangle ($ im, $ I * ($ tspace + $ twidth) + 40, $ height-$ datavalue [$ I]-20, $ I * ($ twidth + $ tspace) + $ tspace + 40, $ height-20, $ jcolor); // draw a rectangle
Imagestring ($ im, 3, $ I * ($ tspace + $ twidth) + 40 + $ twidth/2, $ height-$ datavalue [$ I]-35, $ datavalue [$ I], $ acolor); // write the value on the column
Imagestring ($ im, 3, $ I * ($ tspace + $ twidth) + 40 + $ twidth/2, $ height-15, $ dataname [$ I], $ acolor); // write the value below the column
$ I ++;

}
While ($ j <400/10 ){
Imageline ($ im, 25, ($ height-20)-$ j * 8, 28, ($ height-20)-$ j * 8, $ acolor); // draw a scale
Imagestring ($ im, 2, 5, ($ height-30)-$ j * 8, $ j * 10, $ acolor); // mark the scale value
$ J = $ j + 10;
}
Imagejpeg ($ im );
}
$ Data = array ("1" => 25, "2" => 30, "3" => 21 );
CreateImage ($ data, 300 );

?>

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.