Scripts
grafiek
Dit is een GD2 grafiek, met een class. Hoe moet je hem gebruiken? Staat aan het eind een voorbeeld ;) Ik krijg errors van undefined function Geen GD2 geinstalleerd :) wat zijn de parameters van de constructor? new grafiek(int stappen, int x, int y, string titel) Veel succes!
grafiek
<?php
// grafiek class
// by PurpleMadness[PHP4Purple]
// requires gd2-library
// http://purplephp.nl, http://www.php4ever.net
class grafiek {
// declareer variabelen
var $steps = "";
var $y = "";
var $x = "";
var $image = "";
var $space = 20;
var $gy;
var $gx;
var $startplekx;
var $startpleky;
// constructor
function grafiek($steps, $x, $y, $text)
{
// set wat variabelen
$this->steps = $steps;
$this->y = $y;
$this->x = $x;
// achtergrondkleur
$this->image = imagecreatetruecolor($x * 20 + $this->space + 20, $y * 20 + $this->space + 20);
// zwarte kleur
$black = imagecolorallocate($this->image, 255, 255, 255);
// groene kleur
$green = imagecolorallocate($this->image, 0, 255, 0);
// 0 op de y-as
$this->startpleky = $this->y * 20 + 20;
// 0 op de x-as
$this->startplekx = $this->space;
// maak de titel
$this->imagestringcentered($this->image, $this->space / 4, 1, $text, $green);
//start wat functies
$this->pngheader();
$this->yas();
$this->xas();
$this->cijfers();
imageantialias($this->image, 1);
}
// function imagestringcentered, zorgt ervoor dat de imagestring horizontaal in het middden blijft.
function imagestringcentered ($img, $y, $font, $text, $color)
{
while (strlen($text) * imagefontwidth($font) > imagesx($img)) {
if ($font > 1) {
$font--;
} else {
break;
}
}
$cy = $y;
imagestring($img, $font, imagesx($img) / 2 - strlen($text) * imagefontwidth($font) / 2, $cy, $text, $color);
}
// teken de y-as
function yas()
{
// witte kleur
$white = imagecolorallocate($this->image, 255, 255, 255);
// teken de lijn
imageline($this->image, $this->space, $this->space, $this->space, $this->y * 20 + 20, $white);
}
// teken de x-as
function xas()
{
// witte kleur
$white = imagecolorallocate($this->image, 255, 255, 255);
// teken de lijn
imageline($this->image, $this->space, $this->y * 20 + 20, $this->x * 20 + 20, $this->y * 20 + 20, $white);
}
// teken de streepjes en de cijfers
function cijfers()
{
// rode kleur (tekst kleur)
$txtkleur = imagecolorallocate($this->image, 255, 0, 0);
// blauwe kleur (lijn kleur)
$streepkleur = imagecolorallocate($this->image, 255, 255, 255);
// teken linksonder de 0
imagestring($this->image, 4, $this->space-10, imagesy($this->image) - $this->space, "0", $txtkleur);
$getaly = $this->y + 1;
// de horizontale lijntjes bij elke y as cijfer
for($i = 1; $i <= $this->y; $i++) {
imageline($this->image, $this->space-3, $i * 20, $this->space, $i * 20, $streepkleur);
$getaly--;
imagestring($this->image, 4, $this->space-10 * strlen($getaly * $this->steps), $i * 20-5, $getaly * $this->steps, $txtkleur);
}
// de horizontale lijntjes bij elke x-as-cijfer
for($i = 1; $i <= $this->x; $i++) {
imageline($this->image, $this->space + $i * 20, $this->y * 20 + 20, $this->space + $i * 20, $this->y * 20 + 20 + 3, $streepkleur);
imagestring($this->image, 4, $this->space + $i * 19, $this->y * 20 + 22, $i * $this->steps, $txtkleur);
}
}
// trek strepen van $x naar $y
function lineto($x, $y)
{
$x = $x / $this->steps;
$y = $y / $this->steps;
if (!isset($this->gy, $this->gx)) {
$lijnkleur = imagecolorallocate($this->image, 255, 0, 0);
imageline($this->image, $this->startplekx, $this->startpleky, $x * 20 + 20, $this->y * 20 + 20 - $y * 20, $lijnkleur);
$this->gx = $x * 20 + 20;
$this->gy = $this->y * 20 + 20 - $y * 20;
} else {
$lijnkleur = imagecolorallocate($this->image, 255, 0, 0);
imageline($this->image, $this->gx, $this->gy, $x * 20 + 20, $this->y * 20 + 20 - $y * 20, $lijnkleur);
$this->gx = $x * 20 + 20;
$this->gy = $this->y * 20 + 20 - $y * 20;
}
}
// creƫer de png
function init()
{
imagepng($this->image);
}
// maak de header
function pngheader()
{
header("Content-type: image/png");
}
// eind van het plaatje
function destroy()
{
imagedestroy($this->image);
}
}
// begin voorbeeld code
$grafiek = new grafiek(2, 7, 5, "php4purple grafiek");
$grafiek->lineto(1, 1);
$grafiek->lineto(2, 2.5);
$grafiek->lineto(3, 1);
$grafiek->lineto(4, 1.2);
$grafiek->lineto(5, 5);
$grafiek->lineto(6, 4.7);
$grafiek->lineto(7, 3.1);
$grafiek->init();
$grafiek->destroy();
?>
Reacties
0