Debian Codegraph @

src/js/Rickshaw.Graph.Ajax.js

raw
1 Rickshaw.namespace('Rickshaw.Graph.Ajax');
2
3 Rickshaw.Graph.Ajax = Rickshaw.Class.create( {
4
5 initialize: function(args) {
6
7 this.dataURL = args.dataURL;
8
9 this.onData = args.onData || function(d) { return d };
10 this.onComplete = args.onComplete || function() {};
11 this.onError = args.onError || function() {};
12
13 this.args = args; // pass through to Rickshaw.Graph
14
15 this.request();
16 },
17
18 request: function() {
19
20 jQuery.ajax( {
21 url: this.dataURL,
22 dataType: 'json',
23 success: this.success.bind(this),
24 error: this.error.bind(this)
25 } );
26 },
27
28 error: function() {
29
30 console.log("error loading dataURL: " + this.dataURL);
31 this.onError(this);
32 },
33
34 success: function(data, status) {
35
36 data = this.onData(data);
37 this.args.series = this._splice({ data: data, series: this.args.series });
38
39 this.graph = this.graph || new Rickshaw.Graph(this.args);
40 this.graph.render();
41
42 this.onComplete(this);
43 },
44
45 _splice: function(args) {
46
47 var data = args.data;
48 var series = args.series;
49
50 if (!args.series) return data;
51
52 series.forEach( function(s) {
53
54 var seriesKey = s.key || s.name;
55 if (!seriesKey) throw "series needs a key or a name";
56
57 data.forEach( function(d) {
58
59 var dataKey = d.key || d.name;
60 if (!dataKey) throw "data needs a key or a name";
61
62 if (seriesKey == dataKey) {
63 var properties = ['color', 'name', 'data'];
64 properties.forEach( function(p) {
65 if (d[p]) s[p] = d[p];
66 } );
67 }
68 } );
69 } );
70
71 return series;
72 }
73 } );
74