header('Content-type: image/png'); imagepng($this->image); } } // graph the data using the associated colors public function graphData($data, $colors) { // allocate black for slice outline $black = imagecolorallocate($this->image, 0x0, 0x0, 0x0); // sum of all values $sum = array_sum($data); // starting angle of pie slice $start = -90; for ($i = 0; $i < count($data); $i++) { $color = imagecolorallocate($this->image, $colors[$i]['r'], $colors[$i]['g'], $colors[$i]['b']); // stop angle of pie slice $stop = 100 * $data[$i] / $sum * 3.6 + $start; // draw arc twice - once for filled area and again for outline imagefilledarc($this->image, $this->center, $this->center, $this->width, $this->width, $start, $stop, $color, IMG_ARC_PIE); imagefilledarc($this->image, $this->center, $this->center, $this->width, $this->width, $start, $stop, $black, IMG_ARC_NOFILL | IMG_ARC_EDGED); // increment to next starting point $start = $stop; } } } $data = array(150, 302, 250); $colors = array(array('r' => 0x33, 'g' => 0xcc, 'b' => 0xff), array('r' => 0xff, 'g' => 0x33, 'b' => 0xcc), array('r' => 0xcc, 'g' => 0xff, 'b' => 0x33)); $chart = new PieChart(150); $chart->graphData($data, $colors); $chart->flushImage();