How to convert or create a string to elements in js

0

I probably did not present the question well

I'm a rookie in json code and I need to create a canvas graphic with javascript. The queries in php and mysql are made correctly and I convert the results in an array and this in json code to work with the results in a javascript file.

I'm using the Chart Js framework to create the graphics. My conflict is in this part, where manually it is completed like this

labels: ["month1", "month2"], data: [500, 550],

now the labels and data data I get from an sql query

I have the following code

var nom = '';
for(var i in d2){
    nom += '"'+d2[i].N+'",';
}
nom = nom.substring(0,nom.length-1);

var punto = '';
for(var j in d2){
    punto += d2[j].P+',';
}
punto = punto.substring(0,punto.length-1);

The error occurs when I pass these variables to the corresponding data, that is:

labels: [nom],
data: [punto],

When executing the code, I take each variable as a single string, and I need to take it as several values separated by commas. I do not know what kind of conversion I should do.

    
asked by Sagara 19.09.2017 в 06:55
source

1 answer

1

You take them as strings, because you are defining them as strings. Chart.js expects those values as fixes, so change your code to use fixes.

var nom = [];
for(var i in d2){
    nom.push(d2[i].N);
}

var punto = [];
for(var j in d2){
    punto.push( d2[j].P );
}

And you use them as is:

labels: nom,
data: punto,
    
answered by 19.09.2017 / 07:24
source