# PNG Piecharts

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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/unid/EPSD-5DTT4L?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.

Anyone done this before?

## Re: PNG Piecharts

Alfred Whitney wrote:

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,

\$startx = 0, \$starty = 0){
if(round(\$radius,0) <= 0 ||  <= 0){
return false;
}
\$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,
L, A, 0 "
\$sum += \$value;
\$long_arc = (\$rad > M_PI )? '1','0';
\$path .= ",1 , Z";
\$return[] = \$path;
}
return \$return;
}

--
Grtz,

Rik Wasmus

## Re: PNG Piecharts

Rik wrote:

Well, that might have benifited from a simple test :-).
Errors fixed:
\$startx = 0, \$starty = 0){
return false;
}
\$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,
L, A, 0 ";
\$sum += \$value;
\$long_arc = (\$rad > M_PI )? '1':'0';