-function Graph(container, id, options, transform) {
+function Graph(container, id, options, transform, legend) {
if( !options ) options = { };
this.id = id;
this.options = options;
this.transform = transform;
this.dataset = {};
-
+ this.legend = legend;
+ this.lastvalue = {};
+
+ var name = (options.instanceNames && options.instanceNames[id])
+ ? options.instanceNames[id] : id;
var graph = document.createElement('div');
var label = document.createElement('h2');
label.innerHTML = options.title
- ? options.title.replace("%s", id ) : id;
+ ? options.title.replace("%s", name) : name;
container.appendChild( label );
container.appendChild( graph );
}
Graph.prototype.addDataset = function(name, ds) {
- if( window.console ) console.debug("AddDataset: " + name);
if( !this.layout ) {
this.layout = new PlotKit.Layout( this.type, this.options );
}
}
Graph.prototype.updateDataset = function(name, value) {
- if( window.console ) console.debug("UpdateDataset: " + name + " " + value);
if( this.dataset[name] ) {
var ds = this.dataset[name];
value = Math.abs( parseFloat(value) || 0 );
if( this.transform ) {
- value = ( ds[this.cols-1][1] > 0 )
- ? this.transform(value, ds[this.cols-1][1]) : 0.01;
+ var orgvalue = value;
+ value = (this.lastvalue[name]) ? this.transform(value, this.lastvalue[name]) : 0;
+ this.lastvalue[name] = orgvalue;
}
ds[this.cols-1][1] = value;
this.layout.evaluate();
this.plotter.render();
+
+ legend_opt = {
+ "legendStyle": 'li'
+ };
+
+ legend = new LegendRenderer(this.legend, this.layout, legend_opt);
+ legend.render();
}
}
}
-function GraphRPC(container, uri, action, interval, datasources, options, transform) {
+function GraphRPC(container, uri, action, interval, datasources, options, transform, legend) {
this.ds = datasources;
this.uri = uri
this.action = action;
this.transform = transform;
this.proxy = new MochiKit.JsonRpc.JsonRpcProxy(uri, [action]);
this.graphs = new Object();
+ this.legend = legend;
this.requestData();
if( !this.graphs[gid] ) {
this.options.title = otle.replace('%s', instance) + ': ' + name;
this.graphs[gid] = new Graph(
- this.container, gid, this.options, this.transform
+ this.container, gid, this.options, this.transform, this.legend
);
this.graphs[gid].addDataset(name);
}
else
{
- this.graphs[gid].updateDataset(
- name, instance
+ var datum = null;
+ if (typeof (this.ds[i]) == "function") {
+ datum = this.ds[i](
+ instance ? response[instance] : response
+ );
+ } else {
+ datum = instance
? response[instance][this.ds[i]]
- : response[parseInt(this.ds[i])]
+ : response[this.ds[i]]
+ }
+ this.graphs[gid].updateDataset(
+ name, datum
);
this.graphs[gid].redraw();
}
}
}
else {
- if( !this.graphs[instance] ) {
- this.graphs[instance] = new Graph(
- this.container, instance, this.options, this.transform
+ var gid = instance || 'livegraph';
+ if( !this.graphs[gid] ) {
+ this.graphs[gid] = new Graph(
+ this.container, gid, this.options, this.transform, this.legend
);
- if( window.console ) console.debug("NG: " + instance);
for( var i = 0; i < this.ds.length; i += 2 ) {
var name = this.ds[i+1] || this.ds[i];
- if( window.console ) console.debug("ADS: " + name);
- this.graphs[instance].addDataset(name);
+ this.graphs[gid].addDataset(name);
}
- this.graphs[instance].draw();
+ this.graphs[gid].draw();
}
else {
for( var i = 0; i < this.ds.length; i += 2 ) {
var name = this.ds[i+1] || this.ds[i];
- if( window.console ) console.debug("UDS: " + name + " " + response[instance][this.ds[i]]);
- this.graphs[instance].updateDataset(
- name, instance
+ var datum = null;
+ if (typeof (this.ds[i]) == "function") {
+ datum = this.ds[i](
+ instance ? response[instance] : response
+ );
+ } else {
+ datum = instance
? response[instance][this.ds[i]]
- : response[parseInt(this.ds[i])]
+ : response[this.ds[i]]
+ }
+ this.graphs[gid].updateDataset(
+ name, datum
);
}
- this.graphs[instance].redraw();
+ this.graphs[gid].redraw();
}
}
}