fork download
  1. <?php
  2. # ------- The graph values in the form of associative array
  3. $values=array(
  4. "Jan" => 110,
  5. "Feb" => 130,
  6. "Mar" => 215,
  7. "Apr" => 81,
  8. "May" => 310,
  9. "Jun" => 110,
  10. "Jul" => 190,
  11. "Aug" => 175,
  12. "Sep" => 390,
  13. "Oct" => 286,
  14. "Nov" => 150,
  15. "Dec" => 196
  16. );
  17.  
  18.  
  19. $img_width=450;
  20. $img_height=300;
  21. $margins=20;
  22.  
  23.  
  24. # ---- Find the size of graph by substracting the size of borders
  25. $graph_width=$img_width - $margins * 2;
  26. $graph_height=$img_height - $margins * 2;
  27. $img=imagecreate($img_width,$img_height);
  28.  
  29.  
  30. $bar_width=20;
  31. $total_bars=count($values);
  32. $gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1);
  33.  
  34.  
  35. # ------- Define Colors ----------------
  36. $bar_color=imagecolorallocate($img,0,64,128);
  37. $background_color=imagecolorallocate($img,240,240,255);
  38. $border_color=imagecolorallocate($img,200,200,200);
  39. $line_color=imagecolorallocate($img,220,220,220);
  40.  
  41. # ------ Create the border around the graph ------
  42.  
  43. imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
  44. imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);
  45.  
  46.  
  47. # ------- Max value is required to adjust the scale -------
  48. $max_value=max($values);
  49. $ratio= $graph_height/$max_value;
  50.  
  51.  
  52. # -------- Create scale and draw horizontal lines --------
  53. $horizontal_lines=20;
  54. $horizontal_gap=$graph_height/$horizontal_lines;
  55.  
  56. for($i=1;$i<=$horizontal_lines;$i++){
  57. $y=$img_height - $margins - $horizontal_gap * $i ;
  58. imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
  59. $v=intval($horizontal_gap * $i /$ratio);
  60. imagestring($img,0,5,$y-5,$v,$bar_color);
  61.  
  62. }
  63.  
  64.  
  65. # ----------- Draw the bars here ------
  66. for($i=0;$i< $total_bars; $i++){
  67. # ------ Extract key and value pair from the current pointer position
  68. list($key,$value)=each($values);
  69. $x1= $margins + $gap + $i * ($gap+$bar_width) ;
  70. $x2= $x1 + $bar_width;
  71. $y1=$margins +$graph_height- intval($value * $ratio) ;
  72. $y2=$img_height-$margins;
  73. imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);
  74. imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);
  75. imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
  76. }
  77. header("Content-type:image/png");
  78. imagepng($img);
  79.  
  80. ?>
Runtime error #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
Fatal error: Call to undefined function imagecreate() in /home/N5GIxw/prog.php on line 27