Miguel Flores Miguel Flores - 1 month ago 12
Javascript Question

Something is wrong filling array

I have the following ajax call:

$.ajax({
type: 'POST',
url: '../ws_BQS.asmx/ResultadosDimensionalByDate',
data: '{"fecha":"' + fecha + '"}',
dataType: 'json',
contentType: 'application/json',
timeout: 600000,
error: function (xhr) {

},
success: function (data) {
var t = data.d;
var split;
var datos = t.split(",");

//Thickness
for (var i = 0; i < datos.length; i++) {
array1.push(datos[i]);
i++;
i++;
i++;
//alert(array1);
}
//Width
for (var w = 0; w < datos.length; w++) {
w++;
//array2.push(datos[w]);
w++;
w++;
}
alert(array1);

chart(array1);
}
});


Alert message is (1,5,9,14,18) that's what I expect, but the problem is that my chart function is not doing what I want, here is the code:

function chart(arreglo) {


$('#Grafica').highcharts({
chart: {
type: 'bar'
},
title: {
text: 'Inspeccion Dimensional'
},
xAxis: {
categories: ['Pieza 1', 'Pieza 2', 'Pieza 3', 'Pieza 4', 'Pieza 5']
},
yAxis: {
min: 0,
title: {
text: 'Resultados'
}
},
legend: {
reversed: true
},
plotOptions: {
series: {
stacking: 'normal'
}
},
series: [{
name: 'Thickness',
data: arreglo
}, {
name: 'Width',
data: [2, 2, 3, 2, 1]
}, {
name: 'Length',
data: [2, 2, 3, 2, 1]
}, {
name: 'Diameter',
data: [3, 4, 4, 2, 5]
}]
});

alert(arreglo);
}


My alert message is the same from my ajax call so that's ok, the problem is when I use my var
arreglo
in
series
option, I dont get any error message, just it seems that
arreglo it's empty
, I'm trying to solve this and I found that could be an issue from
array1.push(datos[i]);
because if I do
array1.push(values manually)
it works, how can I solve this?

UPDATE

Here is a Fiddle to see my problem.

UPDATE #2

These are the ways that I get my data:

data: console.log = 1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19,20,21

var datos = t.split(",");
console.log(datos) = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21"]

for loop
console.log(array1) = ["1", "5", "9", "14", "18"]

chart function
console.log(arreglo) = ["1", "5", "9", "14", "18"]


With this I can see that it's wrong receiving data with "", so how can I change this?

Answer

Your array must contain numbers, instead it contains strings which is why the series looks like it does not have any data.

Parse strings to number and it works as expected.

data: arreglo.map(function (value) {
    return Number(value);
  })

example: https://jsfiddle.net/nvf6vcn5/4/