refresh data from JSON with ajax ?
i have new data update from PHP providing with JSON, normal display it work well.
but any idea to update graph ? i try to use setInterval but still not work
I'm also wondering what's the 'official' recommended way to refresh ajax-based data (either json or jsonp).
The solutions I've seen around seem to load a graph using a standard Rickshaw.Graph and then replace graph.series[0].data and issue an update(). However, I am wondering if Rickshaw.Graph.JSONP or Rickshaw.Graph.Ajax have some method to simply tell it to fetch the data again and refresh itself?
Calling request() seems to create another graph, with the same data as the old one for me. Would be great to see a working example if anyone has one.
(related: #156, #142, #135)
:+1: I too have the same question as @gingerlime! ;-)
@gorsuch I ended up creating some kind of a 'refreshable' subclass. See below. The code might not work out-of-the-box because I've had to remove some stuff in favour of clarity ... it was also originally written in coffeescript, but here's the plain js version:
Rickshaw.Graph.JSONP.Refreshable = Rickshaw.Class.create(Rickshaw.Graph.JSONP, {
request: function() {
return this.refreshGraph(period);
},
refreshGraph: function(period) {
var deferred,
_this = this;
deferred = this.getAjaxData(period);
deferred.done(function(result) {
if (result.length <= 0) {
return;
}
if (!_this.graph) {
// calling `success` creates the `graph` object for the first time
_this.success(_this.parseGraphiteData(result));
}
series = _this.parseGraphiteData(result);
for (i = _i = 0, _len = series.length; _i < _len; i = ++_i) {
el = series[i];
_this.graph.series[i].data = el.data;
}
_this.graph.render();
});
},
getAjaxData: function(period) {
var deferred;
return deferred = $.ajax({
dataType: 'json',
url: generateDataURL(this.args.your_url),
error: this.error.bind(this)
});
}
Hope you can get the rough idea what it does / how to use something similar to this. I didn't include the parseGraphiteData function, which does custom parsing / conversion of the data I get (in this case from graphite) to rickshaw-friendly format.
Calling refreshGraph will fetch the data via ajax and render the graph again with the new data.
I'm not sure this is the best way doing this, but it seems to work fine so far.
@gingerlime wow, thanks so much!
@gorsuch - I've created a small pull request in addition to @mikepb's that should make refreshing an Ajax graph much easier. Hope it's merged in and then refreshing an ajax graph can simply be done by calling its request method.
One gotcha with this approach is if you attach any extra graph elements in the onComplete callback you need to make sure you're not redeclaring them each time.
e.g. this code would create the X axis each time the graph is updated, by default this results in an ever increasing boldness to the X Axis labels.
var graph = new Rickshaw.Graph.Ajax( {
element: document.getElementById("chart"),
width: 800,
height: 300,
renderer: 'area',
stroke: true,
interpolation:'basis',
dataURL: 'dashboard/data.json',
onComplete: function(transport) {
var graph = transport.graph;
var axes = new Rickshaw.Graph.Axis.Time( {
graph: graph
} );
axes.render();
},
series: [
{
name: 'maps.google.com',
color: '#c05020',
}
]
} );
I'm getting round this by declaring the vars outside the graph and checking their existence in the onComplete calback. Open to suggestions if there is a better solution.
var axes;
var graph = new Rickshaw.Graph.Ajax( {
element: document.getElementById("chart"),
width: 800,
height: 300,
renderer: 'area',
stroke: true,
interpolation:'basis',
dataURL: 'dashboard/data.json',
onComplete: function(transport) {
var graph = transport.graph;
if(!axes){
axes = new Rickshaw.Graph.Axis.Time( {
graph: graph
} );
axes.render();
}
},
series: [
{
name: 'maps.google.com',
color: '#c05020',
}
]
} );
I'm fighting with that problem for a couple of day.... Could someone post a complete working example, that would be a great help
- index.html
- json to serve via rest
- js code to render rickshaw...
Many thanks
http://code.shutterstock.com/rickshaw/examples/refresh.html should help, you should be able to figure out how to fetch a json via rest, there are plenty of examples for it with jQuery in particular.
I feel your pain. Here's a gist from my project: https://gist.github.com/timrwilliams/11245617
I've had to rip out a few project specific parts of the page but it should give you a good start.
unbolivable you are my Jesus Christ ;-) I have a working example I can work on !!! Helps a lot !, Thanks
How is it possible from there to push new data (not update everything).. I tried graph.series.addData(data); but it seems that it is not working because the object is a Rickshaw.Graph.Ajax and not a Rickshaw.Graph ?
Can't help with that I'm afraid, I just rerender the entire graph after each AJAX request.
The trick I've recently used, is to alter your Rickshaw.Graph.series' data, then call <yourobj>.update().
I'll assume your JSON response is something like this:
[{x: 1, y: 1}, {x: 2, y: 9}, {x: 3, y: 41} .... ]
Simple demo:
/*global jQuery, Rickshaw*/
var ajax, graph;
ajax = new Rickshaw.Graph.Ajax( {
element: document.getElementById("chart"),
renderer: 'area',
dataURL: 'dashboard/data.json',
onComplete: function(transport) {
// This is your reference to your final Rickshaw.Graph object.
graph = transport.graph;
// ... do other misc addon setup, etc...
graph.render();
},
series: [
{
name: 'maps.google.com',
color: '#c05020',
}
]
});
// And for your updates.
$.getJSON('path/to/data.json', {}, function (response) {
// Update each series' data
graph.series[0].data = response;
// Inform D3 of the changes
graph.update();
});
This is a little less than ideal because I need two AJAX handlers, but it gets the job done.