1 function Graph(container, id, options, transform) {
2 if( !options ) options = { };
7 this.options = options;
8 this.transform = transform;
11 var graph = document.createElement('div');
12 var label = document.createElement('h2');
13 label.innerHTML = options.title
14 ? options.title.replace("%s", id ) : id;
16 container.appendChild( label );
17 container.appendChild( graph );
19 this.canvas = document.createElement('canvas');
20 graph.appendChild( this.canvas );
23 this.canvas.width = ( options.width || graph.offsetWidth - 20 );
24 this.canvas.height = ( options.height || 300 );
27 Graph.prototype.addDataset = function(name, ds) {
28 if( window.console ) console.debug("AddDataset: " + name);
30 this.layout = new PlotKit.Layout( this.type, this.options );
35 for( var i = 0; i < this.cols; i++ )
36 ds[i] = new Array( i, 0 );
39 this.dataset[name] = ds;
40 this.layout.addDataset(name, ds);
43 Graph.prototype.updateDataset = function(name, value) {
44 if( window.console ) console.debug("UpdateDataset: " + name + " " + value);
45 if( this.dataset[name] ) {
46 var ds = this.dataset[name];
48 for( var i = 1; i < this.cols; i++ )
49 ds[i-1][1] = ds[i][1];
51 value = Math.abs( parseFloat(value) || 0 );
53 if( this.transform ) {
54 value = ( ds[this.cols-1][1] > 0 )
55 ? this.transform(value, ds[this.cols-1][1]) : 0.01;
58 ds[this.cols-1][1] = value;
59 this.layout.addDataset(name, ds);
63 Graph.prototype.draw = function( options ) {
65 this.plotter = new PlotKit.CanvasRenderer(
66 this.canvas, this.layout, this.options || options || {}
69 this.layout.evaluate();
70 this.plotter.render();
74 Graph.prototype.redraw = function() {
75 if( this.layout && this.plotter ) {
76 this.layout.evaluate();
78 this.plotter.render();
83 function GraphRPC(container, uri, action, interval, datasources, options, transform) {
84 this.ds = datasources;
87 this.options = options || { };
88 this.container = container;
89 this.transform = transform;
90 this.proxy = new MochiKit.JsonRpc.JsonRpcProxy(uri, [action]);
91 this.graphs = new Object();
97 window.setInterval(function(){self.requestData()}, interval);
101 GraphRPC.prototype.requestData = function() {
102 var r = this.proxy[this.action](); var self = this;
103 r.addCallback(function(r){ self.dispatchResponse(r) });
104 r.addErrback(function(e){ throw('Error: ' + e) });
107 GraphRPC.prototype.dispatchResponse = function(response) {
109 if( this.options.instances ) {
110 instances = this.options.instances;
113 instances = new Array();
114 for( var instance in response ) {
115 instances[instances.length] = instance;
119 for( var j = 0; j < instances.length; j++ ) {
120 var instance = instances[j];
122 if( this.options.separateDS ) {
123 for( var i = 0; i < this.ds.length; i += 2 ) {
124 var name = this.ds[i+1] || this.ds[i];
125 var gid = instance + '-' + name;
126 var otle = this.options.title || instance;
128 if( !this.graphs[gid] ) {
129 this.options.title = otle.replace('%s', instance) + ': ' + name;
130 this.graphs[gid] = new Graph(
131 this.container, gid, this.options, this.transform
134 this.graphs[gid].addDataset(name);
135 this.graphs[gid].draw();
136 this.options.title = otle;
140 this.graphs[gid].updateDataset(
142 ? response[instance][this.ds[i]]
143 : response[parseInt(this.ds[i])]
145 this.graphs[gid].redraw();
150 if( !this.graphs[instance] ) {
151 this.graphs[instance] = new Graph(
152 this.container, instance, this.options, this.transform
154 if( window.console ) console.debug("NG: " + instance);
156 for( var i = 0; i < this.ds.length; i += 2 ) {
157 var name = this.ds[i+1] || this.ds[i];
158 if( window.console ) console.debug("ADS: " + name);
159 this.graphs[instance].addDataset(name);
162 this.graphs[instance].draw();
165 for( var i = 0; i < this.ds.length; i += 2 ) {
166 var name = this.ds[i+1] || this.ds[i];
167 if( window.console ) console.debug("UDS: " + name + " " + response[instance][this.ds[i]]);
168 this.graphs[instance].updateDataset(
170 ? response[instance][this.ds[i]]
171 : response[parseInt(this.ds[i])]
175 this.graphs[instance].redraw();