473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

PNG Piecharts

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.

Anyone done this before?

Oct 22 '06 #1
2 1356
Rik
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
Oct 23 '06 #2
Rik
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
Oct 23 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Erwin Moller | last post by:
Hi group, I need to create a bunch of images on the fly, like Piecharts and nice tables. I can create images with imagecreate(450, 100) and draw them by hand. (imagecreatetruecolor() doesn't...
4
by: Paul Keegstra | last post by:
Hello, I am working on a configurator application that needs to have a changing visual representation of the product being configured. I have a base image representing the 'core' requirements...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.