Alfred Whitney wrote:
I've got a condition where I can't use pear or other add-ons on this
server and I need to draw SVG piecharts in PHP, save the file, shell
out and convert with ImageMagick to PNG, and display them. I've got
the ImageMagick 'convert' command down with...
convert /tmp/tmp1.svg /tmp/tmp1.png
...and I have found an excellent SVG piechart example here:
http://www.codestore.net/store.nsf/u...L?OpenDocument
(See first SVG sample code)
...but I can't figure out how to compute the L (lineto) and the ending
of the A (arc) values. I mean, you can't just choose arbitrary numbers
or you end up with a wobbly mess. That's where sine and cosine come
in.
Yes, and that is explained fairly well in the rest of the article? Read it
and you'll know.
Then again, it's been a while since I had to do math, so, as an exercise,
your answer:
function create_pie_chart_svg_paths($values, $radius = 180, $padding = 20,
$startx = 0, $starty = 0){
$padding = round($padding,0);
$radius = round($radius,0);
if(round($radius,0) <= 0 || <= 0){
trigger_error('Radius an padding should be higher then zero');
return false;
}
$centre = array('x' =$radius + $padding + $startx, 'y' =$radius +
$padding + $starty);
$start = array ('x' =$padding + $startx,'y' =$center['y'] +
$starty);
$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;
}
--
Grtz,
Rik Wasmus