I am trying to build a plotting webpage which gets data from multiple xively feeds and plots them with Rickshaw (based on D3). The problem I have is that I can't access the data which is returned from the server outside of the calling function. I'm new to js so I'm not sure how to deal with the asynchronous nature of the calls. I would like to call the xively.feed.history()
to get various sections of historical data and them concatenate them onto a single graph. Anyway, here is something similar to what I've tried. It based on the xively tutorial
xively.setKey('xxxx my key ')
// Replace with your own values
var feedID = 12345678, // Feed ID
selector = "#myelement"; // Your element on the page
var series = [];
xively.feed.history(feedID, { duration: "10days", interval: 1800 ,limit: 1000}, function (mydata) {
mydata.datastreams.forEach( function(stream){
if ((stream.id == "tempinside") || (stream.id == "tempoutside")) {
var points = [];
stream.datapoints.forEach( function(datapoint){
points.push({x: new Date(datapoint.at).getTime()/1000.0, y: parseFloat(datapoint.value)});
// Add Datapoints Array to Graph Series Array
});
series.push({
name: stream.id,
data: points
});
};
});
var graph2 = new Rickshaw.Graph( {
element: document.querySelector("#chart2"),
width: 600,
height: 400,
renderer: 'line',
series: [{
data: series[0].data,
name: series[0].name,
color: 'red'
},{
data: series[1].data,
name: series[1].name,
color: 'blue'
}]
})
})
As I said, I'm new to javascript so maybe I'm missing something. The graph works fine if it is placed inside the callback part of the xively.feed.history
function. If I try to make multiple calls to the function then the data isn't ready by the time the graph code runs.