|  | Posted by Rik on 10/23/06 16:33 
Rik wrote:> <some code>
 
 Well, that might have benifited from a simple test :-).
 Errors fixed:
 function create_pie_chart_svg_paths($values, $radius = 180, $padding = 20,
 $startx = 0, $starty = 0){
 $padding = round($padding,0);
 $radius = round($radius,0);
 if($radius <= 0 ||  $padding <= 0){
 trigger_error('Radius an padding should be higher then zero');
 return false;
 }
 $center = array('x' => $radius + $padding + $startx, 'y' => $radius +
 $padding + $starty);
 $start = array ('x' => $padding + $startx,'y' => $center['y']);
 $total = array_sum($values);
 $value_to_rad = $total / (2 * M_PI);
 $return = array();
 $sum = 0;
 foreach($values as $value){
 $path = "M{$center['x']},{$center['y']}
 L{$start['x']},{$start['y']} A{$radius},{$radius} 0 ";
 $sum += $value;
 $rad = $value / $value_to_rad;
 $long_arc = ($rad > M_PI )? '1':'0';
 $totrad = $sum / $value_to_rad;
 $start = array('x' => ($center['x'] - (cos($totrad) * $radius)),
 'y' => ($center['y'] - (sin($totrad) * $radius)));
 $path .= "{$long_arc},1 {$start['x']},{$start['y']} Z";
 $return[] = $path;
 }
 return $return;
 }
 
 --
 Rik Wasmus
  Navigation: [Reply to this message] |