Create graf from data.csv

musmus
edited March 2017 in Library Questions

How to create graph Duration vs month from data.csv. Any can help me? my data as attachment2017-03-23_1710

Answers

  • Program as below and my output as attach. How to add duration in month 1?

    import org.gicentre.utils.stat.*; // For chart classes.

    // Simple scatterplot compating income and life expectancy.

    XYChart progressgraph;

    // Loads data into the chart and customises its appearance. void setup() { size(500,250);
    textFont(createFont("Arial",11),11);

    // Both x and y data set here.
    progressgraph = new XYChart(this);

    // Load in data from a file // (first line of file contains column headings). String[] data = loadStrings("data.csv"); float[] Month = new float[data.length-1]; float[] Duration = new float[data.length-1];

    for (int i=0; i<data.length-1; i++) { String[] Coloumn = data[i+1].split(","); Month [i] = Float.parseFloat(Coloumn[0]);
    Duration[i] = Float.parseFloat(Coloumn[1]); } 2017-03-23_1714

  • edited March 2017 Answer ✓

    Please edit your post, select your code and hit ctrl+o to format your code. Make sure there is an empty line above and below your code.

    Please also check the reference for an example of how to use split or splitTokens. Go To processing.org and then check under Reference.

    Kf

  • Dear kfrajer, below is my code

        import org.gicentre.utils.stat.*;     
    
       XYChart progressgraph;
    
        void setup()
        {
          size(500,250);   
          textFont(createFont("Arial",11),11);
    
          progressgraph = new XYChart(this);
    
    
          String[] data = loadStrings("Log.csv");
          float[] Month  = new float[data.length-1];
          float[] Duration = new float[data.length-1];
    
    
    
    
          for (int i=0; i<data.length-1; i++)
          {
            String[] Coloumn = data[i+1].split(",");
    
            Month [i]  = Float.parseFloat(Coloumn[0]);   
            Duration[i] = Float.parseFloat(Coloumn[1]); 
          }
    
    
          progressgraph.setData(Month ,Duration);
    
    
          progressgraph.showXAxis(true); 
          progressgraph.showYAxis(true); 
          progressgraph.setMinX(0);
          progressgraph.setMaxX(12);
          progressgraph.setMinY(0);
          progressgraph.setMaxY(100);
    
          progressgraph.setXAxisLabel("\n EXERCISE MONTH");
          progressgraph.setYAxisLabel("EXERCISE DURATION (Minit)\n");
    
    
          progressgraph.setPointColour(color(180,50,50,100));
          progressgraph.setPointSize(5);
          progressgraph.setLineWidth(2);
        }
    
    
        void draw()
        {
          background(255);
    
          textSize(12);
          progressgraph.draw(20,20,width-40,height-40);
    
          fill(#1724FF);
          textSize(15);
          text("GRAF FOR PATIENT EXERCISE PROGRESS VS MONTH ", 70,30);
        }
    
  • i want like below picture and x axis is show 1,2,3,4,5,6,7,8,9,10,11,12

  • edited March 2017

    Next assumes your data from your csv file is sorted.

    Add this to the top of your sketch so to have global scope:

    FloatList monthAcum;
    FloatList durationAcum;
    float currMonth=-1;
    

    Add next in setup:

    monthAcum = new FloatList();
    durationAcum = new FloatList();
    

    Replace lines 20 to 26 for this modified version:

    for (int i=0; i<data.length-1; i++)  //Skipping file header
    {
      String[] Coloumn = data[i+1].split(" ");
      println(i);
      println(Coloumn);
    
      Month [i]  = Float.parseFloat(Coloumn[0]);  
      Duration[i] = Float.parseFloat(Coloumn[1]);
    
      if (currMonth==Month[i]) {
        durationAcum.set(durationAcum.size()-1,durationAcum.get(durationAcum.size()-1)+Duration[i]);
      } else {
        currMonth=Month[i];
        monthAcum.append(Month[i]);
        durationAcum.append(Duration[i]);
      }
    }
    
    //Printing array's contents:
    for(int i=0;i<monthAcum.size();i++){
    println(monthAcum.get(i) + "\t" + durationAcum.get(i));
    }
    

    The information you want is displayed in the console. For more information about manipulating ArrayList, check the reference under the following entries: ArrayList, FloatList.

    Kf

  • Dear kfrajer, so this code is like that right.but the display still same

    import org.gicentre.utils.stat.*;    
    
    XYChart progressgraph;
    FloatList monthAcum;
    FloatList durationAcum;
    float currMonth=-1;
    
    void setup()
    {
      size(500,250);   
      textFont(createFont("Arial",11),11);
    
      progressgraph = new XYChart(this);
      monthAcum = new FloatList();
      durationAcum = new FloatList();
    
      String[] data = loadStrings("Log.csv");
      float[] Month  = new float[data.length-1];
      float[] Duration = new float[data.length-1];
    
    for (int i=0; i<data.length-1; i++)  //Skipping file header
    {
      String[] Coloumn = data[i+1].split(",");
        println(i);
        println(Coloumn);
    
      Month [i]  = Float.parseFloat(Coloumn[0]);  
      Duration[i] = Float.parseFloat(Coloumn[1]);
    
      if (currMonth==Month[i]) {
        durationAcum.set(durationAcum.size()-1,durationAcum.get(durationAcum.size()-1)+Duration[i]);
      } else {
        currMonth=Month[i];
        monthAcum.append(Month[i]);
        durationAcum.append(Duration[i]);
      }
    }
    
    //Printing array's contents:
    for(int i=0;i<monthAcum.size();i++){
    println(monthAcum.get(i) + "\t" + durationAcum.get(i));
    }
    
    
      progressgraph.setData(Month ,Duration);
    
    
      progressgraph.showXAxis(true); 
      progressgraph.showYAxis(true); 
      progressgraph.setMinX(0);
      progressgraph.setMaxX(12);
      progressgraph.setMinY(0);
      progressgraph.setMaxY(100);
    
      progressgraph.setXAxisLabel("\n EXERCISE MONTH");
      progressgraph.setYAxisLabel("EXERCISE DURATION (Minit)\n");
    
    
      progressgraph.setPointColour(color(180,50,50,100));
      progressgraph.setPointSize(5);
      progressgraph.setLineWidth(2);
    }
    
    
    void draw()
    {
      background(255);
    
      textSize(12);
      progressgraph.draw(20,20,width-40,height-40);
    
      fill(#1724FF);
      textSize(15);
      text("GRAF FOR PATIENT EXERCISE PROGRESS VS MONTH ", 70,30);
    }
    
  • Answer ✓

    Replace this line 45 in your previous post with progressgraph.setData(monthAcum.array() ,durationAcum.array());

    Kf

  • Thank you very much kfrajer.Now i get what i want.Thank you

Sign In or Register to comment.