grafiek
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?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();
?>
// 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();
?>