开发者

Adding a series to a simple dojo chart

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜