Doing a fade-in within a for loop
in
Programming Questions
•
2 years ago
Hello all,
I'm creating a modestly interactive area chart. The data is pulled from an array, which I activate with a for loop.
I'm doing mouseovers on the data points, so that a user can roll over the datapoint and have a small window appear with the data value. This part of it works fine. However, I'd like for the small window to fade in when the user mouses over, but I can't figure out how to do this within the context of the loop.
Now, I can do this manually by adjusting the alpha values, like so:
if (sqrt(sq(disX0) + sq(disY0)) < ellipseSize/2 ) {
//ellipse redraw
fill(255);
stroke(5, 55, 105);
strokeWeight(2);
ellipse(plotX1, d0, ellipseSize, ellipseSize);
strokeWeight(1);
noStroke();
fill(250, a);
rect(mouseX-25, mouseY-35, 50, 30);
fill(5, 55, 105, a);
rect(mouseX-25, mouseY-35, 50, 15);
fill(255, a);
textAlign(CENTER);
text(q0, mouseX, mouseY-25);
fill(100, a);
text(d0Label, mouseX, mouseY-10);
if (a < 255 ) {
a += (255 - a) * easing;
}
}
However, since I've got a lot of data points I'm using an array, and a for loop, and I can't figure out how to get the same effect in the context of the following code:
for (int i = 0; i < data.length; i++) {
float disX = plotX1 - mouseX;
float x = plotX1;
String Label = str(data[i]);
String Quarter = quarter[i];
if (dataMin < 0) {
y = (newY - (map(data[i], dataMin, dataMax, 0, 80)) + mapConstant);
}
else {
y = (newY - (map(data[i], dataMin, dataMax, 0, 80)));
}
float disY = y - mouseY;
if (sqrt(sq(disX) + sq(disY)) < ellipseSize/2 ) {
//ellipse redraw
fill(255);
stroke(5, 55, 105);
strokeWeight(2);
ellipse(x, y, ellipseSize, ellipseSize);
strokeWeight(1);
noStroke();
fill(250, alph);
rect(mouseX-25, mouseY+consA, 50, 30);
fill(5, 55, 105, alph);
rect(mouseX-25, mouseY+consA, 50, 15);
fill(255, alph);
textAlign(CENTER);
text(Quarter, mouseX, mouseY+consB);
fill(100, alph);
text(Label, mouseX, mouseY+consC);
}
plotX1 += 25;
}
Any suggestions? I've tried sticking the code to update the alpha variable all over the place, but to no avail. Note that all this code is within a "chart" class that gets initialized in setup and drawn in draw. Many thanks!
I'm creating a modestly interactive area chart. The data is pulled from an array, which I activate with a for loop.
I'm doing mouseovers on the data points, so that a user can roll over the datapoint and have a small window appear with the data value. This part of it works fine. However, I'd like for the small window to fade in when the user mouses over, but I can't figure out how to do this within the context of the loop.
Now, I can do this manually by adjusting the alpha values, like so:
if (sqrt(sq(disX0) + sq(disY0)) < ellipseSize/2 ) {
//ellipse redraw
fill(255);
stroke(5, 55, 105);
strokeWeight(2);
ellipse(plotX1, d0, ellipseSize, ellipseSize);
strokeWeight(1);
noStroke();
fill(250, a);
rect(mouseX-25, mouseY-35, 50, 30);
fill(5, 55, 105, a);
rect(mouseX-25, mouseY-35, 50, 15);
fill(255, a);
textAlign(CENTER);
text(q0, mouseX, mouseY-25);
fill(100, a);
text(d0Label, mouseX, mouseY-10);
if (a < 255 ) {
a += (255 - a) * easing;
}
}
However, since I've got a lot of data points I'm using an array, and a for loop, and I can't figure out how to get the same effect in the context of the following code:
for (int i = 0; i < data.length; i++) {
float disX = plotX1 - mouseX;
float x = plotX1;
String Label = str(data[i]);
String Quarter = quarter[i];
if (dataMin < 0) {
y = (newY - (map(data[i], dataMin, dataMax, 0, 80)) + mapConstant);
}
else {
y = (newY - (map(data[i], dataMin, dataMax, 0, 80)));
}
float disY = y - mouseY;
if (sqrt(sq(disX) + sq(disY)) < ellipseSize/2 ) {
//ellipse redraw
fill(255);
stroke(5, 55, 105);
strokeWeight(2);
ellipse(x, y, ellipseSize, ellipseSize);
strokeWeight(1);
noStroke();
fill(250, alph);
rect(mouseX-25, mouseY+consA, 50, 30);
fill(5, 55, 105, alph);
rect(mouseX-25, mouseY+consA, 50, 15);
fill(255, alph);
textAlign(CENTER);
text(Quarter, mouseX, mouseY+consB);
fill(100, alph);
text(Label, mouseX, mouseY+consC);
}
plotX1 += 25;
}
Any suggestions? I've tried sticking the code to update the alpha variable all over the place, but to no avail. Note that all this code is within a "chart" class that gets initialized in setup and drawn in draw. Many thanks!
1