2010-06-24 3 views
0

나는 dojox.charting을 사용하여 프로그래밍 방식으로 차트를 작성하는 간단한 함수를 만들었습니다. addSeries를 통해 차트에 변수를 전달할 때 변수를 올바르게 캐스팅하는 방법에 대해 약간 당황 스럽습니다. 다음을 고려하십시오 :간단한 도장 차트에 시리즈 추가하기

function buildChart(targetDiv){ 
     //grab the destination 
     var bc = dojo.byId(targetDiv); 

     //define the data for the series 
     var testData = [2,4,2,2,2,3,2,10,11,12,8,4]; 
     var string = "2,4,2,2,2,3,2,10,11,12,8,4"; 
     var convertedString = string.split(","); 
     console.log("Variable testData value is " + typeof(testData)); 
     console.log("Variable convertedString value is " + typeof(convertedString)); 

     //build the chart 
     dojo.attr(bc,"style","width:300px;height:200px;"); 
     var chart = new dojox.charting.Chart2D(bc); 
     chart.addPlot("default", {type: "Lines"}); 
     chart.addAxis("x"); 
     chart.addAxis("y", {vertical: true}); 
     //chart.addSeries("Series 1 works fine", testData); 
     chart.addSeries("Series 2 not working", convertedString); 
     chart.render(); 
}//buildChartenter code here 

testData 변수는 올바르게 작동하지만 convertedString 변수는 작동하지 않습니다. 나는 아주 간단한 것을 놓치고 있어야합니다. 이 경우 인바운드 문자열 변수를 어떻게 캐스팅합니까?

답변

1

그래, 그것은 쉽다 : testData는 숫자의 배열이고, convertedString은 문자열의 배열이다.

var convertedString = dojo.map(string.split(","), parseFloat); 

또는 수동으로 작업을 수행 할 수 있습니다 :

당신은 그런 식으로 숫자로 그 문자열을 변환 할 수 있습니다

var convertedString = string.split(","); 
for(var i = 0; i < convertedString.length; ++i){ 
    convertedString[i] = parseFloat(convertedString[i]); 
} 

PS를 : 식별자는 것 중 & hellip 같이 string를 사용; 잘못된.

+0

우수! 나는 지금 내가 놓친 것을 본다. 그리고 저는 동의합니다. 'string'이라는 변수는 고통스럽게 상상조차 못합니다. 다음 번엔 더 나은 식별자 (foo!)로 예제를 만들 것입니다. 빠른 응답에 감사드립니다, Eugene. – Bruce