/** * creates pie chart of given data * @param int $ex excentricity (width) * @param array $data data input * @param array $dnames data field names * @param array $dcolors colros of data fields * */ public function pieChart($data, $dnames, $colors, $addPercent = null, $width = null, $padding = null) { $this->colors = $colors; $this->dataN = $dnames; $this->data = $data; if ($addPercent == "true") { $addPercent = true; } else { $addPercent = false; } if (isset($width)) { $this->width = $width; } else { $this->width = 200; } if (isset($padding)) { $this->padding = $padding; } else { $this->padding = 10; } // 100 % = how much? $valueSum = array_sum($this->data); $converted = array(); $convertedDeg = array(); $convertedArc = array(); $radius = $this->width / 2; $center = $radius; // INIT Image parent::Init($this->width, $this->width, "bars"); // whats the percentual contingent of each dataPart? foreach ($data as $dataPart) { $procentual = round($dataPart * (100 / $valueSum), 2); $converted[] = $procentual; } // 100% of circles' degress = 360; $degSum = 360; $degDone = 0; // used space should be skipped $last = 0; foreach ($converted as $dataPart) { $deg = $dataPart * ($degSum / 100); $deg += $degDone; $fdeg = $last + ($deg - $last) / 2; $xy = self::getXYbogen($deg, $radius, $this->width); $fxy = self::getXYbogen($fdeg, $radius / 2, $this->width); $fontDeg[] = $fxy; $convertedDeg[] = $xy; $degDone = $deg; $last = $deg; } parent::imgFill("white", 1, 1); parent::imgEllipse($center, $center, $this->width, $this->width, "black"); foreach ($convertedDeg as $xy) { parent::imgLine($center, $center, $xy[0], $xy[1], "black"); } $i = 0; foreach ($fontDeg as $field) { $fontxy = $fontDeg[$i]; if (isset($this->colors) && isset($this->colors[$i])) { $c = $this->colors[$i]; parent::imgFill($c, $fontxy[0], $fontxy[1], "black"); } $i++; } $i = 0; foreach ($fontDeg as $field) { $txt = $dnames[$i]; if ($addPercent) { $txt .= " (" . $converted[$i] . "%)"; } $fontxy = $fontDeg[$i]; $hpl = imagefontheight(2) / 2; $wpl = imagefontwidth(2) * (strlen($txt) / 2); @parent::imgString(2, $fontxy[0] - $wpl, $fontxy[1] - $hpl, $txt, "black"); $i++; } if (isset($this->padding)) { parent::Init($this->width + $this->padding, $this->width + $this->padding, "container"); parent::imgFill("white", 1, 1); parent::mergeHandles("container", "bars", $this->padding / 2, $this->padding / 2); parent::switchHandle("container"); $center = ($this->width + $this->padding) / 2; parent::imgEllipse($center, $center, $this->width, $this->width, "black"); } }