PHP ver gevorderde |
|
<?php
$titel = "Grootte MySQL";
// array op deze manier genereren $hoofdarray = array(x-as => y-as)
// bv:
$sql = MySQL_query("SELECT * FROM site_database_grootte");
while( $rij = mysql_fetch_assoc( $sql ) ) {
$datum = $rij['datum'];
$grootte = $rij['grootte'];
$hoofdarray = array($datum => $grootte);
}
$gebied_breedte = 400; // grootte van de grafiek zelf (zonder randen)
$gebied_hoogte = 200; // hoogte van de grafiek zelf (zonder randen)
$rand_links = 20; // de breedte van de linker rand (hou rekening met de lengte van de waardes die op de y-as komen, in toekomst misschien automatisch)
$rand_rechts = 10; // de breedte van de rechter rand
$rand_onder = 40; // de rand onder de grafiek (hou rekening met de lengte van de waardes die op de x-as komen, in toekomst misschien automatisch)
$rand_boven = 30; // de rand boven (hou rekening met de titel die hier in komt)
$hor_lijnen = 4; // het aantal verdelingen van de y-as
$hor_raster = TRUE; // horizontale lijnen zichtbaar?
$ver_lijnen = 10; // het aantal verdelingen van de x-as
$ver_raster = FALSE; // verticale lijnen zichtbaar?
$grafiek_titel = $titel; // de titel van de grafiek
header("Content-type: image/png");
if(function_exists("ImageCreateTrueColor"))
{
$plaatje = ImageCreateTrueColor(($gebied_breedte + $rand_rechts + $rand_links), ($gebied_hoogte + $rand_onder + $rand_boven));
}
else
{
$plaatje = ImageCreate(($gebied_breedte + $rand_rechts + $rand_links), ($gebied_hoogte + $rand_onder + $rand_boven));
}
$zwart = ImageColorAllocate($plaatje, 000, 000, 000);
$wit = ImageColorAllocate($plaatje, 255, 255, 255);
$grijs = ImageColorAllocate($plaatje, 225, 225, 225);
$lijn = ImageColorAllocate($plaatje, 000, 000, 255); // blauwe grafieklijn
imagefill($plaatje,1,1,$wit);
// zwarte rand of niet:
//imagefilledrectangle($plaatje, 1, 1, ($gebied_breedte + $rand_rechts + $rand_links - 2), ($gebied_hoogte + $rand_onder + $rand_boven -2), $wit); //
imagefilledrectangle($plaatje, ($rand_links + 1), ($rand_boven + 1), ($rand_links + $gebied_breedte - 1), ($rand_boven + $gebied_hoogte -1), $grijs);
$hoogste = 1;
foreach($hoofdarray as $key => $value)if($value > $hoogste)$hoogste = $value;
$style=array($zwart, $zwart, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT );
imagesetstyle($plaatje,$style);
$waarde = 1;
for($y = $rand_boven;$y < ($gebied_hoogte + $rand_boven + 1);$y += ($gebied_hoogte / $hor_lijnen))
{
$align = $rand_links - 3 - strlen(round($hoogste * $waarde)) * 5;
imagestring($plaatje, 2, $align, $y - 7 , round($hoogste * $waarde), $zwart);
if($hor_raster)imageline($plaatje, $rand_links, $y, ($rand_links + $gebied_breedte), $y, IMG_COLOR_STYLED);
$waarde -= (1 / $hor_lijnen);
}
$aantal = count($hoofdarray);
$interval = $gebied_breedte / ($aantal - 1);
$yinterval = $gebied_hoogte / $hoogste;
$x = $rand_links;
$coords = array();
$ticker = 0;
$numdates = round($aantal / $ver_lijnen);
if($numdates < 1)$numdates = 1;
foreach($hoofdarray as $xas => $yas)
{
$coords[$ticker]["x"] = round($x);
$coords[$ticker]["y"] = ($rand_boven + $gebied_hoogte) - (round($yas * $yinterval));
if($ticker / $numdates == round($ticker / $numdates) || $ticker == 0 || $ticker == $aantal-1)
{
$align = ($gebied_hoogte + $rand_boven) + (strlen($xas) * 5) + 5;
imagestringup($plaatje, 2 , round($x) - 7 , $align, $xas ,$zwart);
if($ver_raster)imageline($plaatje, round($x) , $rand_boven , round($x) , ($rand_boven + $gebied_hoogte), IMG_COLOR_STYLED);
}
$x += $interval;
$ticker++;
}
for($a = 0;$a < count($coords)-1;$a++)
{
imagesmoothline($plaatje, $coords[$a]["x"] , $coords[$a]["y"] , $coords[$a+1]["x"] , $coords[$a+1]["y"], $lijn);
}
imagerectangle($plaatje, $rand_links, $rand_boven, ($rand_links + $gebied_breedte), ($rand_boven + $gebied_hoogte), $zwart);
imagestring($plaatje, 4 , ((($rand_links + $rand_rechts + $gebied_breedte) / 2) - ((strlen($grafiek_titel) / 2) * 8)) , ($rand_boven / 2) - 8 , $grafiek_titel , $zwart);
ImagePNG($plaatje);
ImageDestroy($plaatje);
// functie voor de anti-aliasing van de grafieklijn (van php.net)
function imagesmoothline ( $image , $x1 , $y1 , $x2 , $y2 , $color )
{
$colors = imagecolorsforindex ( $image , $color );
if ( $x1 == $x2 )
{
imageline ( $image , $x1 , $y1 , $x2 , $y2 , $color ); // Vertical line
}
else
{
$m = ( $y2 - $y1 ) / ( $x2 - $x1 );
$b = $y1 - $m * $x1;
if ( abs ( $m ) <= 1 )
{
$x = min ( $x1 , $x2 );
$endx = max ( $x1 , $x2 );
while ( $x <= $endx )
{
$y = $m * $x + $b;
$y == floor ( $y ) ? $ya = 1 : $ya = $y - floor ( $y );
$yb = ceil ( $y ) - $y;
$tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , $x , floor ( $y ) ) );
$tempcolors['red'] = $tempcolors['red'] * $ya + $colors['red'] * $yb;
$tempcolors['green'] = $tempcolors['green'] * $ya + $colors['green'] * $yb;
$tempcolors['blue'] = $tempcolors['blue'] * $ya + $colors['blue'] * $yb;
if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
imagesetpixel ( $image , $x , floor ( $y ) , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
$tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , $x , ceil ( $y ) ) );
$tempcolors['red'] = $tempcolors['red'] * $yb + $colors['red'] * $ya;
$tempcolors['green'] = $tempcolors['green'] * $yb + $colors['green'] * $ya;
$tempcolors['blue'] = $tempcolors['blue'] * $yb + $colors['blue'] * $ya;
if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
imagesetpixel ( $image , $x , ceil ( $y ) , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
$x ++;
}
}
else
{
$y = min ( $y1 , $y2 );
$endy = max ( $y1 , $y2 );
while ( $y <= $endy )
{
$x = ( $y - $b ) / $m;
$x == floor ( $x ) ? $xa = 1 : $xa = $x - floor ( $x );
$xb = ceil ( $x ) - $x;
$tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , floor ( $x ) , $y ) );
$tempcolors['red'] = $tempcolors['red'] * $xa + $colors['red'] * $xb;
$tempcolors['green'] = $tempcolors['green'] * $xa + $colors['green'] * $xb;
$tempcolors['blue'] = $tempcolors['blue'] * $xa + $colors['blue'] * $xb;
if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
imagesetpixel ( $image , floor ( $x ) , $y , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
$tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , ceil ( $x ) , $y ) );
$tempcolors['red'] = $tempcolors['red'] * $xb + $colors['red'] * $xa;
$tempcolors['green'] = $tempcolors['green'] * $xb + $colors['green'] * $xa;
$tempcolors['blue'] = $tempcolors['blue'] * $xb + $colors['blue'] * $xa;
if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
imagesetpixel ( $image , ceil ( $x ) , $y , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
$y ++;
}
}
}
}
?>
<?php $titel = "Grootte MySQL"; // array op deze manier genereren $hoofdarray = array(x-as => y-as) // bv: $sql = MySQL_query("SELECT * FROM site_database_grootte"); $datum = $rij['datum']; $grootte = $rij['grootte']; $hoofdarray = array($datum => $grootte); } $gebied_breedte = 400; // grootte van de grafiek zelf (zonder randen) $gebied_hoogte = 200; // hoogte van de grafiek zelf (zonder randen) $rand_links = 20; // de breedte van de linker rand (hou rekening met de lengte van de waardes die op de y-as komen, in toekomst misschien automatisch) $rand_rechts = 10; // de breedte van de rechter rand $rand_onder = 40; // de rand onder de grafiek (hou rekening met de lengte van de waardes die op de x-as komen, in toekomst misschien automatisch) $rand_boven = 30; // de rand boven (hou rekening met de titel die hier in komt) $hor_lijnen = 4; // het aantal verdelingen van de y-as $hor_raster = TRUE; // horizontale lijnen zichtbaar? $ver_lijnen = 10; // het aantal verdelingen van de x-as $ver_raster = FALSE; // verticale lijnen zichtbaar? $grafiek_titel = $titel; // de titel van de grafiek header("Content-type: image/png"); { $plaatje = ImageCreateTrueColor(($gebied_breedte + $rand_rechts + $rand_links), ($gebied_hoogte + $rand_onder + $rand_boven)); } else { $plaatje = ImageCreate(($gebied_breedte + $rand_rechts + $rand_links), ($gebied_hoogte + $rand_onder + $rand_boven)); } $zwart = ImageColorAllocate($plaatje, 000, 000, 000); $wit = ImageColorAllocate($plaatje, 255, 255, 255); $grijs = ImageColorAllocate($plaatje, 225, 225, 225); $lijn = ImageColorAllocate($plaatje, 000, 000, 255); // blauwe grafieklijn imagefill($plaatje,1,1,$wit); // zwarte rand of niet: //imagefilledrectangle($plaatje, 1, 1, ($gebied_breedte + $rand_rechts + $rand_links - 2), ($gebied_hoogte + $rand_onder + $rand_boven -2), $wit); // imagefilledrectangle($plaatje, ($rand_links + 1), ($rand_boven + 1), ($rand_links + $gebied_breedte - 1), ($rand_boven + $gebied_hoogte -1), $grijs); $hoogste = 1; foreach($hoofdarray as $key => $value)if($value > $hoogste)$hoogste = $value; $style=array($zwart, $zwart, IMG_COLOR_TRANSPARENT , IMG_COLOR_TRANSPARENT , IMG_COLOR_TRANSPARENT ); imagesetstyle($plaatje,$style); $waarde = 1; for($y = $rand_boven;$y < ($gebied_hoogte + $rand_boven + 1);$y += ($gebied_hoogte / $hor_lijnen)) { $align = $rand_links - 3 - strlen(round($hoogste * $waarde)) * 5; imagestring ($plaatje, 2, $align, $y - 7 , round($hoogste * $waarde), $zwart); if($hor_raster)imageline($plaatje, $rand_links, $y, ($rand_links + $gebied_breedte), $y, IMG_COLOR_STYLED); $waarde -= (1 / $hor_lijnen); } $aantal = count($hoofdarray); $interval = $gebied_breedte / ($aantal - 1); $yinterval = $gebied_hoogte / $hoogste; $x = $rand_links; $ticker = 0; $numdates = round($aantal / $ver_lijnen); if($numdates < 1)$numdates = 1; foreach($hoofdarray as $xas => $yas) { $coords[$ticker]["x"] = round($x); $coords[$ticker]["y"] = ($rand_boven + $gebied_hoogte) - (round($yas * $yinterval)); if($ticker / $numdates == round($ticker / $numdates) || $ticker == 0 || $ticker == $aantal-1) { $align = ($gebied_hoogte + $rand_boven) + (strlen($xas) * 5) + 5; imagestringup ($plaatje, 2 , round($x) - 7 , $align, $xas ,$zwart); if($ver_raster)imageline ($plaatje, round($x) , $rand_boven , round($x) , ($rand_boven + $gebied_hoogte), IMG_COLOR_STYLED ); } $x += $interval; $ticker++; } for($a = 0;$a < count($coords)-1;$a++) { imagesmoothline($plaatje, $coords[$a]["x"] , $coords[$a]["y"] , $coords[$a+1]["x"] , $coords[$a+1]["y"], $lijn); } imagerectangle($plaatje, $rand_links, $rand_boven, ($rand_links + $gebied_breedte), ($rand_boven + $gebied_hoogte), $zwart); imagestring ($plaatje, 4 , ((($rand_links + $rand_rechts + $gebied_breedte) / 2) - ((strlen($grafiek_titel) / 2) * 8)) , ($rand_boven / 2) - 8 , $grafiek_titel , $zwart); ImagePNG($plaatje); ImageDestroy($plaatje); // functie voor de anti-aliasing van de grafieklijn (van php.net) function imagesmoothline ( $image , $x1 , $y1 , $x2 , $y2 , $color ) { $colors = imagecolorsforindex ( $image , $color ); if ( $x1 == $x2 ) { imageline ( $image , $x1 , $y1 , $x2 , $y2 , $color ); // Vertical line } else { $m = ( $y2 - $y1 ) / ( $x2 - $x1 ); $b = $y1 - $m * $x1; { $endx = max ( $x1 , $x2 ); while ( $x <= $endx ) { $y = $m * $x + $b; $y == floor ( $y ) ? $ya = 1 : $ya = $y - floor ( $y ); $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , $x , floor ( $y ) ) ); $tempcolors['red'] = $tempcolors['red'] * $ya + $colors['red'] * $yb; $tempcolors['green'] = $tempcolors['green'] * $ya + $colors['green'] * $yb; $tempcolors['blue'] = $tempcolors['blue'] * $ya + $colors['blue'] * $yb; if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ); imagesetpixel ( $image , $x , floor ( $y ) , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) ); $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , $x , ceil ( $y ) ) ); $tempcolors['red'] = $tempcolors['red'] * $yb + $colors['red'] * $ya; $tempcolors['green'] = $tempcolors['green'] * $yb + $colors['green'] * $ya; $tempcolors['blue'] = $tempcolors['blue'] * $yb + $colors['blue'] * $ya; if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ); imagesetpixel ( $image , $x , ceil ( $y ) , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) ); $x ++; } } else { $endy = max ( $y1 , $y2 ); while ( $y <= $endy ) { $x = ( $y - $b ) / $m; $x == floor ( $x ) ? $xa = 1 : $xa = $x - floor ( $x ); $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , floor ( $x ) , $y ) ); $tempcolors['red'] = $tempcolors['red'] * $xa + $colors['red'] * $xb; $tempcolors['green'] = $tempcolors['green'] * $xa + $colors['green'] * $xb; $tempcolors['blue'] = $tempcolors['blue'] * $xa + $colors['blue'] * $xb; if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ); imagesetpixel ( $image , floor ( $x ) , $y , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) ); $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , ceil ( $x ) , $y ) ); $tempcolors['red'] = $tempcolors['red'] * $xb + $colors['red'] * $xa; $tempcolors['green'] = $tempcolors['green'] * $xb + $colors['green'] * $xa; $tempcolors['blue'] = $tempcolors['blue'] * $xb + $colors['blue'] * $xa; if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ); imagesetpixel ( $image , ceil ( $x ) , $y , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) ); $y ++; } } } } ?>
|