Trying to draw a lifetime graph
in
Programming Questions
•
1 year ago
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
- float dataMin, dataMax;
- float plotX1, plotY1;
- float plotX2, plotY2;
- float labelX, labelY;
- 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};
- int timeInterval = 1;
- int timeMin = 0;
- int timeMax = 24;
- int volumeInterval = 10;
- int volumeIntervalMinor = 5;
- float lineX = 120;
- PFont plotFont;
- void setup ()
- {
- size(1000,500);
- plotX1 = 120;
- plotX2 = width - 80;
- labelX = 50;
- plotY1 = 60;
- plotY2 = height - 70;
- labelY = height - 25;
- dataMin = 0;
- dataMax = ceil (50/volumeInterval)*volumeInterval;
- plotFont = createFont("SansSerif", 20);
- textFont(plotFont);
- smooth();
- }
- void draw ()
- {
- background(224);
- // Show the plot area as a white box
- fill(255);
- rectMode(CORNERS);
- noStroke();
- rect(plotX1, plotY1, plotX2, plotY2);
- stroke(2);
- smooth();
- drawTitle();
- drawAxisLabels();
- drawYearLabels();
- drawVolumeLabels();
- drawDataLine();
- }
- void drawTitle() {
- fill(0);
- textSize(20);
- textAlign(LEFT);
- String title = "Temperature";
- text(title, plotX1, plotY1 - 10);
- }
- void drawYearLabels()
- {
- fill(0);
- textSize(10);
- textAlign(CENTER);
- // Use thin, gray lines to draw the grid
- stroke(224);
- strokeWeight(1);
- for (int row = 0; row < 25; row++) {
- if (time[row] % timeInterval == 0) {
- float x = map(time[row], timeMin, timeMax, plotX1, plotX2);
- text(time[row], x, plotY2 + textAscent() + 10);
- line(x, plotY1, x, plotY2);
- }
- }
- }
- void drawAxisLabels()
- {
- fill(0);
- textSize(13);
- textLeading(15);
- textAlign(CENTER, CENTER);
- text("Graus Celsius\n (ºC)", labelX, (plotY1+plotY2)/2);
- textAlign(CENTER);
- text("Time (h)", (plotX1+plotX2)/2, labelY);
- }
- void drawVolumeLabels() {
- fill(0);
- textSize(10);
- textAlign(RIGHT);
- stroke(128);
- strokeWeight(1);
- for (float v = dataMin; v <= dataMax; v += volumeIntervalMinor) {
- if (v % volumeIntervalMinor == 0) { // If a tick mark
- float y = map(v, dataMin, dataMax, plotY2, plotY1);
- if (v % volumeInterval == 0) { // If a major tick mark
- float textOffset = textAscent()/2; // Center vertically
- if (v == dataMin) {
- textOffset = 0; // Align by the bottom
- } else if (v == dataMax) {
- textOffset = textAscent(); // Align by the top
- }
- text(floor(v), plotX1 - 10, y + textOffset);
- line(plotX1 - 4, y, plotX1, y); // Draw major tick
- } else {
- //line(plotX1 - 2, y, plotX1, y); // Draw minor tick
- }
- }
- }
- }
- void mousePressed()
- {
- println("Coordinates: " + mouseX +"," + mouseY);
- }
- void drawDataLine() {
- stroke(#5679C1);
- strokeWeight(5);
- noFill();
- if(pmouseY>60 && pmouseY<(height-70))
- {
- line (lineX,mouseY,lineX,pmouseY);
- lineX ++;
- delay(20);
- }
- if (lineX>width-80)
- {
- lineX=120;
- background (255);
- }
- }
1