We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Help with Pie chart example.
Page Index Toggle Pages: 1
Help with Pie chart example. (Read 1250 times)
Help with Pie chart example.
Nov 3rd, 2009, 6:59am
 
I'm trying to adapt the pie chart example form the learning area to work from an array of 3 changing values, but I'm having trouble making my pie chart draw itself as a full circle before diviing itself proportionally, it always seems to draw an incomplete circle..?

Here is the example, an easy way to see what I mean is just to delete some of the values from the string - the other slices dont change, sections just disappear!

Code:


size(200, 200);
background(100);
smooth();
noStroke();

int diameter = 150;
int[] angs = {30, 10, 45, 35, 60, 38, 75, 67};
float lastAng = 0;

for (int i = 0; i < angs.length; i++){
fill(angs[i] * 3.0);
arc(width/2, height/2, diameter, diameter, lastAng, lastAng+radians(angs[i]));
lastAng += radians(angs[i]);
}



If someone can how me how to alter this to draw a full-circle divided prportionally between the string values I might be able to get my own working, cheers L.
Re: Help with Pie chart example.
Reply #1 - Nov 3rd, 2009, 8:03am
 
There is no 'string'...

Perhaps you mean the int array 'angs'?  Notice that the numbers add up to 360: the number of degrees in a full circle.  If you alter the values in the array you obviously need to ensure they add up to a full circle.  If that doesn't answer the question maybe you should post your own code rather than the example you're working from...
Re: Help with Pie chart example.
Reply #2 - Nov 3rd, 2009, 8:21am
 
pretty easy, instead of declaring the angle and add them up to 360. think of them as percentage and add them up to 100... now only adjust the code by multiplying the angle by 3.6

size(200, 200);
background(100);
smooth();
noStroke();

int diameter = 150;
int[] angs = {30,40,10,20};
float lastAng = 0;

for (int i = 0; i < angs.length; i++){
 fill(angs[i] * 3.0);
 arc(width/2, height/2, diameter, diameter, lastAng, lastAng+radians(angs[i]*3.6));
 lastAng += radians(angs[i]*3.6);  
}
Re: Help with Pie chart example.
Reply #3 - Nov 4th, 2009, 8:58am
 
Or even better it's uploaded here now: http://samhumphrey.co.uk/applet/pie.html
Re: Help with Pie chart example.
Reply #4 - Nov 4th, 2009, 9:01am
 
Ladle wrote on Nov 4th, 2009, 6:21am:
Ok well that's sort-of helped! I have now managed to get my pie chart to draw itself ina circle, but it has illuminated somethign else going on that I dont quite understand!

I cant upload my sketch at the moment, but here is my full code (and some pasted data from the datafile to make it work), I can see many many 'slices of pie' appearing all the time when the mouse is pressed, instead of just 3 with changing values! - This is my first real experiment using code like this so im not quite sure what i'm seeing..

Is my sketch drawing out 3 new 'slices' each frame over the top of the last I'm not sure what I should be trying to learn to fix it :- Thanks!


Got it uploaded here: http://samhumphrey.co.uk/applet/pie.html
Re: Help with Pie chart example.
Reply #5 - Nov 4th, 2009, 9:31am
 
as far as i can tell without running your sketch is, that the problem is that you dont call a background on click. and just overdraw the next pie chart with an alpha value of 10. so you just overlay another transparent pie chart.  Just try to add background(255);
Page Index Toggle Pages: 1