Loading...
Logo
Processing Forum
Hello peolple,

I´m just trying to get a life time plot from temperature values, for that I built a sketch on processing based. To simulate the temperature values I used the position of the mouse.

The problem is that I just get a point running on time, not a line like I want. I think the mistake is on drawDataLine() function (last function).

Somebody can help me with this??

Thanks in advance

Copy code
  1. float dataMin, dataMax;
  2. float plotX1, plotY1;
  3. float plotX2, plotY2;
  4. float labelX, labelY;


  5. int [] time = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
  6. int timeInterval = 1;
  7. int timeMin = 0;
  8. int timeMax = 24;
  9. int volumeInterval = 10;
  10. int volumeIntervalMinor = 5;

  11. float lineX = 120;

  12. PFont plotFont;

  13. void setup ()
  14. {
  15.   size(1000,500);
  16.   plotX1 = 120; 
  17.   plotX2 = width - 80;
  18.   labelX = 50;
  19.   plotY1 = 60;
  20.   plotY2 = height - 70;
  21.   labelY = height - 25;
  22.   dataMin = 0;
  23.   dataMax = ceil (50/volumeInterval)*volumeInterval;
  24.   plotFont = createFont("SansSerif", 20);
  25.   textFont(plotFont);
  26.   
  27.   smooth();
  28. }
  29.   
  30. void draw ()
  31. {
  32.   background(224);
  33.     
  34.   // Show the plot area as a white box  
  35.   fill(255);
  36.   rectMode(CORNERS);
  37.   noStroke();
  38.   rect(plotX1, plotY1, plotX2, plotY2);  
  39.   stroke(2);
  40.   smooth();
  41.   
  42.   drawTitle();
  43.   drawAxisLabels();
  44.   drawYearLabels();
  45.   drawVolumeLabels();
  46.   drawDataLine();

  47. }

  48. void drawTitle() {
  49.   fill(0);
  50.   textSize(20);
  51.   textAlign(LEFT);
  52.   String title = "Temperature";
  53.   text(title, plotX1, plotY1 - 10);
  54. }
  55.   
  56. void drawYearLabels() 
  57. {
  58.   fill(0);
  59.   textSize(10);
  60.   textAlign(CENTER);
  61.   
  62.   // Use thin, gray lines to draw the grid
  63.   stroke(224);
  64.   strokeWeight(1);
  65.   
  66.   for (int row = 0; row < 25; row++) {
  67.     if (time[row] % timeInterval == 0) {
  68.       float x = map(time[row], timeMin, timeMax, plotX1, plotX2);
  69.       text(time[row], x, plotY2 + textAscent() + 10);
  70.       line(x, plotY1, x, plotY2);
  71.     }
  72.   }
  73. }

  74.   void drawAxisLabels() 
  75. {
  76.   fill(0);
  77.   textSize(13);
  78.   textLeading(15);
  79.   
  80.   textAlign(CENTER, CENTER);
  81.   text("Graus Celsius\n (ºC)", labelX, (plotY1+plotY2)/2);
  82.   textAlign(CENTER);
  83.   text("Time (h)", (plotX1+plotX2)/2, labelY);

  84. void drawVolumeLabels() {
  85.   fill(0);
  86.   textSize(10);
  87.   textAlign(RIGHT);
  88.   
  89.   stroke(128);
  90.   strokeWeight(1);

  91.   for (float v = dataMin; v <= dataMax; v += volumeIntervalMinor) {
  92.     if (v % volumeIntervalMinor == 0) {     // If a tick mark
  93.       float y = map(v, dataMin, dataMax, plotY2, plotY1);  
  94.       if (v % volumeInterval == 0) {        // If a major tick mark
  95.         float textOffset = textAscent()/2;  // Center vertically
  96.         if (v == dataMin) {
  97.           textOffset = 0;                   // Align by the bottom
  98.         } else if (v == dataMax) {
  99.           textOffset = textAscent();        // Align by the top
  100.         }
  101.         text(floor(v), plotX1 - 10, y + textOffset);
  102.         line(plotX1 - 4, y, plotX1, y);     // Draw major tick
  103.       } else {
  104.         //line(plotX1 - 2, y, plotX1, y);     // Draw minor tick
  105.       }
  106.     }
  107.   }
  108. }

  109. void mousePressed() 
  110. {
  111.   println("Coordinates: " + mouseX +"," + mouseY);
  112. }


  113. void drawDataLine() {  
  114.   stroke(#5679C1);
  115.   strokeWeight(5);
  116.   noFill();
  117.   
  118.   if(pmouseY>60 && pmouseY<(height-70))
  119.   {
  120.   line (lineX,mouseY,lineX,pmouseY); 
  121.   lineX ++;
  122.   delay(20);
  123.   }
  124.   
  125.   if (lineX>width-80)
  126.   {
  127.     lineX=120;
  128.     background (255);
  129.    }
  130. }

Replies(8)

wow sorry i can't help you but that is a really impressive graph!!!

good luck... i guess my only help will be bumping your thread up for you :)
One problem, among others, is that you redraw the whole graph on each frame. That's OK, but your sketch has no memory of the previous data, so it can only draw the instant value.
You have to store all the coordinates, eg. making a PVector for each value, and storing them in an array list. Then after drawing the plot area, iterate on the values in the array list and draw lines between the coordinates.
Hi PhiLho,

I think you are right, but I'm not sure if I know how to do that...
I replaced the  void drawDataLine() function above by the following one, but the whole code doesn´t work anymore..
Can you help me please?

Is it something like this what you told me to do?

Thank you very much for your help
Copy code
  1. float [] coordinateX;
    float [] coordinateY; 

    void drawDataLine() 
      for (int lineX= 0; lineX<=1000; lineX++)
      {
        coordinateX [lineX] = lineX;
      }
      for (int a= 0; a <=1000; a++)
      {
        coordinateY [a] = pmouseY;
      }

      stroke(#5679C1);
      strokeWeight(5);
      noFill();
      
      for (int b = 0; b<1000; b++)
      {
        if(pmouseY>60 && pmouseY<(height-70))
        {
          line (0,mouseY,coordinateX [b],coordinateY [b]); 
          delay(20);
        }
      }
    }
Not really... See the Technical FAQ for explanations why it is a bad idea to use delay() and to do lot of loops in draw()...
And I was thinking about ArrayList. There are lot of examples of using it, search the forum with ArrayList, PVector, perhaps drawing. I will come back this evening to help more if needed.
I will have a look at that, I didn't work yet with ArrayList but I will try to manage..after I tell you something.
The strange thing is that I had already draw a graph based on the following links and it was not necessary to save the previous data to get the whole line ( http://arduino.cc/en/Tutorial/Graph, http://robotpig.net/UAVs-blogs/arduino-and-processing-_1772). And I don´t understand why it doesn´t work in this case I´m trying now..

Thanks again for help
Hello,

I already fixed the problem...I was setting background in the void draw()  function and the background was hiding the curve.
Also,everything you draw with some fill() in the   void draw () function hides the curve, that´s why I have the grid lines at the same colour as the curve. 
Here is the new code if you want to see..
I think it works well now!With some McGyver tricks but it works..

Thanks a lot for your help

 
Copy code
  1. float dataMin, dataMax;
  2. float plotX1, plotY1;
  3. float plotX2, plotY2;
  4. float labelX, labelY;

  5. //Set the time data
  6. int [] time = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
  7. int timeInterval = 1;
  8. int timeMin = 0;
  9. int timeMax = 24;
  10. int volumeInterval = 2;
  11. int volumeIntervalMinor = 2;

  12. float lineX = 120;//To start next to the Y axis

  13. PFont plotFont;

  14. void setup ()
  15. {
  16.   size(1000,600);
  17.   background(224);
  18.   plotX1 = 120; 
  19.   plotX2 = width - 80;
  20.   labelX = 50;
  21.   plotY1 = 60;
  22.   plotY2 = height - 70;
  23.   labelY = height - 25;
  24.   dataMin = 0;
  25.   dataMax = ceil (50/volumeInterval)*volumeInterval;
  26.   plotFont = createFont("SansSerif", 20);
  27.   textFont(plotFont);
  28.   
  29.   smooth();
  30. }
  31.   
  32. void draw ()
  33. {
  34.   // Show the plot area as a white box  
  35.   //fill(255);
  36.   rectMode(CORNERS);
  37.   stroke(#5679C1);
  38.   strokeWeight(1);
  39.   rect(plotX1, plotY1, plotX2, plotY2);  
  40.   stroke(2);
  41.   smooth();
  42.   fill(224);
  43.   noStroke();
  44.   rect(0,0,width,plotY1);
  45.   rect(0,0,plotX1,height);
  46.   rect(0,height,width,plotY2);
  47.   rect(plotX2,0,width,height);

  48.   drawAxisLabels();
  49.   drawYearLabels();
  50.   drawVolumeLabels();
  51.   drawTitle();
  52.   drawDataLine();
  53.   smooth();

  54. }

  55. void drawTitle() {
  56.   fill(0,0,255);
  57.   textSize(20);
  58.   textAlign(LEFT);
  59.   String title = "Temperature";
  60.   text(title, plotX1, plotY1 - 10);
  61. }
  62.   
  63. void drawYearLabels() 
  64. {
  65.   fill(0,0,255);
  66.   textSize(10);
  67.   textAlign(CENTER);
  68.   
  69.   // Use thin, blue lines to draw the grid
  70.   for (int row = 0; row < 25; row++) {
  71.     if (time[row] % timeInterval == 0) {
  72.       float x = map(time[row], timeMin, timeMax, plotX1, plotX2);
  73.       text(time[row], x, plotY2 + textAscent() + 10);
  74.       stroke(#5679C1);
  75.       strokeWeight(0.00001);
  76.       line(x, plotY1, x, plotY2);
  77.       line(plotX1,plotY2,plotX2,plotY2);
  78.       line(plotX2,plotY1,plotX2,plotY2);
  79.       stroke(0);
  80.       strokeWeight(1);
  81.       line(x, plotY2+4, x, plotY2);
  82.     }
  83.   }
  84. }

  85.   void drawAxisLabels() 
  86. {
  87.   fill(0,0,255);
  88.   textSize(13);
  89.   textLeading(15);
  90.   
  91.   textAlign(CENTER, CENTER);
  92.   text("Graus Celsius\n (ºC)", labelX, (plotY1+plotY2)/2);
  93.   textAlign(CENTER);
  94.   text("Time (h)", (plotX1+plotX2)/2, labelY);

  95. void drawVolumeLabels() {
  96.   fill(0,0,255);
  97.   textSize(10);
  98.   textAlign(RIGHT);
  99.   
  100.   stroke(0);
  101.   strokeWeight(1);

  102.   for (float v = dataMin; v <= dataMax; v += volumeIntervalMinor) {
  103.     if (v % volumeIntervalMinor == 0) {     // If a tick mark
  104.       float y = map(v, dataMin, dataMax, plotY2, plotY1);  
  105.       if (v % volumeInterval == 0) {        // If a major tick mark
  106.         float textOffset = textAscent()/2;  // Center vertically
  107.         if (v == dataMin) {
  108.           textOffset = 0;                   // Align by the bottom
  109.         } else if (v == dataMax) {
  110.           textOffset = textAscent();        // Align by the top
  111.         }
  112.         text(floor(v), plotX1 - 10, y + textOffset);
  113.         line(plotX1 - 4, y, plotX1, y);     // Draw major tick
  114.         line(plotX1-2, y+9, plotX1, y+9);  //line to draw midle lines
  115.       } else {
  116.         //line(plotX1 - 2, y, plotX1, y);     // Draw minor tick
  117.       }
  118.     }
  119.   }
  120. }

  121. void mousePressed() 
  122. {
  123.   println("Coordinates: " + mouseX +"," + mouseY);
  124. }


  125. void drawDataLine() {  
  126.   stroke(#5679C1);
  127.   strokeWeight(5);
  128.   noFill();
  129.   
  130.   if(pmouseY>60 && pmouseY<(height-70))
  131.   {
  132.   line (lineX,mouseY,lineX,pmouseY); 
  133.   lineX ++;
  134.   delay(20);
  135.   }
  136.   
  137.   if (lineX>width-80)
  138.   {
  139.     lineX=120;
  140.     background (255);
  141.    }
  142. }


Hello people,

Now that I have it working a just need a little more help...
This sketch ( http://www.openprocessing.org/sketch/61287) makes a realtime graph of the mouse position when it's inside the window, it opens 3 windows that do exactly the same. It is a sketch to make a realtime graph of 3 different sensors..
However I would like to put them together on the same window and switch the view with tabs like it  does in this example:   http://www.openprocessing.org/sketch/49248 (similar to interpolate example).

The problem is that i don´t understand how can I modify the code to adapt to my case.

Somebody can help me with this?

Thanks in advance
SOmebody here that already had used Processing as an interface, with this tabs/buttons that Ruben mentioned above??