Posted By

jatkins on 02/14/11


Tagged

bar generator chart graph


Versions (?)

Bar Chart Generator


 / Published in: PHP
 

Released under the MIT license by Josh Atkins. Requires http://mierendo.com/software/antialiased_arcs/ by Ulrich Mierendorff (also under the MIT license).

  1. <?php
  2. function new_barchart($values, $labels, $x_axis_label, $y_axis_label, $title, $bar_width = 100) {
  3. $bars = $values;
  4. sort($values, SORT_NUMERIC);
  5. $highest_value = $values[count($values)-1];
  6. /*if(strlen($highest_value)>2) {
  7. $division_by = pow(10, strlen($highest_value) - 2);
  8. for($i=0;$i<count($bars);$i++) {
  9. $bars[$i] = $bars[$i] / $division_by;
  10. }
  11. }
  12. else
  13. $division_by = 1;*/
  14. $division_by = 1;
  15. while($highest_value>250) {
  16. $highest_value /= 3;
  17. $division_by *= 3;
  18. }
  19. if($division_by!=1) {
  20. $highest_value *= $division_by;
  21. for($i=0;$i<count($bars);$i++) {
  22. $bars[$i] = $bars[$i] / $division_by;
  23. }
  24. }
  25. // $division_by = 1;
  26. $barchart = imagecreatetruecolor(1, 1);
  27. $title_coordinates = imagettftext($barchart, 10, 0, 0, 10, 0, 'C:/Windows/Fonts/arialbd.ttf', $title); // draw the title to get its coordinates
  28. $y_axis_max_value = imagettftext($barchart, 10, 0, 0, $title_coordinates[1], 0, 'C:/Windows/Fonts/arialbd.ttf', $highest_value); // draw the y-axis max value label
  29. $width_of_chart = count($bars) * ($bar_width + 10) + $y_axis_max_value[2] + 10;
  30. $barchart = imagecreatetruecolor($title_coordinates[2] > $width_of_chart ? $title_coordinates[2] : $width_of_chart, $title_coordinates[3] + 11 + $highest_value / $division_by);
  31. imagefilledrectangle($barchart, 0, 0, imagesx($barchart), imagesy($barchart), imagecolorallocate($barchart, 255, 255, 255)); // color the background white
  32. imagettftext($barchart, 10, 0, 0, 10, 0, 'C:/Windows/Fonts/arialbd.ttf', $title); // draw the title again
  33. $max_value_label_coordinates = imagettftext($barchart, 10, 0, 0, 31, 0, 'C:/Windows/Fonts/arialbd.ttf', $highest_value); // draw the max value label
  34. $middle_of_max_value_label = ($max_value_label_coordinates[7] + $max_value_label_coordinates[1]) / 2;
  35. imagefilledrectangle($barchart, $max_value_label_coordinates[2] + 10, $middle_of_max_value_label, $max_value_label_coordinates[2] + 3, $middle_of_max_value_label + 1, 0); // draw the line from the y-axis to the max value label
  36. imagefilledrectangle($barchart, $max_value_label_coordinates[2] + 10, $title_coordinates[3] + 12, $max_value_label_coordinates[2] + 11, imagesy($barchart), 0); // draw the y-axis
  37. imagefilledrectangle($barchart, $max_value_label_coordinates[2] + 10, imagesy($barchart) - 2, imagesx($barchart), imagesy($barchart), 0); // draw the x-axis
  38. /* START: Draw bars */
  39. $previous_bar_right = $max_value_label_coordinates[2] + 12;
  40. $i = 0;
  41. foreach($bars as $bar) {
  42. imagefilledrectangle($barchart, $previous_bar_right, imagesy($barchart) + 1 - $bar, $previous_bar_right + $bar_width, imagesy($barchart) - 3, imagecolorallocate($barchart, rand(25, 255), rand(25, 255), rand(25, 255)));
  43. imagettftext($barchart, 10, 0, $previous_bar_right + 2, imagesy($barchart) - 5, 0, 'C:/Windows/Fonts/arialbd.ttf', $labels[$i]);
  44. $previous_bar_right += $bar_width + 10;
  45. $i++;
  46. }
  47. /* END: Draw bars */
  48. header('Content-Type: image/png');
  49. imagepng($barchart);
  50. }
  51.  
  52. new_barchart(array(30.45e6, 34e6, 3312e3), array('Space Shuttle', 'Saturn V', 'Delta IV'), 0, 0, 'Comparison of launch vehicle thrust (Meganewtons)');
  53. ?>

Report this snippet  

You need to login to post a comment.