Cirkeldiagram
Er gaat alleen iets fout en ik kan er maar niet achter komen hoe dit kan.
Je maakt dus een instantie van de klasse, en doet steeds de kleur instellen en daarna de hoek van de taartpunt.
Ik krijg de volgende afbeelding eruit:
Bij de tweede punt gaat het mis. Je ziet een wit stuk tussen de groene en blauwe punt. Dit zal wel door een reken fout zijn.
Als je een taartpunt in de diagram erbij wil, en je geeft het aantal graden, dan worden deze graden relatief aan de vorige taartpunt gezien.
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
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
<?PHP
// piechart.class.php
/*
* Class for creating pie charts
*/
class Piechart {
private $_image;
private $_width;
private $_height;
private $_center_x;
private $_center_y;
private $_arc_width;
private $_arc_height;
private $_arc_start;
private $_arc_color;
private $_arc_count;
private $_effect_thickness;
public function __construct($width = 200, $height = 200) {
$this->_width = $width;
$this->_height = $height;
$this->_image = imagecreate($width, $height);
$white = imagecolorallocate($this->_image, 255, 255, 255);
imagefill($this->_image, 0, 0, $white);
$this->_center_x = intval($width / 2);
$this->_center_y = intval($height / 2);
$this->_arc_width = $width;
$this->_arc_height = intval($height / 2);
$this->_arc_start = 0;
$this->_arc_color = imagecolorallocate($this->_image, 255, 0, 0);
$this->_arc_count = 0;
// effect
$this->_effect_thickness = 10;
}
public function setArcColor($r, $g, $b) {
$this->_arc_color = imagecolorallocate($this->_image, $r, $g, $b);
}
// Makes it possible to get the color also from outside the class
public function getArcColor() {
return $this->_arc_color;
}
private function _getArcStart() {
return $this->_arc_start;
}
public function setEffectThickness($thickness) {
$this->_effect_thickness = $thickness;
}
private function _createArcEffect() {
for($i = $this->_effect_thickness + $this->_arc_height; $i > $this->_arc_height; $i--) {
}
}
public function createArc($degrees) {
$this->_createArcEffect();
imagefilledarc($this->_image,
$this->_center_x, // Center point x of the image, x of arc
$this->_center_y, // Center point y of the image, y of arc
$this->_arc_width, // Width of arc, relative to the x of the arc
$this->_arc_height, // Height of arc, relative to the y of the arc
$this->_arc_start, // Start degrees of arc, this is the end degrees of the previous arc
$degrees, // End degrees of the arc, given as paramater
$this->_arc_color, // Color of arc
IMG_ARC_PIE // Style
);
$this->_arc_start = $this->_arc_start + $degrees;
$this->_arc_count++;
}
public function getArcCount() {
return $this->_arc_count;
}
public function getImage() {
return $this->_image;
}
}
$p = new Piechart;
$p->createArc(40);
$p->setArcColor(0, 255, 0);
$p->createArc(50);
$p->setArcColor(0, 0, 255);
$p->createArc(200);
// flush image
header('Content-type: image/png');
imagepng($p->getImage());
imagedestroy($p->getImage());
?>
// piechart.class.php
/*
* Class for creating pie charts
*/
class Piechart {
private $_image;
private $_width;
private $_height;
private $_center_x;
private $_center_y;
private $_arc_width;
private $_arc_height;
private $_arc_start;
private $_arc_color;
private $_arc_count;
private $_effect_thickness;
public function __construct($width = 200, $height = 200) {
$this->_width = $width;
$this->_height = $height;
$this->_image = imagecreate($width, $height);
$white = imagecolorallocate($this->_image, 255, 255, 255);
imagefill($this->_image, 0, 0, $white);
$this->_center_x = intval($width / 2);
$this->_center_y = intval($height / 2);
$this->_arc_width = $width;
$this->_arc_height = intval($height / 2);
$this->_arc_start = 0;
$this->_arc_color = imagecolorallocate($this->_image, 255, 0, 0);
$this->_arc_count = 0;
// effect
$this->_effect_thickness = 10;
}
public function setArcColor($r, $g, $b) {
$this->_arc_color = imagecolorallocate($this->_image, $r, $g, $b);
}
// Makes it possible to get the color also from outside the class
public function getArcColor() {
return $this->_arc_color;
}
private function _getArcStart() {
return $this->_arc_start;
}
public function setEffectThickness($thickness) {
$this->_effect_thickness = $thickness;
}
private function _createArcEffect() {
for($i = $this->_effect_thickness + $this->_arc_height; $i > $this->_arc_height; $i--) {
}
}
public function createArc($degrees) {
$this->_createArcEffect();
imagefilledarc($this->_image,
$this->_center_x, // Center point x of the image, x of arc
$this->_center_y, // Center point y of the image, y of arc
$this->_arc_width, // Width of arc, relative to the x of the arc
$this->_arc_height, // Height of arc, relative to the y of the arc
$this->_arc_start, // Start degrees of arc, this is the end degrees of the previous arc
$degrees, // End degrees of the arc, given as paramater
$this->_arc_color, // Color of arc
IMG_ARC_PIE // Style
);
$this->_arc_start = $this->_arc_start + $degrees;
$this->_arc_count++;
}
public function getArcCount() {
return $this->_arc_count;
}
public function getImage() {
return $this->_image;
}
}
$p = new Piechart;
$p->createArc(40);
$p->setArcColor(0, 255, 0);
$p->createArc(50);
$p->setArcColor(0, 0, 255);
$p->createArc(200);
// flush image
header('Content-type: image/png');
imagepng($p->getImage());
imagedestroy($p->getImage());
?>
Hoe kan ik dit probleem oplossen?
Ik denk dat het komt doordat de variabele _arc_start nog 0 is bij de tweede arc, maar dat zou niet moeten kunnen, want na het maken van de eerste arc, bevat deze variabele al het aantal graden van de vorige (eerste) arc.
Gewijzigd op 24/08/2013 18:40:32 door Mark Hogeveen
Bedankt! Nu werkt het. Het was dus toch een rekenfout.