I have to generate something like this:
[{
"balloonText": "[[category]]: <b>[[value]]</b>",
"fillAlphas": 0.9,
"lineAlpha": 0.2,
"type": "column",
"valueField": "Porcentaje1"
}, {
"balloonText": "[[category]]: <b>[[value]]</b>",
"fillAlphas": 0.9,
"lineAlpha": 0.2,
"type": "column",
"valueField": "Porcentaje2"
}]
But the number of elements in that array is dynamic, everything is the result of an SQL query, the fields must have the same information except "valueField" (it is the one that is filled dynamically).
I do this with a function, by which I send an array with the values that valueField will carry (example: Names ["Percentage1", "Percentage2",...]
So my function is the following:
var array = new Object();
var res = [];
for (i=1; i<cantNames;i++)
{
array["ballonText"] = "[[category]]: <b>[[value]]</b>";
array["fillAlphas"] = 0.9;
array["lineAlpha"] = 0.2;
array["type"] = "column";
array["valueField"] = Names[i];
res.push(array);
}
result = res;
return result;
cantNames is the number of values I get but I need to exclude the first one.
Names
is filled as follows:
var valores = Data;
if(valores.length > 0)
{
var atributos = [];
for(var aux in valores[0])
{
atributos.push(aux);
}
return atributos;
}
else
{
alert('No hay datos');
return '';
}
When I get the result it gives me the following:
[{
"balloonText": "[[category]]: <b>[[value]]</b>",
"fillAlphas": 0.9,
"lineAlpha": 0.2,
"type": "column",
"valueField": "Porcentaje2"
}, {
"balloonText": "[[category]]: <b>[[value]]</b>",
"fillAlphas": 0.9,
"lineAlpha": 0.2,
"type": "column",
"valueField": "Porcentaje2"
}]
Does anyone know why it is repeating the value of "Percentage2"?? Thank you!!!
The problem is not in the filling but in the object references of the array that you make a copy of, move the line
var array = new Object();
inside the for loop so that your copy of the finished arrayresult = res
has the correct references, otherwise you are left with only the references of the last element pushed to the array.Cheers