From f54776e9bea7226c908a860943eab2969b371ec8 Mon Sep 17 00:00:00 2001 From: Jan Klemkow Date: Tue, 8 Jan 2013 14:50:15 +0100 Subject: Add piechart php program. --- piechart.php | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 piechart.php diff --git a/piechart.php b/piechart.php new file mode 100644 index 0000000..42b29f9 --- /dev/null +++ b/piechart.php @@ -0,0 +1,114 @@ + + + + Pie chart + Picture of a pie chart +END; + +print piechart($values,200,200,190); + +print "\n\n"; + + +/* + The piechart function + + Arguments are an aray of values, the centre coordinates x and y, and + the radius of the piechart. +*/ + +function piechart($data, $cx, $cy, $radius) { + $chartelem = ""; + + $max = count($data); + +// $colours = array('#4e9a06','#a40000','#204a87','#5c3566','#ce5c00'); + $colours = array('#73d216','#cc0000','#3465a4','#75507b','#f57900'); +// $colours = array('#8ae234','#ef2929','#729fcf','#ad7fa8','#fcaf3e'); + + $sum = 0; + foreach ($data as $key=>$val) { + $sum += $val; + } + $deg = $sum/360; // one degree + $jung = $sum/2; // necessary to test for arc type + + /* Data for grid, circle, and slices */ + + $dx = $radius; // Starting point: + $dy = 0; // first slice starts in the East + $oldangle = 0; + + /* Loop through the slices */ + for ($i = 0; $i<$max; $i++) { + $angle = $oldangle + $data[$i]/$deg; // cumulative angle + $x = cos(deg2rad($angle)) * $radius; // x of arc's end point + $y = sin(deg2rad($angle)) * $radius; // y of arc's end point + + $colour = $colours[$i]; + + if ($data[$i] > $jung) { + // arc spans more than 180 degrees + $laf = 1; + } + else { + $laf = 0; + } + + $ax = $cx + $x; // absolute $x + $ay = $cy + $y; // absolute $y + $adx = $cx + $dx; // absolute $dx + $ady = $cy + $dy; // absolute $dy + $chartelem .= "\n"; + $chartelem .= ""; + $dx = $x; // old end points become new starting point + $dy = $y; // id. + $oldangle = $angle; + } + + return $chartelem; +} + +?> -- cgit