1 function Graph(container, id, options, transform, legend) {
2 if( !options ) options = { };
7 this.options = options;
8 this.transform = transform;
13 var name = (options.instanceNames && options.instanceNames[id])
14 ? options.instanceNames[id] : id;
15 var graph = document.createElement('div');
16 var label = document.createElement('h2');
17 label.innerHTML = options.title
18 ? options.title.replace("%s", name) : name;
20 container.appendChild( label );
21 container.appendChild( graph );
23 this.canvas = document.createElement('canvas');
24 graph.appendChild( this.canvas );
27 this.canvas.width = ( options.width || graph.offsetWidth - 20 );
28 this.canvas.height = ( options.height || 300 );
31 Graph.prototype.addDataset = function(name, ds) {
33 this.layout = new PlotKit.Layout( this.type, this.options );
38 for( var i = 0; i < this.cols; i++ )
39 ds[i] = new Array( i, 0 );
42 this.dataset[name] = ds;
43 this.layout.addDataset(name, ds);
46 Graph.prototype.updateDataset = function(name, value) {
47 if( this.dataset[name] ) {
48 var ds = this.dataset[name];
50 for( var i = 1; i < this.cols; i++ )
51 ds[i-1][1] = ds[i][1];
53 value = Math.abs( parseFloat(value) || 0 );
55 if( this.transform ) {
57 value = (this.lastvalue[name]) ? this.transform(value, this.lastvalue[name]) : 0;
58 this.lastvalue[name] = orgvalue;
61 ds[this.cols-1][1] = value;
62 this.layout.addDataset(name, ds);
66 Graph.prototype.draw = function( options ) {
68 this.plotter = new PlotKit.CanvasRenderer(
69 this.canvas, this.layout, this.options || options || {}
72 this.layout.evaluate();
73 this.plotter.render();
79 legend = new LegendRenderer(this.legend, this.layout, legend_opt);
84 Graph.prototype.redraw = function() {
85 if( this.layout && this.plotter ) {
86 this.layout.evaluate();
88 this.plotter.render();
93 function GraphRPC(container, uri, action, interval, datasources, options, transform, legend) {
94 this.ds = datasources;
97 this.options = options || { };
98 this.container = container;
99 this.transform = transform;
100 this.proxy = new MochiKit.JsonRpc.JsonRpcProxy(uri, [action]);
101 this.graphs = new Object();
102 this.legend = legend;
108 window.setInterval(function(){self.requestData()}, interval);
112 GraphRPC.prototype.requestData = function() {
113 var r = this.proxy[this.action](); var self = this;
114 r.addCallback(function(r){ self.dispatchResponse(r) });
115 r.addErrback(function(e){ throw('Error: ' + e) });
118 GraphRPC.prototype.dispatchResponse = function(response) {
120 if( this.options.instances ) {
121 instances = this.options.instances;
124 instances = new Array();
125 for( var instance in response ) {
126 instances[instances.length] = instance;
130 for( var j = 0; j < instances.length; j++ ) {
131 var instance = instances[j];
133 if( this.options.separateDS ) {
134 for( var i = 0; i < this.ds.length; i += 2 ) {
135 var name = this.ds[i+1] || this.ds[i];
136 var gid = instance + '-' + name;
137 var otle = this.options.title || instance;
139 if( !this.graphs[gid] ) {
140 this.options.title = otle.replace('%s', instance) + ': ' + name;
141 this.graphs[gid] = new Graph(
142 this.container, gid, this.options, this.transform, this.legend
145 this.graphs[gid].addDataset(name);
146 this.graphs[gid].draw();
147 this.options.title = otle;
152 if (typeof (this.ds[i]) == "function") {
154 instance ? response[instance] : response
158 ? response[instance][this.ds[i]]
159 : response[this.ds[i]]
161 this.graphs[gid].updateDataset(
164 this.graphs[gid].redraw();
169 var gid = instance || 'livegraph';
170 if( !this.graphs[gid] ) {
171 this.graphs[gid] = new Graph(
172 this.container, gid, this.options, this.transform, this.legend
175 for( var i = 0; i < this.ds.length; i += 2 ) {
176 var name = this.ds[i+1] || this.ds[i];
177 this.graphs[gid].addDataset(name);
180 this.graphs[gid].draw();
183 for( var i = 0; i < this.ds.length; i += 2 ) {
184 var name = this.ds[i+1] || this.ds[i];
186 if (typeof (this.ds[i]) == "function") {
188 instance ? response[instance] : response
192 ? response[instance][this.ds[i]]
193 : response[this.ds[i]]
195 this.graphs[gid].updateDataset(
200 this.graphs[gid].redraw();