[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

imagefilledarc - Ugly code to create a pie chart

Posted on 2004-08-17
5
Medium Priority
?
320 Views
Last Modified: 2009-12-16
This is really ugly, but it seems to work. Now how to improve it???
I pull an array from my db called $graphValues, and pull a sum to get a $total - the $total will represent 360 degrees of the pie chart:

     // Figure out start and end points for each graph segment
     $degperhit = 360 / $total;                     //  360 / Total # of hits = # of degrees per unit

Then I turn $graphValues into a temp array so I can screw around with it, ie. Figure out start and end values for imagefilledarc by turning the sum of the actual values into how many degrees of the circle it will be, rounded down with 'floor', and then drop a value and do it again.

     $tempValArray = $graphValues;                      // temp array to work with for segment start and end values
     for($i = count($tempValArray); $i > 0; $i--){
      $almostStart[] = floor(array_sum($tempValArray)*$degperhit);
      array_pop($tempValArray);
      }
     $almostStart = array_reverse($almostStart);
This will give me end values as is.

           $endArray = $almostStart;

Start values set by adding a 0 to the front of the array, then dropping the last value, which will always be 360

      array_unshift($almostStart, "0");
      array_pop($almostStart);

      $startArray = $almostStart;
------------------------------------------------------

This is pretty darn ugly right? Any thoughts on a 'cleaner' or more efficient way of doing it???






0
Comment
Question by:winglis4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 5

Author Comment

by:winglis4
ID: 11822644
A little more,
then I draw the chart with this:
(image is 400x400)


for ($i=0; $i < count($endArray); $i++){
imagefilledarc($image, 200, 200, 380, 240, $startArray[$i],  $endArray[$i], $colorArray[$i], IMG_ARC_PIE);
}
0
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 1000 total points
ID: 11822982
Have you look into JPGraph?  Might be easier than doing this all the hard way.
http://www.aditus.nu/jpgraph/index.php
0
 
LVL 5

Author Comment

by:winglis4
ID: 11823068
Yeah, I really want to try it on my own (well, almost on my own, hehe) as a learning experience. I've done some bar graphs, but wanted to stretch my brain and see if I could get the same data into a pie chart. I may get JPGraph for the 'real' production of my graphs, though.

I really was psyched that I figured out how to get the db values into degrees, and how to get it all into a format that imagefililedarc could use. It ain't pretty but it works!!! :-)
0
 
LVL 25

Assisted Solution

by:Marcus Bointon
Marcus Bointon earned 1000 total points
ID: 11828653
I can vouch for JPGraph too. This is also a very nice solution - not nearly as flexible, but much better looking:

http://www.maani.us/charts/index.php?menu=Introduction
0
 
LVL 5

Author Comment

by:winglis4
ID: 11837938
I just played around with JPGraph a bit - not bad!!! :-)
I gotta give props to Squinky, though. Those Flash graphs look too good!!!!

Thanks fellas.
winglis4
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question