Hallo, ich habe bei php.net eine Diagramm funktion gefunden, wobei allerdings nicht steht wie man sie bentutzt? hat evtl jemand eine idee?
mein eigentliches problem ist wie der $data array aussehen muss?
MfG
Nascar
mein eigentliches problem ist wie der $data array aussehen muss?
PHP-Code:
<?php
function Graph($data, $w, $h, $titel, $y_titel, $x_titel, $y_start_with_null=true, $x_start_with_null=true)
{
$typ_farben = array('0000FF', 'FF0000', '006600', 'FF9900', '663300');
// Maximal- und Minimalwerte ermitteln
$min_x = 0xFFFFFFFF;
$max_x = 0x00000000;
$min_y = 0xFFFFFFFF;
$max_y = 0x00000000;
foreach($data as $typ)
{
foreach($typ['data'] as $x => $y)
{
$min_x = min($min_x, $x);
$max_x = max($max_x, $x);
$min_y = min($min_y, $y);
$max_y = max($max_y, $y);
}
}
reset($data);
if($y_start_with_null)
$min_y = 0;
if($x_start_with_null)
$min_x = 0;
// Bild erstellen
$im = imagecreate($w, $h);
// Basisfarben registrieren
$grey = imagecolorallocate($im, 240, 240, 240);
$black = imagecolorallocate($im, 0, 0, 0);
// Den Typen Farben zuordnen und im Bild registrieren
$i = 0;
foreach($data as $key=>$typ)
{
if($i > (count($typ_farben)-1))
$i = 0;
$data[$key]['color'] = imagecolorallocate($im, hexdec(substr($typ_farben[$i], 0, 2)), hexdec(substr($typ_farben[$i], 2, 2)), hexdec(substr($typ_farben[$i], 4, 2)));
$i++;
}
reset($data);
// Grungeruest zeichnen
imagestring($im, 2, (($w/2) - ((strlen($titel)*6)/2)), 8, $titel, $black);
imagestring($im, 2, 60, 28, $y_titel, $black);
imagestring($im, 2, $w-30-(strlen($x_titel)*6), $h-28, $x_titel, $black);
imageline($im, 40, 30, 40, $h-60, $black);
imageline($im, 40, $h-60, $w-30, $h-60, $black);
$graph_width = $w-30-40;
$graph_height = $h-40-60;
$x_segment = @($graph_width / ($max_x - $min_x));
$y_segment = @($graph_height / ($max_y - $min_y));
// Raster Y-Achse
$raster_y = ($max_y > (floor($graph_height/22)) ? floor($graph_height/22) : $max_y);
for($i=$min_y; $i<=$raster_y; $i++)
{
$y = ($h-60) - (($i-$min_y)*@($graph_height/($raster_y-$min_y)));
$zahl = round(($i-$min_y)*@($max_y/($raster_y-$min_y)), 0)+$min_y;
imageline($im, 36, $y, 40, $y, $black);
imagestring($im, 2, $max_y < 10 ? 15 : 10, $y-6, $zahl, $black);
}
// Raster X-Achse
$raster_x = ($max_x > floor($graph_width/32)) ? floor($graph_width/32) : $max_x;
for($i=$min_x; $i<=$raster_x; $i++)
{
$x = 40 + (($i-$min_x)*($graph_width/($raster_x-$min_x)));
$zahl = round(($i-$min_x)*($max_x/($raster_x-$min_x)), 0)+$min_x;
imageline($im, $x, $h-60, $x, $h-54, $black);
imagestring($im, 2, $max_x < 10 ? $x-3 : $x-5, $h-45, $zahl, $black);
}
// Daten zeichnen
$last_legend_x = 10;
foreach($data as $typ)
{
// Legende zeichnen
imagefilledrectangle($im, $last_legend_x, $h-25, $last_legend_x+6, $h-18, $typ['color']);
imagerectangle($im, $last_legend_x, $h-25, $last_legend_x+6, $h-18, $black);
$last_legend_x += 10;
imagestring($im, 2, $last_legend_x, $h-28, $typ['titel'], $typ['color']);
$last_legend_x += (strlen($typ['titel']) * 6) + 10;
// Graph zeichnen
$last_x = 0;
$last_y = 0;
foreach($typ['data'] as $x=>$y)
{
$new_x = 40+(($x-$min_x)*$x_segment);
$new_y = ($h-60)-(($y-$min_y)*$y_segment);
imageline($im, $last_x==0 ? $new_x : $last_x, $last_y==0 ? $new_y : $last_y, $new_x, $new_y, $typ['color']);
imagefilledrectangle($im, $new_x-1, $new_y-1, $new_x+1, $new_y+1, $typ['color']);
$last_x = $new_x;
$last_y = $new_y;
}
}
// Bild ausgeben
header('Content-Type: image/png');
imagepng($im);
}
?>
Nascar
Kommentar