/ 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).
Expand |
Embed | Plain Text
<?php function new_barchart($values, $labels, $x_axis_label, $y_axis_label, $title, $bar_width = 100) { $bars = $values; /*if(strlen($highest_value)>2) { $division_by = pow(10, strlen($highest_value) - 2); for($i=0;$i<count($bars);$i++) { $bars[$i] = $bars[$i] / $division_by; } } else $division_by = 1;*/ $division_by = 1; while($highest_value>250) { $highest_value /= 3; $division_by *= 3; } if($division_by!=1) { $highest_value *= $division_by; for($i=0;$i<count($bars);$i++) { $bars[$i] = $bars[$i] / $division_by; } } // $division_by = 1; $barchart = imagecreatetruecolor(1, 1); $title_coordinates = imagettftext($barchart, 10, 0, 0, 10, 0, 'C:/Windows/Fonts/arialbd.ttf', $title); // draw the title to get its coordinates $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 $barchart = imagecreatetruecolor($title_coordinates[2] > $width_of_chart ? $title_coordinates[2] : $width_of_chart, $title_coordinates[3] + 11 + $highest_value / $division_by); imagefilledrectangle($barchart, 0, 0, imagesx($barchart), imagesy($barchart), imagecolorallocate($barchart, 255, 255, 255)); // color the background white imagettftext($barchart, 10, 0, 0, 10, 0, 'C:/Windows/Fonts/arialbd.ttf', $title); // draw the title again $max_value_label_coordinates = imagettftext($barchart, 10, 0, 0, 31, 0, 'C:/Windows/Fonts/arialbd.ttf', $highest_value); // draw the max value label $middle_of_max_value_label = ($max_value_label_coordinates[7] + $max_value_label_coordinates[1]) / 2; 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 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 imagefilledrectangle($barchart, $max_value_label_coordinates[2] + 10, imagesy($barchart) - 2, imagesx($barchart), imagesy($barchart), 0); // draw the x-axis /* START: Draw bars */ $previous_bar_right = $max_value_label_coordinates[2] + 12; $i = 0; foreach($bars as $bar) { imagettftext($barchart, 10, 0, $previous_bar_right + 2, imagesy($barchart) - 5, 0, 'C:/Windows/Fonts/arialbd.ttf', $labels[$i]); $previous_bar_right += $bar_width + 10; $i++; } /* END: Draw bars */ imagepng($barchart); } ?>
You need to login to post a comment.
