Php Tortendiagramm erstellen
Mit dem Code lassen sich ganz einfach Tortendiagramme erstellen.
Der hier verwendete Code
<?php
$piece_values = array(70, 15, 10, 7);
$piece_names = array("MSIE", "Firefox", "Opera", "Safari");
$height = 580; $width = 600;
$height3d = 15;
$no_sort = false; // Sortierung an
function graph_colors($im) {
$colors["background"] = ImageColorAllocate($im, 255, 255, 255);
$colors["text"] = ImageColorAllocate($im, 0, 0, 0);
$colors[0] = ImageColorAllocate($im, 255, 80, 80);
$colors[1] = ImageColorAllocate($im, 254, 255, 80);
$colors[2] = ImageColorAllocate($im, 81, 80, 255);
$colors[3] = ImageColorAllocate($im, 255, 80, 254);
$colors[4] = ImageColorAllocate($im, 80, 255, 81);
$colors[5] = ImageColorAllocate($im, 80, 255, 255);
$colors_dark[0] = ImageColorAllocate($im, 155, 0, 0);
$colors_dark[1] = ImageColorAllocate($im, 154, 155, 0);
$colors_dark[2] = ImageColorAllocate($im, 0, 0, 155);
$colors_dark[3] = ImageColorAllocate($im, 155, 0, 154);
$colors_dark[4] = ImageColorAllocate($im, 0, 155, 0);
$colors_dark[5] = ImageColorAllocate($im, 0, 155, 155);
return array("colors" => $colors, "colors_dark" => $colors_dark);
}
function graph_pie_3d($piece = array(), $piece_text = array(), $height = 180, $width = 200, $height3d = 15, $no_sort = false) {
header("Content-type: image/png");
$im = ImageCreateTrueColor($width, $height);
$graph_colors = graph_colors($im);
$colors = $graph_colors["colors"];
$colors_dark = $graph_colors["colors_dark"];
ImageFill($im, 0, 0, $colors["background"]);
$sum = array_sum($piece);
$height_graph = $height - ImageFontHeight(2) * count($piece) - $height3d - 15;
$width_graph = $width - 20;
$x_graph = $width / 2;
$y_graph = $height_graph / 2 + $height3d + 10;
if (!$no_sort) arsort($piece);
for ($i = 0; $i <= $height3d; $i++) {
$l = 0;
$j = 0;
while (list($key, $val) = each($piece)) {
$degree = $val / $sum * 360;
ImageFilledArc($im, $x_graph, $y_graph - $i, $width_graph, $height_graph, $l, $l + $degree, $colors_dark[$j], IMG_ARC_PIE);
$l+= $degree;
$j++;
}
reset($piece);
}
reset($piece);
$l = 0;
$j = 0;
while (list($key, $val) = each($piece)) {
$degree = $val / $sum * 360;
ImageFilledArc($im, $x_graph, $y_graph - $height3d, $width_graph, $height_graph, $l, $l + $degree, $colors[$j], IMG_ARC_PIE);
$y_legend = $height_graph + $height3d + ImageFontHeight(2) * ($j + 1);
ImageFilledRectangle($im, 10, $y_legend + 2, 18, $y_legend + ImageFontHeight(2) - 2, $colors[$j]);
ImageRectangle($im, 10, $y_legend + 2, 18, $y_legend + ImageFontHeight(2) - 2, $text);
ImageString($im, 2, 24, $y_legend, $piece_text[$key]." (".$val.")", $colors["text"]);
$l+= $degree;
$j++;
}
ImagePNG ($im);
}
graph_pie_3d($piece_values, $piece_names, $height, $width, $height3d, $no_sort);
?>