Label PieChart

resres
edited April 2018 in Questions about Code

Hi, i hope somebody could help me because i should do the Project for my work. I created a pie Chart but i dont know how i can place the Label.

In the middle of the arc (outside the Circle) there should be the Label.

for (Rohtipps g : myRohtipp) {

    //PVector c = randomVector(mapped);
    //draw ellipse
    PVector loc = g.getLocation();
    float dia = g.getDiameter();
    //noFill();

    float[] data={g.getColdlead(), g.getWarmlead(), g.getHotlead()};

    //float[] data={50.0, 50.0, 50.0, 50.0};
    float sumdata=0;
    for (int i=0; i<data.length; i++) {
      sumdata+=data[i];
    }
    //println(sumdata);
    sumdata = 360/sumdata;

    for (int i=0; i<data.length; i++) {
      data[i] = sumdata*data[i];
    }
    float lastAngle=0;

    for (int i=0; i<data.length; i++) {
       float col=map(i, 0, data.length, 20, 255);
       fill(55, 150, col, col+100);
       noStroke();
       arc(loc.x, loc.y, dia, dia, lastAngle, lastAngle+radians(data[i]));
       lastAngle +=radians(data[i]);

    }

    //println(g.getKampagne()+" "+g.getColdlead()+" "+g.getWarmlead()+" "+g.getHotlead());
    //pieChart(dia, loc.x, loc.y, data);
    //ellipse(loc.x, loc.y, dia, dia);
    fill(40, 40, 40);
    //label positionieren
    textSize(11);


    float tw=(textWidth(g.getKampagne()))/2;
    text(g.getKampagne(), loc.x-tw, loc.y+30);

    //println(g.getAnzahl());
    ts=ts-2;
    textSize(ts);
    fill(30, 30, 30);
    text(g.getKampagne()+" "+g.getAnzahl(), 1400, g.getPositionText());
    textSize(24);
    //println(g.getColdlead());
  }

Thanks Theresa

Answers

  • edited April 2018 Answer ✓

    The code you posted does not run. It is missing user-defined classes, and probably your data.

    Here is an example of a pie-chart with a label instead. Make sure you understand how this is drawn, and then use that understanding to draw your own pie chart and label.

    size(600,400);
    colorMode(HSB,255);
    background(0);
    for(int i = 0; i < 10; i++){
      fill(25*i,255,255);
      stroke(0);
      arc(200,200,350,350,TWO_PI/10.0*i,TWO_PI/10.0*(i+1) );
      rect(420,20+30*i,20,20);
      fill(255);
      text("Section " + i, 450, 36 + 30*i);
    }
    
  • Answer ✓
    size(1600, 900);
    colorMode(HSB, 255);
    background(0);
    
    for (int i = 0; i < 10; i++) {
    
      // set color 
      fill(25*i, 255, 255);
    
      // show arc 
      stroke(0);
      arc(width/2, height/2, 
        350, 350, 
        TWO_PI/10.0*i, TWO_PI/10.0*(i+1) );
    
      // show rects with text  
      rect(1420, 20+30*i, 20, 20);
      fill(255);
      textAlign(LEFT);
      text("Section " + i, 1450, 36 + 30*i);
    
      // text at arcs 
      float halfAngle = TWO_PI/10.0/3;
      float x1=width/2 + 210* cos(TWO_PI/10.0*i+halfAngle); 
      float y1=height/2  + 210* sin(TWO_PI/10.0*i+halfAngle);
      textAlign(CENTER); 
      text("Section " + i, 
        x1, y1);
    
      //
    }//for 
    
  • Thank you guys! Great!

Sign In or Register to comment.