Reputation: 513
Today I have been trying to create a chart using canvasjs. It all worked out very well. Now the script needs to get the chart information from a different site. When you create a request to that page, it return something like:
{x: 0.33, y: 2},{x: 0.33, y: 1}
. This is my script:
window.onload = function () {
var previousprice = 0;
var dps = []; // dataPoints
var chart = new CanvasJS.Chart("priceChart", {
title :{
text: "Coins"
},
axisY: [{
includeZero: false,
title: "dollar"
}],
axisX: {
title: "coins"
},
data: [{
type: "line",
dataPoints: dps
}]
});
var xVal = 0;
var yVal = 100;
var updateInterval = 1000;
var dataLength = 300;
var updateChart = function (count) {
count = count || 1;
if (dps.length < dataLength) {
var hihi = httpGet(location.protocol+"//"+window.location.hostname+"/inc/php/functions.php?method=get300");
console.log(hihi);
dps.push(hihi);
}
console.log(dps.length);
if (dps.length > dataLength) {
dps.shift();
}
chart.render();
};
updateChart(dataLength);
setInterval(function(){updateChart()}, updateInterval);
}
So, to add an item to the chart I need to use (example) dps.push({x: 12, y: 65})
or dps.push({x: 12, y: 65}, {x: 13, y: 50})
, but when I get that from the url, it doesn't work. Probably because it's a string.
How to convert the string into the array I need?
Upvotes: 0
Views: 153
Reputation: 102
You need parse string to JSON and push all response objects to your result array.
var dps = [];
var responseJson = JSON.parse(hihi);
responseJson.forEach(function(item) {
dps.push(item);
});
Upvotes: 2