5
Good afternoon, it’s my first question here.
I’m getting the json below through an ajax request.
[
{"SEMANA":1.0,"PRODUCAO":0.0,"PRODUCAO2":0.0},
{"SEMANA":2.0,"PRODUCAO":29280.0,"PRODUCAO2":55992.0},
{"SEMANA":3.0,"PRODUCAO":93864.0,"PRODUCAO2":75072.0},
{"SEMANA":4.0,"PRODUCAO":135625.0,"PRODUCAO2":102480.0}
]
I convert JSON to array this way below:
$.get("caminho do WebService", function (dados) {
var arrSemana = [],
arrProd1 = [],
arrProd2 = [];
for (var i = 0; i < dados.length; i++) {
arrSemana.push(dados[i].SEMANA);
arrProd1.push(dados[i].PRODUCAO);
arrProd2.push(dados[i].PRODUCAO2);
}
//......
});
Just as soon as I check this one array (arrSemana, arrProd1, arrProd2)
gets like this [,,,,,,,,]
and not with the values as I would like [1.0, 2.0, 3.0]
I’m creating graphics with the highChart so I need that array.
Where am I wrong that he’s not taking the values as I need them? Does anyone have an example that can help me?
Thank you all for your attention.
I am working with Apache Cordova (Phonegap). I need to put this in 3 array so I can put it on the chart.
$(Function () { $.get("Webservice path", Function (data) {
var arrSemana = [],
arrProd1 = [],
arrProd2 = [];
for (var i = 0; i < dados.length; i++) {
arrSemana.push(dados[i].SEMANA);
arrProd1.push(dados[i].PRODUCAO);
arrProd2.push(dados[i].PRODUCAO2);
}
// Os alerts estão aqui para teste...
alert(dados);
alert(arrSemana);
alert(arrProd1);
alert(arrProd2);
$('#chart').highcharts({
title: {
text: 'Produção semanal de açúcar (registrada)',
x: -20 //center
},
subtitle: {
text: 'Dados: Teste de Relatório',
x: -20
},
xAxis: {
title: {
text: 'Semanas'
},
categories: arrSemana, //Um array vem aqui...
minTickInterval: 5,
minRange: 0,
min: 0
},
yAxis: {
allowDecimals: false,
title: {
text: 'Produção'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: 'un'
},
legend: {
layout: 'vertical',
align: 'middle',
verticalAlign: 'bottom',
borderWidth: 0
},
series: [{
name: 'Semanas 12/13',
color: '#32CD32',
data: arrProd1 // Outro array vem aqui...
}, {
name: 'Semanas 13/14',
color: '#000000',
data: arrProd2 // Outro array vem aqui...
}]
});
}).fail(Function() { Alert("Error loading report data."); }); });
you cannot send the array in the correct format?
– Erlon Charles
But the array is what I’m unable to mount. Did you talk about JSON will be? .
– Rafael Oliveira
Is the return type (mimetype, contenttype) correct? If you call
get
without specifying adataType
jQuery "guesses" the correct format based on server return. You can usegetJSON
also. Other than that, your code seems correct. Have you ever tried to doconsole.log(dados)
and see if everything is ok?– mgibsonbr
I already used $.getJSON and it happened the same thing, so I did it this way to test if it would be different the result.
– Rafael Oliveira
Your password is correct; http://jsbin.com/layojiqahu/1/edit?js,console da um console.log no
dados
and see if it is returning correct from the server– Renato Gama
Poise I also tested the code and it seems correct: http://jsfiddle.net/3s8mrcb3/
– Antony Alkmim
@Rafaeloliveira If the code is correct and the type is correct, either the problem is in the data or it’s time for you use the values. If the
console.log
do not show anything abnormal, please post the code where you use these arrays.– mgibsonbr
So that’s what struck me, too. Then I got confused, because it seems right. You have some other idea of how to put this together?
– Rafael Oliveira
I am leaving now, I will be tomorrow and return here the result. Thank you for now.
– Rafael Oliveira
@mgibsonbr posted my code here...as I am working with Apache Cordova (phonegap) I am not able to test the console.log, I can only emulate via device.
– Rafael Oliveira