문제

I have constructed a simple function that programatically builds charts using dojox.charting. I'm a bit puzzled as to how to cast variables correctly when passing them to the chart via addSeries. Consider this:

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

Notice that the testData variable works fine, but the convertedString variable does not. I must be missing something very simple. How would I cast an inbound string variable to work in this case?

도움이 되었습니까?

해결책

Yep, it is easy: testData is the array of numbers, while convertedString is the array of strings.

You can convert those strings to numbers like that:

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

Or you can do it manually:

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

PS: Using string as in identifier seems … wrong.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top