note 52208 deleted from ref.image by betz

From: Date: Sun, 24 Apr 2005 16:43:47 +0000
Subject: note 52208 deleted from ref.image by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-88495@lists.php.net to get a copy of this message
Note Submitter: info at b1g dot de Reason: bad code ---- A function to generate a simple connected-point diagram. Perhaps someone might need this. <?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'); // Find minimum and maximum values $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; // Create image $im = imagecreate($w, $h); // Allocate base colors $grey = imagecolorallocate($im, 240, 240, 240); $black = imagecolorallocate($im, 0, 0, 0); // Associate and allocate colors to types $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); // Draw base stuff 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); // Y-Axis $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*($max_y/$raster_y), 0); imageline($im, 36, $y, 40, $y, $black); imagestring($im, 2, $max_y < 10 ? 15 : 10, $y-6, $zahl, $black); } // X-Axis $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*($max_x/$raster_x), 0); imageline($im, $x, $h-60, $x, $h-54, $black); imagestring($im, 2, $max_x < 10 ? $x-3 : $x-5, $h-45, $zahl, $black); } // Draw data $last_legend_x = 10; foreach($data as $typ) { // Draw legend 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; // Draw graph $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; } } // Show image header('Content-Type: image/png'); imagepng($im); } ?> A test of the function: <?php Graph( array( 0 => array( 'titel' => 'HTTP', 'data' => array( 1 => 44, // x => y 2 => 4, 3 => 6, 4 => 7, 5 => 6 ) ), 1 => array( 'titel' => 'FTP', 'data' => array( 1 => 2, 2 => 1, 3 => 3, 4 => 4, 5 => 2 ) ), 2 => array( 'titel' => 'All', 'data' => array( 1 => 48, 2 => 5, 3 => 9, 4 => 11, 5 => 8 ) ) ), 420, // width of diagram 280, // height of diagram "Website-Traffic, April 2005", // title "Traffic in GB", // title of y-axis "Days", // title of x-axis true, // start at y=0? false // start at x=0? ); ?>

« previous php.notes (#88495) next »