Scripts
Cirkeldiagram
Dit script verwacht paar gegevens uit de $_GET['stat'] waarde. Dat ziet er zo uit: diagram.php?stats=Appels-500;Bananen-250;Peren-150 Bestanden: index.php - Zie hieronder Verdana.ttf : Te vinden in C:\WINDOWS\Fonts\Verdana.ttf 23-11-2008, 12:54 Update in beveiliging : Geen headers sent errors meer als $_GET['stats'] leeg is. 23-11-2008, 12:56 Update in beveiliging : Het deel na de - in de $_GET['stats'] var moet nu een getal zijn. 24-11-2008, 20:21 Update in script: - Breedte instelbaar - Veel random kleuren toegevoegd, met de optie voor de script-gebruiker om meer toe te voegen
cirkeldiagram
[code]
<?php
//--- Colours
//--- Function to switch html to rgb
function html2rgb( $color = '000000' )
{
// Gevonden via Google, deze is dus NIET van mij.
if ($color[0] == '#')
{
$color = substr($color, 1);
}
$length = strlen( $color );
if ( $length == 6 )
{
list($r, $g, $b) = array(
$color[0].$color[1],
$color[2].$color[3],
$color[4].$color[5]);
}
elseif ( $length == 3 )
{
list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
}
else
{
return false;
}
$r = hexdec( $r );
$g = hexdec( $g );
$b = hexdec($b);
return array($r, $g, $b);
}
$colors = array(
'FFFAFA',
'F8F8FF',
'F5F5F5',
'DCDCDC',
'FFFAF0',
'FDF5E6',
'FAF0E6',
'FAEBD7',
'FFEFD5',
'FFEBCD',
'FFE4C4',
'FFDAB9',
'FFDEAD',
'FFE4B5',
'FFF8DC',
'FFFFF0',
'FFFACD',
'FFF5EE',
'F0FFF0',
'F5FFFA',
'F0FFFF',
'F0F8FF',
'E6E6FA',
'FFF0F5',
'FFE4E1',
'000000',
'2F4F4F',
'696969',
'708090',
'778899',
'BEBEBE',
'D3D3D3',
'191970',
'000080',
'6495ED',
'483D8B',
'6A5ACD',
'7B68EE',
'8470FF',
'0000CD',
'4169E1',
'0000FF',
'1E90FF',
'00BFFF',
'87CEEB',
'4682B4',
'B0C4DE',
'ADD8E6',
'B0E0E6',
'AFEEEE',
'00CED1',
'48D1CC',
'40E0D0',
'00FFFF',
'E0FFFF',
'5F9EA0',
'66CDAA',
'7FFFD4',
'006400',
'556B2F',
'8FBC8F',
);
/* Meer kleuren? http://catless.ncl.ac.uk/Lindsay/swatch1.html (Swatch 1 tot 8)
-------------------------------------------------------------------------------------------- */
//--- Uniek!
$colors = array_unique( $colors );
function getRandomcolor()
{
global $used_colours, $img, $colors;
$rand = rand(0, ( count( $colors ) - 1 ) );
$color = $colors[$rand];
if ( in_array( $rand, $used_colours ) )
{
return getRandomcolor();
}
else
{
$used_colours[] .= $rand;
}
list( $r, $g, $b ) = html2rgb( $color );
return imagecolorallocate( $img, $r, $g, $b );
}
function h2r( $color = '000000')
{
return html2rgb( $color );
}
//--- Get stats to fix circle diagram with
if ( $_GET['stats'] == '' && $_GET['stats'] == NULL )
{
exit( 'Sorry, geen opties gevonden! (Geef deze zo op: "' . $_SERVER['PHP_SELF'] . '?stats=Peren-500;Appels-300;Bananen-200&side=300")' );
}
else
{
$explode = explode(';', $_GET['stats'] );
if ( is_array( $explode ) )
{
$input_stats = array();
foreach( $explode as $value )
{
list( $key, $val ) = explode('-', $value );
$input_stats[$key] = $val;
}
$options = @count( $input_stats );
if ( $options < 1 )
{
exit( 'Sorry, geen opties gevonden! (Geef deze zo op: "' . $_SERVER['PHP_SELF'] . '?stats=Peren-500;Appels-300;Bananen-200&side=300")' );
}
}
else
{
exit( 'Sorry, geen opties gevonden! (Geef deze zo op: "' . $_SERVER['PHP_SELF'] . '?stats=Peren-500;Appels-300;Bananen-200&side=300")' );
}
}
foreach( $input_stats as $key => $value )
{
if ( ( $key == '' && $key == NULL ) OR ( $value == '' && $value == NULL ) )
{
exit( 'Sorry, geen opties gevonden! (Geef deze zo op: "' . $_SERVER['PHP_SELF'] . '?stats=Peren-500;Appels-300;Bananen-200")' );
}
if ( !is_numeric( $value ) )
{
exit( 'Het aantal moet een nummer zijn (Was ' . $value . ')' );
}
}
$side = ( $_GET['side'] != '' && $_GET['side'] != NULL && is_numeric( $_GET['side'] ) && $_GET['side'] > 0 && $_GET['side'] < 400 ) ? $_GET['side'] : 200;
$height = $side + ( $options * 25 );
$width = ( $side < 300 ) ? 300 : $side;
//--- Create image stream
$img = @imagecreate( $width, $height ) or exit( 'Failed creating image' );
//--- Background color (white)
list( $r, $g, $b ) = h2r( 'FFFFFF' );
$background_color = imagecolorallocate($img, $r, $g, $b );
$stats = array();
$totaal = 0;
foreach( $input_stats as $value )
{
$totaal = $totaal + $value;
}
$id = 0;
foreach( $input_stats as $name => $stat )
{
$procent = $stat / $totaal * 100;
$start = 0 + $eind;
$eind = 0 + $start + ( $procent * 3.6 );
$stats[$id] = array(
'object_naam' => $name,
'aantal' => $stat,
'procenten' => $procent,
'start' => $start,
'eind' => $eind,
);
$id++;
}
//--- Create blocks
$zwart = imagecolorallocate($img, 0, 0, 0);
$arc = imagefilledarc($img, ( $side / 2 ), ( $side / 2 ), $side - 15, $side - 15, 0, 360, $zwart, IMG_ARC_PIE );
$i = 0;
$font = './Verdana.ttf';
$used_colours = array();
foreach( $stats as $name => $stat )
{
$colour = getRandomcolor();
$arc = imagefilledarc($img, ( $side / 2 ), ( $side / 2 ), $side - 15, $side - 15, $stat['start'], $stat['eind'], $colour, IMG_ARC_PIE );
// Give the information below the diagramm
$x_top = $side + ( $i * 20 );
imagefilledrectangle($img, 40, $x_top, 50, $x_top + 10, $colour );
imagettftext($img, 10, 0, 60, $x_top + 10, imagecolorallocate($img, 0, 0, 0), $font, $stat['aantal'] . ' ' .$stat['object_naam'] );
$i++;
if ( count( array_unique( array_merge( $used_colours, $colors ) ) ) == count( $colors ) )
{
//--- Alle kleuren gebruikt, nieuwe loop
$used_colours = array();
}
}
//--- Create image
header( 'Content-type: image/png' );
imagepng( $img );
imagedestroy( $img );
?>
[/code]
Reacties
0