2013-11-23 2 views
0

나는 Processing 스케치를 가지고 있는데, 여기서 내가 작업하고있는 데이터 세트의 산점도를 그리는 중이다. 배열에 .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 배열이 전역 변수이기 때문에, I는 모두 설정에 대한 액세스를 가지며 그린다. 이제 내가 마지막으로 for 루프에 println(relativeInterest[i])이 있으면 모든 적절한 데이터를 반환합니다. 그러나, 아래 draw() 루프에서 인쇄 할 경우 그들은 모두 0을 반환합니다. 나는 x 축과 y 축의 위치 데이터가 많아서 각 점을 그리는 것을 참조하는 선만 포함합니다.) 포함하지 않았다 배열의 데이터를 0으로 설정하기 때

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으로 변환되는지 알 수 없습니까?

/모든 도움을 주시면 대단히 감사하겠습니다. 그런 긴 포스트를 유감스럽게 생각합니다!

답변

1

int i = 0;을 for 루프 외부로 이동하십시오. 또한 각각 Integer.valueOfFloat.valueOf이 필요하며 int()float()이 아닙니다.

+0

고맙습니다! 그래서 나는 float() 변환이 여분의 0을 잘라내는 것이라고 생각했습니다. 나는 아직도 이것에 대해 많이 모른다. 어리석은 후속 질문에 대해 유감스럽게 생각한다. 왜 for 루프 밖에서'int i = 0'을 움직여야 할까? – BardiaD

+0

그렇지 않으면 매번 0으로 재설정됩니다. –

관련 문제