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 angles from text file
Page Index Toggle Pages: 1
help with pie angles from text file (Read 327 times)
help with pie angles from text file
Jan 8th, 2009, 9:01am
 
I am having trouble figuring out a clean way to do what I'm after. I have a text file with 70 or so CSV entries in which there are 5 columns whose numbers cumulatively equal 100 and I am intending to use these to create wedges in a pie chart.

is there a better way to do this than : cycling through each entry, assigning each portion of that entry's pie to a variable, which is then passed to an array of numbers, From there, I would pass that array to a function associated with a pie class. I guess it seems like alot of steps and I feel like it's pretty clumsy.

I could easily concatenate these numbers in excel or format them in a different way, but I'm not sure what the bet method is.

any ideas?

Re: help with pie angles from text file
Reply #1 - Jan 9th, 2009, 1:32am
 
here's what I am working with in my TSV:

trees water grass soil rock
10 45 35 0 10
25 40 20 15 0
0 50 10 20 20

and so on. And I want to insert each group of 5 integers into an array of integers, so that the first one would be something like:

int[] pct = new int[5];
pct[1] = {10, 45, 35, 0, 10}; (well it wouldnt be like this in the code because those entries would be fed into it...)

Re: help with pie angles from text file
Reply #2 - Jan 9th, 2009, 11:06am
 
I would make instead a specialized PieChart class, with perhaps 5 fields for each value. It might include a method to parse a line and feed these fields, and another to draw the chart.
I might do a prototype later, if I find time.
Re: help with pie angles from text file
Reply #3 - Jan 10th, 2009, 11:41am
 
I used a non-specialize PieChart class, after all.
Code:
// Hard-coded here, should be read from a file
String[] lines =
{
 "10 45 35 0 10",
 "25 40 20 15 0",
 "0 50 10 20 20 ",
 "20 15 20 25 20",
 "5 10 15 20 25",
 "5 7 11 13 17 2 11 3 5 7 19"
};

PieChart[] pieCharts;

void setup()
{
 size(800, 200);
 pieCharts = new PieChart[lines.length];
 for (int i = 0; i < lines.length; i++)
 {
   pieCharts[i] = new PieChart(lines[i]);
 }
}

void draw()
{
 for (int i = 0; i < pieCharts.length; i++)
 {
   pushMatrix();
   translate(100 + i * 120, height/2);
   scale(100);
   pieCharts[i].display();
   popMatrix();
 }
}

class PieChart
{
 float[] values;
 color[] colors =
 {
   #FF0000, #00FF00, #0000FF, #FFFF00, #00FFFF,
   // Yes, there are slightly too much colors!
   #FF00FF, #000000, #FFFFFF
 };

 PieChart(String tsvValues)
 {
   String[] sValues = split(tsvValues, " ");
   values = new float[sValues.length];
   float total = 0;
   for (int i = 0; i < sValues.length; i++)
   {
     values[i] = float(sValues[i]);
     total += values[i];
   }
   if (total < 99.99 || total > 100.01) // Tolerate float rounding errors...
   {
     println("Warning! Incorrect total of values: " + total);
   }
 }

 void display()
 {
   noStroke();
   float startAngle = 0;
   for (int i = 0; i < values.length; i++)
   {
     fill(colors[i % colors.length]);
     // Angle proportional to value
     float pieAngle = (values[i] / 100) * TWO_PI;
     // Use normalized values, scale and translate
     // will give the wanted size/position
     arc(0, 0, 1, 1, startAngle, startAngle + pieAngle);
     startAngle += pieAngle;
   }
 }
}
Page Index Toggle Pages: 1