Pie chart

Basics > Form > PieChart

int   NUMERO;
int   VALORI[];
float angoli[];
float xc, yc;
float raggio1, raggio2;
color colore;

void setup()
{
   size(500, 500);
   noStroke();
   smooth();
   ellipseMode(RADIUS);

   PFont font = loadFont("C16.vlw");
   textFont(font);
   textAlign(CENTER);
   
   xc=width/2;
   yc=height/2;
   raggio1=0.42*width;
   raggio2=0.47*width;

   esegui(); 
   noLoop();
}

void mousePressed()
{
   esegui();
   redraw();
}

void esegui()
{
    NUMERO=(int)random(5, 10);
    
    VALORI=new int[NUMERO];
    for(int i=0; i < NUMERO; i++)  
        VALORI[i]=(int)random(5, 50);
        
    int totale=0;
    for(int i=0; i < NUMERO; i++)
        totale += VALORI[i];
    float angolo=TWO_PI/totale;

    angoli=new float[NUMERO];
    for(int i=0; i < NUMERO; i++)
       angoli[i] = angolo*VALORI[i];
}

void draw()
{
   background(255); 
   float angolo=0;

   for (int i=0; i < NUMERO; i++)
   {
       colore=color(random(0, 128), random(0, 128), random(0, 128));
       fill(colore);       
       stroke(colore);
       arc(xc, yc, raggio1, raggio1, angolo, angolo+angoli[i]);
   
       text(VALORI[i], xc+raggio2*cos(angolo+angoli[i]/2),
                       yc+raggio2*sin(angolo+angoli[i]/2));
   
       angolo += angoli[i];
   }
}