2013-11-23 25 views
0

我有一個處理草圖,我正在試圖繪製一個正在處理的數據集的散點圖。我將.CSV文件加載到數組中並沒有問題,我可以將其全部格式化並將其放入「標記」數組中,以便訪問特定行中的信息。陣列中的數據在哪裏?

這個問題似乎是在我解析數據後,嘗試映射信息以繪製圖形,數組中的所有元素都返回0.0。

該代碼有點冗長,但我會盡量保持相關的位,並希望不會留下任何東西。

void setup(){ 
size(1024, 768) 
smooth(); 

OfficinaBold36 = loadFont("OfficinaBold-36.vlw"); 

//Load data 
googleData  = loadStrings("RobFordCorrelate.csv"); 

//Init all the arrays 
if((googleData == null){ 
println("Error, one of the datasets could not be loaded."); 
} 
else{ 

    totalSearch  = googleData.length-1; 
    googleDates  = new int[totalSearch]; 
    relativeInterest = new float[totalSearch]; 

    //Also grabbing all column names for use later, if needed 
    columnNames  = googleData[0].split(","); 
} 

parseGoogleData(); 
} 

這個parseGoogleData功能:

void parseGoogleData(){ 

/*Grab all the dates, we have to loop backwards through this set, 
because the CSV was formatted in reverse chronological order. 

Note that because of the row > 0 condition, we will not include 
row 0, i.e the column title row */ 

for (int row = googleData.length-1 ; row > 0; row--){ 

/*counter going up, we need a reference to a counter that 
counts up, while the main index counts down, to be 
able to assign certain values*/ 
int i = 0; 

//Grab all the elements in that row, splitting them at the comma 
googleTokens = googleData[row].split(","); 

// Grab the elements we want to look at, the date, index 0 
googleDates[i]  = int(googleTokens[0]); 

//and relative interest in the search term "rob ford", index 1 
relativeInterest[i] = float(googleTokens[1]); 


//increment our 2nd counter 
i++; 
} 
} 

另外,relativeInterest陣列是一個全局變量,所以我有機會獲得這兩個設置和借鑑。現在,如果我println(relativeInterest[i])在最後一個for循環,它將返回所有正確的數據。但是,如果我在下面的draw()循環中打印它們,它們都會返回零(我只包含指向繪製每個點的線,因爲我有許多x和y軸的定位數據,不想包括):

void draw(){ 
for(int row = 0 ; row < totalSearch; row++){ 
float y = map(relativeInterest[row], -3, 3, 0, width); 
float x = row*lb; 

int d = 5; 

noStroke(); 
fill(#FFBA00, 180); 
ellipse(x, y, d, d); 
} 

當我運行這段代碼,每個橢圓是完全相同的y位置,我不知道當陣列中的數據越來越設置爲0?繪製循環中的東西不會訪問數組,直到這些行命中,但我不知道他們爲什麼被轉換爲0.0?

任何/所有的幫助,非常感謝。對不起,這麼長的帖子!

回答

1

將您的int i = 0;移到for循環之外。你也分別想要Integer.valueOfFloat.valueOf,而不是int()float()

+0

謝謝!所以我有點想到'float()'轉換正在斬斷額外的0。我仍然對這很多新東西感到抱歉,所以對於愚蠢的後續問題感到抱歉,爲什麼我需要將'int i = 0'移到for循環之外? – BardiaD

+0

否則每次都會重置爲0。 –