From 32e03133f5364ffb25b55d8b8dd34ed2962f392a Mon Sep 17 00:00:00 2001 From: Richard Wall Date: Sat, 10 Apr 2010 11:07:08 +0100 Subject: A chart class with multiple RRD data sources and an example of stacked chart --- index.html | 73 +++++++++++++++++++++++++++++++++++++++++++++----------------- jrrd.js | 27 ++++++++++++----------- 2 files changed, 68 insertions(+), 32 deletions(-) diff --git a/index.html b/index.html index 48bd6a1..e209be2 100644 --- a/index.html +++ b/index.html @@ -7,6 +7,7 @@ + @@ -23,10 +24,12 @@ mode: 'x' }, series: { + stack: true, points: { show: false }, lines: { show: true, steps: false, + fill: 0.1, shadowSize: 0, lineWidth: 1 }, @@ -37,36 +40,66 @@ } }; - function drawChart(db, startTime, endTime) { - return db.getData(startTime, endTime) - .addCallback( - function(data) { - var plot = $.plot($('.container'), data, graphOptions); - }) - .addErrback( - function(failure) { - $('.container').text('error: ' + failure.message); - }); - } - - var db = new jrrd.RrdQueryRemote('data/localhost/load/load.rrd'); + var Chart = function(template) { + var self = this; - $(function() { - drawChart(db, new Date('7 April 2010 09:30:00'), - new Date('7 April 2010 15:00:00')); + this.template = template; + this.data = []; - $('.container').bind('plotclick', function(event, pos, item) { + this.template.bind('plotclick', function(event, pos, item) { if (item) { - console.log(['X: ' + new Date(item.datapoint[0]), 'Y: ' + item.datapoint[1]]); + console.log(['X: ' + new Date(item.datapoint[0]), + 'Y: ' + item.datapoint[1]]); } }); - $('.container').bind("plotselected", function(event, ranges) { + this.template.bind("plotselected", function(event, ranges) { var startTime = new Date(ranges.xaxis.from); var endTime = new Date(ranges.xaxis.to); - drawChart(db, startTime, endTime); + self.draw(startTime, endTime); console.log("You selected " + startTime + " to " + endTime); }); + }; + + Chart.prototype.addData = function(label, db) { + this.data.push([label, db]); + }; + + Chart.prototype.draw = function(startTime, endTime) { + var self = this; + + var results = []; + for(var i=0; i diff --git a/jrrd.js b/jrrd.js index f811223..44fdb95 100644 --- a/jrrd.js +++ b/jrrd.js @@ -43,10 +43,15 @@ jrrd.RrdQuery = function(rrd) { this.rrd = rrd; }; -jrrd.RrdQuery.prototype.getData = function(startTime, endTime) { +jrrd.RrdQuery.prototype.getData = function(startTime, endTime, dsId) { var startTimestamp = startTime.getTime()/1000; var endTimestamp = endTime.getTime()/1000; + if(dsId == null) { + dsId = 0; + } + var ds = this.rrd.getDS(dsId); + var consolidationFunc = 'AVERAGE'; var lastUpdated = this.rrd.getLastUpdate(); @@ -74,18 +79,16 @@ jrrd.RrdQuery.prototype.getData = function(startTime, endTime) { startRow = rraRowCount - parseInt((lastUpdated - startTimestamp)/step); endRow = rraRowCount - parseInt((lastUpdated - endTimestamp)/step); - returnData = []; - for(var d=this.rrd.getNrDSs()-1; d>=0; d--) { - flotData = []; - timestamp = firstUpdated + (startRow - 1) * step; - for (var i=startRow; i<=endRow; i++) { - var val = bestRRA.getEl(i, d); - flotData.push([timestamp*1000.0, val]); - timestamp += step; - } - returnData.push({label: this.rrd.getDS(d).getName(), data: flotData}); + + flotData = []; + timestamp = firstUpdated + (startRow - 1) * step; + dsIndex = ds.getIdx(); + for (var i=startRow; i<=endRow; i++) { + var val = bestRRA.getEl(i, dsIndex); + flotData.push([timestamp*1000.0, val]); + timestamp += step; } - return returnData; + return {label: ds.getName(), data: flotData}; }; -- cgit v1.1-4-g5e80