 |
[PHP]
การวาดกราฟ php การวาดกราฟทำไงคะ วาดกราฟเส้นค่ะ จาก จุด (a,b) - (b,c) - (c,d) - (d,e)- (e,f) |
|
 |
|
|
 |
 |
|
JP Graph ช่วยท่านได้
|
 |
 |
 |
 |
Date :
16 มี.ค. 2551 22:06:29 |
By :
theboubman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวอย่างนี้เป็นการสร้างกราฟแผนภูมิแท่ง ใช้ PHP ในการสร้างครับ
Code (PHP)
01. <?
02. header( "Content-type: image/png" );
03.
04. $data [1]= "30" ;
05. $data [2]= "40" ;
06. $data [3]= "50" ;
07. $data [4]= "60" ;
08. $data [5]= "70" ;
09. $data [6]= "80" ;
10. $data [7]= "90" ;
11. $data [8]= "100" ;
12. $img = ImageCreate(500, 310);
13. ImageColorAllocate( $img , 255, 255, 255);
14.
15.
16. $line = ImageColorAllocate( $img ,176, 208, 210);
17. $dataFill = ImageColorAllocate( $img , 122, 176, 180);
18. $black = ImageColorAllocate( $img , 87, 32, 324);
19.
20.
21. ImageLine( $img , 50, 50, 50, 50, $line );
22.
23. $bartick = 20;
24. $barspace = 30;
25. $barmax = 400;
26. $baseX = 50;
27. $baseY = 30;
28.
29.
30. for ( $i =1; $i <= count ( $data ); $i ++) {
31.
32. $percent [ $i ]= $data [ $i ];
33. $data [ $i ]= $data [ $i ]* $barmax /100;
34.
35.
36.
37. $setBar [ $i ] = array ( $baseX , $baseY ,
38. ( $baseX + $barmax ), $baseY ,
39. ( $baseX + $barmax ), ( $baseY + $bartick ),
40. $baseX , ( $baseY + $bartick ));
41.
42. ImagePolygon( $img , $setBar [ $i ], 4, $line );
43.
44. ImageFilledRectangle( $img , $baseX , $baseY , ( $data [ $i ]+50), ( $baseY + $bartick ), $dataFill );
45.
46. ImageString( $img , 3, ( $baseX - 10), $baseY + 5, $i , $black );
47. ImageString( $img , 3, ( $data [ $i ] + 55), $baseY + 5,
48. number_format( $percent [ $i ]) . "%" , $black );
49.
50. $baseY += $barspace ;
51. }
52.
53. ImagePNG( $img );
54. ImageDestroy( $img );
55. ?>
Code (Sample1.php)
01. <html>
02. <head>
03. <title>กราฟ</title>
04. <meta http-equiv= "Content-Type" content= "text/html; charset=windows-874" >
05. </head>
06. <body>
07. ตัวอย่างกราฟนะจ๊ะ
08. <br>
09. <?php
10. echo "<img src='Sample2.php'><br><br>\n" ;
11. ?>
12. </body>
13. </html>
Code (Sample2.php)
01. <?
02. header( "Content-type: image/png" );
03.
04. $data [1]= "30" ;
05. $data [2]= "40" ;
06. $data [3]= "50" ;
07. $data [4]= "60" ;
08. $data [5]= "70" ;
09. $data [6]= "80" ;
10. $data [7]= "90" ;
11. $data [8]= "100" ;
12. $img = ImageCreate(500, 310);
13. ImageColorAllocate( $img , 255, 255, 255);
14.
15.
16. $line = ImageColorAllocate( $img ,176, 208, 210);
17. $dataFill = ImageColorAllocate( $img , 122, 176, 180);
18. $black = ImageColorAllocate( $img , 87, 32, 324);
19.
20.
21. ImageLine( $img , 50, 50, 50, 50, $line );
22.
23. $bartick = 20;
24. $barspace = 30;
25. $barmax = 400;
26. $baseX = 50;
27. $baseY = 30;
28.
29.
30. for ( $i =1; $i <= count ( $data ); $i ++) {
31.
32. $percent [ $i ]= $data [ $i ];
33. $data [ $i ]= $data [ $i ]* $barmax /100;
34.
35.
36.
37. $setBar [ $i ] = array ( $baseX , $baseY ,
38. ( $baseX + $barmax ), $baseY ,
39. ( $baseX + $barmax ), ( $baseY + $bartick ),
40. $baseX , ( $baseY + $bartick ));
41.
42. ImagePolygon( $img , $setBar [ $i ], 4, $line );
43.
44. ImageFilledRectangle( $img , $baseX , $baseY , ( $data [ $i ]+50), ( $baseY + $bartick ), $dataFill );
45.
46. ImageString( $img , 3, ( $baseX - 10), $baseY + 5, $i , $black );
47. ImageString( $img , 3, ( $data [ $i ] + 55), $baseY + 5,
48. number_format( $percent [ $i ]) . "%" , $black );
49.
50. $baseY += $barspace ;
51. }
52.
53. ImagePNG( $img );
54. ImageDestroy( $img );
55. ?>
สำหรับอื่น ๆ สามารถอ่านได้จาก
Ref : PHP Graph & Charts
หรือจะลองใช้งาน
PHP JpGraph
ครับ
|
 |
 |
 |
 |
Date :
2009-04-25 09:02:30 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|