Transforming data into a pie chart

edited December 2013 in Questions about Code

Hi I am new to processing.

I am trying to transform data into a pie chart. When I am trying to replace "angles"(point 4) with "grad" (point 3) in the pie chart function I get following error message: "grad cannot be resolved to a variable".

Should I make the variable "grad" to a global variable and how do I do that?

Thank in advance and sorry about the german comments.

Answers

  • edited December 2013
    void setup(){
      size(500,500);
      noStroke();
      noLoop();  // Run once and stop
    }
    
    
    float[] mean_5y_All = {0.2, 15, 12, 13, 13, 22,23,40};  //Mittelwerte einzelner Bezirke über 5 Jahre ; A1) MUSS noch eingelesen werden!) 
    int[] angles = { 30, 10, 45, 35, 60, 38, 75, 67 };   // Werte für Kreisdiagramm müssen draußen bleiben ;) 
    
    
    void draw(){
      background(255); //Farbe des Hintergrunds wird festgelegt
      pieChart(300, angles);
    
    
    //------- 1. Elemente von "kreisDaten" summieren 
      float summe = 0;
      for(int i=0; i<mean_5y_All.length; i++){
         summe += mean_5y_All[i];    //alle Elemente i werden addiert und in "summe" gespeichert
      }
       println("Summe=  "+summe);
    
    
    //------ 2. Wieviel Prozent sind 1 Datenpunkt 
      float datenpunkt =0;
        datenpunkt= 100/summe;
    
      float prozent[] = new float[8];   //A3: Arraygröße muss dynamisch sein !
    
      for(int i=0; i<mean_5y_All.length; i++){
    
        prozent[i] = mean_5y_All[i]* datenpunkt;  
        //float kreisWinkel[] ={};
        //kreisWinkel = ;
      }
        println(prozent);
    
    // ------- 3. Array Werte i mit   3,6 multiplizieren/ multiplying results wth 3.6
    
        float grad [] = new float[8];  //A3: Arraygröße muss dynamisch sein !
        float dreik6= 3.6;            // Prozente werden mit diesem Wert multipliziert und ergeben zusamen 360°
    
        for(int i=0; i<prozent.length; i++){
           grad[i]= prozent[i]* dreik6;
        }
         println("Winkel");
         println(grad);
    
     //------- 4. Funktion für Kreisdiagramm  //Function of the pie chart   
    }
    
     /*
      void pieChart(float diameter, int[] data) {    //http://processing.org/examples/piechart.html
        float lastAngle = 0;
        for (int i = 0; i < data.length; i++) {
          float gray = map(i, 0, data.length, 0, 255);
          fill(gray);
          arc(width/2, height/2, diameter, diameter, lastAngle, lastAngle+radians(angles[i]));
          lastAngle += radians(angles[i]);
        }
      }
    */
     void pieChart(float diameter, int[] data) {    //http://processing.org/examples/piechart.html
        float lastAngle = 0;
        for (int i = 0; i < data.length; i++) {
          float gray = map(i, 0, data.length, 0, 255);
          fill(gray);
          arc(width/2, height/2, diameter, diameter, lastAngle, lastAngle+radians(grad[i]));
          lastAngle += radians(grad[i]);
        }
      }
    
  • (please format code (Ctrl k). I'd do it but am using phone and can't highlight blocks)

  • edited December 2013

    I have formatted the code for you. Please change the topic title to something more descriptive. I have moved the topic to "Questions about Code"... this forum would be a nightmare to navigate if every topic title were named in a similar fashion to this one, seeing as a large percentage of the questions asked here deal with code.

  • Answer ✓

    If I understand what you are trying to do, I think you just need to replace all instances of "angles" to "grad". I see on line 9 where you create the angles array. It looks like you may just need to change that to "grad" also then you should be good to go.

    You can use Edit > Find and Replace to change variables across your whole sketch.

    Hope this helps! ak

  • Yes , thank you akiersky! It worked ;). @calsign: thank you for formatting and moving the topic!

    Cheers

Sign In or Register to comment.