我想弄清楚如何將draw()函數中的這些行合併到for循環中。基本上,我正在嘗試對條形圖進行動畫處理,例如試圖將每個條從0繪製到它的Y值,然後繪製下一個條等等。我可以手動添加下面的每個塊,並且它可以很好地創建每一行,但似乎更好地自動化它。每行創建一個欄/列。如何在處理可視化中動畫條形圖數據?
float yPos = map(actions[1].presses, minVal, maxVal, height/2, 50);
line(lineXpos, actions[1].a, endLineXpos, actions[1].a);
actions[1].a = actions[1].a - 2;
if (actions[1].a < yPos) {
actions[1].a = height/2;
}
float yPos2 = map(actions[2].presses, minVal, maxVal, height/2, 50);
line(lineXpos+10, actions[2].a, endLineXpos+10, actions[2].a);
actions[2].a = actions[2].a - 2;
if (actions[2].a < yPos2) {
actions[2].a = height/2;
}
float yPos3 = map(actions[3].presses, minVal, maxVal, height/2, 50);
...
...
and so on...
好像我可以鞏固上述弄成這個樣子以下,但是當我運行它,它無休止地運行,並創建了一個斜坡。
void draw() {
for(int j = 1; j < actions.length; j++) {
actions[j].yPos = map(actions[j].presses, minVal, maxVal, height/2, 50);
line(actions[j].lineXpos, actions[j].a, actions[j].endLineXpos, actions[j].a);
actions[j].a = actions[j].a - 2;
if (actions[j].a < actions[j].yPos) {
actions[j].a = height/2;
actions[j].lineXpos = actions[j].lineXpos + 10;
actions[j].endLineXpos = actions[j].endLineXpos + 10;
}
}
}
我認爲每次操作[j] .yPos都會被覆蓋。任何想法我做錯了什麼?
謝謝!
**編輯:添加我的完整代碼下面測試出來。
Action[] actions;
int leng;
float minVal, maxVal;
float a;
float lineXpos = 0;
float endLineXpos = 10;
//now draw lines
float xPos = 0;
float lineX = 0;
float lineY = 0;
void setup() {
size(1400, 600);
background(160,196,242);
stroke(191,133,99);
fill(0);
smooth();
textSize(14);
textAlign(LEFT, TOP);
String[] data = loadStrings("keystrokes.log");
actions = new Action[data.length];
for(int i = 1; i < actions.length; i++) {
float[] values = float(split(data[i], ","));
actions[i] = new Action(values[0], values[1], values[2]);
if(i == 1) {
minVal = maxVal = actions[i].presses;
}
if(actions[i].presses < minVal) minVal = actions[i].presses;
if(actions[i].presses > maxVal) maxVal = actions[i].presses;
}
println("clicks min:"+minVal+" max:"+maxVal);
}
void draw() {
for(int j = 1; j < actions.length; j++) {
//println("presses: "+actions[j].presses);
float yPos = map(actions[j].presses, minVal, maxVal, height/2, 50);
//draw lines
if(j == 1) {
lineX = xPos;
lineY = yPos;
} else {
line(lineX+10, height/2, xPos, yPos);
}
lineX = xPos;
lineY = yPos;
xPos += 10;
}
float yPos = map(actions[1].presses, minVal, maxVal, height/2, 50);
line(0, actions[1].a, 10, actions[1].a);
actions[1].a = actions[1].a - 2;
if (actions[1].a < yPos) {
actions[1].a = height/2;
}
float yPos2 = map(actions[2].presses, minVal, maxVal, height/2, 50);
line(0+10, actions[2].a, 10+10, actions[2].a);
actions[2].a = actions[2].a - 2;
if (actions[2].a < yPos2) {
actions[2].a = height/2;
}
float yPos3 = map(actions[3].presses, minVal, maxVal, height/2, 50);
line(0+20, actions[3].a, 10+20, actions[3].a);
actions[3].a = actions[3].a - 2;
if (actions[3].a < yPos3) {
actions[3].a = height/2;
}
}
// the white dots Object, cookie cutter
class Action {
float time;
float clicks;
float presses;
float speed;
float a = height/2;
float xpos;
float lineXpos = 0;
float endLineXpos = 10;
float yPos;
Action(float t, float p, float c) {
time = t;
presses = p;
clicks = c;
speed = 2;
}
}
Keystokes.log:
1369064940, 0, 0
1369065060, 65, 19
1369065180, 90, 28
1369065300, 179, 27
1369065420, 242, 20
1369065540, 156, 30
是的工作!感謝@luisdaniel超級有用。我覺得我很接近,但由於某種原因無法克服駝峯。 – Aaron