Ajout d'une série à un graphique simple dojo
-
29-09-2019 - |
Question
Je l'ai construit une fonction simple qui construit à l'aide programatically graphiques dojox.charting. Je suis un peu perplexe quant à la façon dont les variables de la distribution correctement lors de leur passage à la carte via addSeries. Considérez ceci:
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
Notez que la variable TestData fonctionne très bien, mais la variable convertedString ne fonctionne pas. Je dois manquer quelque chose de très simple. Comment puis-je lancer une variable chaîne entrant au travail dans ce cas?
La solution
Oui, il est facile. testData
est le tableau de nombres, alors que convertedString
est le tableau de chaînes
Vous pouvez convertir ces chaînes en nombres comme ça:
var convertedString = dojo.map(string.split(","), parseFloat);
Ou vous pouvez le faire manuellement:
var convertedString = string.split(",");
for(var i = 0; i < convertedString.length; ++i){
convertedString[i] = parseFloat(convertedString[i]);
}
PS:. L'utilisation string
comme identifiant semble ... mauvais