simpele-barchart
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
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
<?php
if($_COOKIE['counter_time'] <> 'visited') {
setcookie('counter_time', 'visited', time() + (3600 * 24));
// Simple BarChart Counter by Richard van Velzen
// Het plaatje komt in een png of jpeg afbeelding te staan en wordt steed met 1 opgeteld
// Het werkt per maaand
// Hier openen we het bestand dat bij deze maand hoort in read modus...
$fopen = fopen(date('n'), 'r');
// ff inlezen
$teller = fread($fopen, 4096);
// en nu weer sluiten
fclose($fopen);
// we tellen bij het getal 1 op
$teller = $teller + 1;
// nu openen we het bestand weer, maar nu in write+ modus
$fopen = fopen(date('n'), 'w+');
// en we schrijven het erin
fwrite($fopen, $teller);
// en sluiten het weer af
fclose($fopen);
}
// een array van iedere maand en inhoud van het bijbehorende bestand
$data = array (
'Jan' => file_get_contents('1'),
'Feb' => file_get_contents('2'),
'Mar' => file_get_contents('3'),
'Apr' => file_get_contents('4'),
'May' => file_get_contents('5'),
'Jun' => file_get_contents('6'),
'Jul' => file_get_contents('7'),
'Aug' => file_get_contents('8'),
'Sep' => file_get_contents('9'),
'Oct' => file_get_contents('10'),
'Nov' => file_get_contents('11'),
'Dec' => file_get_contents('12')
);
// hoogte en breedte vaststellen
$width = 480;
$height = 250;
$image = imagecreate($width, $height);
// kleuren die worden gebruikt
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
// Nu een flink stuk code om er balken omheen te zetten, de titel neer te zetten,
// de waarden in de grafiek te zetten en de waarden op de y-as neer te zetten
$maxval = max($data);
$nval = sizeof($data);
$vmargin = 20;
$hmargin = 38;
$base = floor(($width - $hmargin) / $nval);
$ysize = $height - 2 * $vmargin;
$xsize = $nval * $base;
$titlefont = 3;
$title = 'Bezoekers van deze pagina';
$txtsz = imagefontwidth($titlefont) * strlen($title);
$xpos = (int)($hmargin + ($xsize - $txtsz)/2);
$xpos = max(1, $xpos);
$ypos = 3;
imagestring($image, $titlefont, $xpos, $ypos, $title , $black);
$labelfont = 2;
$ngrid = 4;
$dydat = $maxval / $ngrid;
$dypix = $ysize / ($ngrid + 1);
for($i = 0; $i <= ($ngrid + 1); $i++)
{
$ydat = (int)($i * $dydat);
$ypos = $vmargin + $ysize - (int)($i * $dypix);
$txtsz = imagefontwidth($labelfont) * strlen($ydat);
$txtht = imagefontheight($labelfont);
$xpos = (int)(($hmargin - $txtsz) / 2);
$xpos = max(1, $xpos);
imagestring($image, $labelfont, $xpos, $ypos - (int)($txtht / 2), $ydat, $black);
if(!($i == 0) && !($i > $ngrid))
{
imageline($image, $hmargin - 3, $ypos, $hmargin + $xsize, $ypos, $gray);
}
}
$padding = 3;
$yscale = $ysize / (($ngrid+1) * $dydat);
for($i = 0; list($xval, $yval) = each($data); $i++)
{
$ymax = $vmargin + $ysize;
$ymin = $ymax - (int)($yval * $yscale);
$xmax = $hmargin + ($i + 1) * $base - $padding;
$xmin = $hmargin + $i * $base + $padding;
imagefilledrectangle($image, $xmin, $ymin, $xmax, $ymax, $navy);
$txtsz = imagefontwidth($labelfont) * strlen($xval);
$xpos = $xmin + (int)(($base - $txtsz) / 2);
$xpos = max($xmin, $xpos);
$ypos = $ymax + 3;
imagestring($image, $labelfont, $xpos, $ypos, $xval, $black);
}
imagerectangle($image, $hmargin, $vmargin,
$hmargin + $xsize, $vmargin + $ysize, $black);
/* De mime-type vaststellen, kan ook jpeg zijn als je dat wil:
header("Content-type: image/jpeg");
imagejpeg($image);
*/
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
if($_COOKIE['counter_time'] <> 'visited') {
setcookie('counter_time', 'visited', time() + (3600 * 24));
// Simple BarChart Counter by Richard van Velzen
// Het plaatje komt in een png of jpeg afbeelding te staan en wordt steed met 1 opgeteld
// Het werkt per maaand
// Hier openen we het bestand dat bij deze maand hoort in read modus...
$fopen = fopen(date('n'), 'r');
// ff inlezen
$teller = fread($fopen, 4096);
// en nu weer sluiten
fclose($fopen);
// we tellen bij het getal 1 op
$teller = $teller + 1;
// nu openen we het bestand weer, maar nu in write+ modus
$fopen = fopen(date('n'), 'w+');
// en we schrijven het erin
fwrite($fopen, $teller);
// en sluiten het weer af
fclose($fopen);
}
// een array van iedere maand en inhoud van het bijbehorende bestand
$data = array (
'Jan' => file_get_contents('1'),
'Feb' => file_get_contents('2'),
'Mar' => file_get_contents('3'),
'Apr' => file_get_contents('4'),
'May' => file_get_contents('5'),
'Jun' => file_get_contents('6'),
'Jul' => file_get_contents('7'),
'Aug' => file_get_contents('8'),
'Sep' => file_get_contents('9'),
'Oct' => file_get_contents('10'),
'Nov' => file_get_contents('11'),
'Dec' => file_get_contents('12')
);
// hoogte en breedte vaststellen
$width = 480;
$height = 250;
$image = imagecreate($width, $height);
// kleuren die worden gebruikt
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
// Nu een flink stuk code om er balken omheen te zetten, de titel neer te zetten,
// de waarden in de grafiek te zetten en de waarden op de y-as neer te zetten
$maxval = max($data);
$nval = sizeof($data);
$vmargin = 20;
$hmargin = 38;
$base = floor(($width - $hmargin) / $nval);
$ysize = $height - 2 * $vmargin;
$xsize = $nval * $base;
$titlefont = 3;
$title = 'Bezoekers van deze pagina';
$txtsz = imagefontwidth($titlefont) * strlen($title);
$xpos = (int)($hmargin + ($xsize - $txtsz)/2);
$xpos = max(1, $xpos);
$ypos = 3;
imagestring($image, $titlefont, $xpos, $ypos, $title , $black);
$labelfont = 2;
$ngrid = 4;
$dydat = $maxval / $ngrid;
$dypix = $ysize / ($ngrid + 1);
for($i = 0; $i <= ($ngrid + 1); $i++)
{
$ydat = (int)($i * $dydat);
$ypos = $vmargin + $ysize - (int)($i * $dypix);
$txtsz = imagefontwidth($labelfont) * strlen($ydat);
$txtht = imagefontheight($labelfont);
$xpos = (int)(($hmargin - $txtsz) / 2);
$xpos = max(1, $xpos);
imagestring($image, $labelfont, $xpos, $ypos - (int)($txtht / 2), $ydat, $black);
if(!($i == 0) && !($i > $ngrid))
{
imageline($image, $hmargin - 3, $ypos, $hmargin + $xsize, $ypos, $gray);
}
}
$padding = 3;
$yscale = $ysize / (($ngrid+1) * $dydat);
for($i = 0; list($xval, $yval) = each($data); $i++)
{
$ymax = $vmargin + $ysize;
$ymin = $ymax - (int)($yval * $yscale);
$xmax = $hmargin + ($i + 1) * $base - $padding;
$xmin = $hmargin + $i * $base + $padding;
imagefilledrectangle($image, $xmin, $ymin, $xmax, $ymax, $navy);
$txtsz = imagefontwidth($labelfont) * strlen($xval);
$xpos = $xmin + (int)(($base - $txtsz) / 2);
$xpos = max($xmin, $xpos);
$ypos = $ymax + 3;
imagestring($image, $labelfont, $xpos, $ypos, $xval, $black);
}
imagerectangle($image, $hmargin, $vmargin,
$hmargin + $xsize, $vmargin + $ysize, $black);
/* De mime-type vaststellen, kan ook jpeg zijn als je dat wil:
header("Content-type: image/jpeg");
imagejpeg($image);
*/
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>