-
/**
- * Use case of the php bar chart imagefilledrectugle function
- * By bbs.it-home.org
- */
- 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 );
- ?>
|