Skip to content

Commit

Permalink
Merge pull request shutterstock#14 from travisbeck/master
Browse files Browse the repository at this point in the history
Added Fixed Duration Series subclass
  • Loading branch information
David Chester committed Dec 20, 2011
2 parents f3ab077 + d0847d7 commit 3f482fb
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 93 deletions.
81 changes: 81 additions & 0 deletions src/js/Rickshaw.Series.FixedDuration.js
@@ -0,0 +1,81 @@
Rickshaw.namespace('Rickshaw.Series.FixedDuration');

Rickshaw.Series.FixedDuration = function(data, palette, options) {

this.initialize(data, palette, options);
}

Rickshaw.Series.FixedDuration.prototype = new Rickshaw.Series;

Rickshaw.Series.FixedDuration.prototype.constructor = Rickshaw.Series.FixedDuration;

Rickshaw.Series.FixedDuration.prototype.initialize = function (data, palette, options) {
var self = this;
options = options || {}

if (typeof(options.timeInterval) === 'undefined') {
throw('FixedDuration series requires timeInterval');
}
if (typeof(options.maxDataPoints) === 'undefined') {
throw('FixedDuration series requires maxDataPoints');
}

self.palette = new Rickshaw.Color.Palette(palette);
self.timeBase = typeof(options.timeBase) === 'undefined' ? Math.floor(new Date().getTime() / 1000) : options.timeBase;
self.setTimeInterval(options.timeInterval);
if (self[0] && self[0].data && self[0].data.length) {
self.currentSize = self[0].data.length;
self.currentIndex = self[0].data.length;
} else {
self.currentSize = 0;
self.currentIndex = 0;
}
self.maxDataPoints = options.maxDataPoints;

// reset timeBase for zero-filled values if needed
if ((typeof(self.maxDataPoints) !== 'undefined') && (self.currentSize < self.maxDataPoints)) {
self.timeBase -= Math.floor((self.maxDataPoints - self.currentSize) * self.timeInterval);
}

if (data && (typeof(data) == "object") && (data instanceof Array)) {
data.forEach( function (item) { self.addItem(item) } );
self.currentSize += 1;
self.currentIndex += 1;
}

// zero-fill up to maxDataPoints size if we don't have that much data yet
if ((typeof(self.maxDataPoints) !== 'undefined') && (self.currentSize < self.maxDataPoints)) {
for (var i = self.maxDataPoints - self.currentSize; i > 0; i--) {
self.currentSize += 1;
self.currentIndex += 1;
self.forEach( function (item) {
item.data.splice(1, 0, { x: (i * self.timeInterval || 1) + self.timeBase, y: 0 });
} );
}
}
}

Rickshaw.Series.FixedDuration.prototype.addData = function(data) {
// call the parent
Rickshaw.Series.prototype.addData.call(this, data);
this.currentSize += 1;
this.currentIndex += 1;

if (this.maxDataPoints !== undefined) {
while (this.currentSize > this.maxDataPoints) {
this.dropData();
}
}
}

Rickshaw.Series.FixedDuration.prototype.dropData = function() {
var self = this;
self.forEach( function(item) {
item.data.splice(0, 1);
} );
self.currentSize -= 1;
}

Rickshaw.Series.FixedDuration.prototype.getIndex = function () {
return this.currentIndex;
}
194 changes: 101 additions & 93 deletions src/js/Rickshaw.Series.js
Expand Up @@ -2,124 +2,132 @@ Rickshaw.namespace('Rickshaw.Series');

Rickshaw.Series = function(data, palette, options) {

var self = this;

this.initialize = function (data, palette, options) {
options = options || {}
this.initialize(data, palette, options);
}

self.palette = new Rickshaw.Color.Palette(palette);
self.timeBase = typeof(options.timeBase) === 'undefined' ? Math.floor(new Date().getTime() / 1000) : options.timeBase;
Rickshaw.Series.prototype = new Array;

if (data && (typeof(data) == "object") && (data instanceof Array)) {
data.forEach( function (item) { self.addItem(item) } );
}
}
Rickshaw.Series.prototype.constructor = Rickshaw.Series;

this.addItem = function(item) {
if (typeof(item.name) === 'undefined') {
throw('addItem() needs a name');
}

item.color = (item.color || self.palette.color(item.name));
item.data = (item.data || []);

// backfill, if necissary
if ((item.data.length == 0) && (self.getIndex() > 0)) {
self[0].data.forEach( function (plot) {
item.data.push({ x: plot.x, y: 0 });
} );
} else {
// otherwise add a first plot
item.data.push({ x: self.timeBase, y: 0 });
}
Rickshaw.Series.prototype.initialize = function (data, palette, options) {
var self = this;
options = options || {}

self.push(item);
self.palette = new Rickshaw.Color.Palette(palette);
self.timeBase = typeof(options.timeBase) === 'undefined' ? Math.floor(new Date().getTime() / 1000) : options.timeBase;

if (self.legend) {
self.legend.addLine(self.itemByName(item.name));
}
if (data && (typeof(data) == "object") && (data instanceof Array)) {
data.forEach( function (item) { self.addItem(item) } );
}
}

this.addData = function(data) {
var index = this.getIndex();
Rickshaw.Series.prototype.addItem = function(item) {
var self = this;
if (typeof(item.name) === 'undefined') {
throw('addItem() needs a name');
}

Rickshaw.keys(data).forEach( function (name) {
if (! self.itemByName(name)) {
self.addItem({ name: name });
}
} );
item.color = (item.color || self.palette.color(item.name));
item.data = (item.data || []);

self.forEach( function (item) {
item.data.push({ x: (index * self.timeInterval || 1) + self.timeBase, y: (data[item.name] || 0) });
// backfill, if necessary
if ((item.data.length == 0) && (self.getIndex() > 0)) {
self[0].data.forEach( function (plot) {
item.data.push({ x: plot.x, y: 0 });
} );
} else {
// otherwise add a first plot
console.log(self.timeBase);
item.data.push({ x: self.timeBase, y: 0 });
}

this.getIndex = function () {
return (self[0] && self[0].data && self[0].data.length) ? self[0].data.length : 0;
}
self.push(item);

this.itemByName = function (name) {
var ret;
self.forEach( function (item) {
if (item.name == name) { ret = item }
} );
return ret;
if (self.legend) {
self.legend.addLine(self.itemByName(item.name));
}
}

this.setTimeInterval = function (iv) {
self.timeInterval = parseInt(iv/1000);
}
Rickshaw.Series.prototype.addData = function(data) {
var self = this;
var index = this.getIndex();

this.setTimeBase = function (t) {
self.timeBase = t;
}
Rickshaw.keys(data).forEach( function (name) {
if (! self.itemByName(name)) {
self.addItem({ name: name });
}
} );

self.forEach( function (item) {
item.data.push({ x: (index * self.timeInterval || 1) + self.timeBase, y: (data[item.name] || 0) });
} );
}

Rickshaw.Series.prototype.getIndex = function () {
var self = this;
return (self[0] && self[0].data && self[0].data.length) ? self[0].data.length : 0;
}

Rickshaw.Series.prototype.itemByName = function (name) {
var self = this;
var ret;
self.forEach( function (item) {
if (item.name == name) { ret = item }
} );
return ret;
}

Rickshaw.Series.prototype.setTimeInterval = function (iv) {
this.timeInterval = parseInt(iv/1000);
}

this.dump = function () {
var data = {
timeBase: self.timeBase,
timeInterval: self.timeInterval,
items: [],
Rickshaw.Series.prototype.setTimeBase = function (t) {
this.timeBase = t;
}

Rickshaw.Series.prototype.dump = function () {
var self = this;
var data = {
timeBase: self.timeBase,
timeInterval: self.timeInterval,
items: [],
};
self.forEach( function (item) {
var newItem = {
color: item.color,
name: item.name,
data: []
};
self.forEach( function (item) {
var newItem = {
color: item.color,
name: item.name,
data: []
};

item.data.forEach( function (plot) {
newItem.data.push({ x: plot.x, y: plot.y });
} );

data.items.push(newItem);
} );
return data;
}

this.load = function (data) {
if (data.timeInterval) {
self.timeInterval = data.timeInterval;
}
item.data.forEach( function (plot) {
newItem.data.push({ x: plot.x, y: plot.y });
} );

if (data.timeBase) {
self.timeBase = data.timeBase;
}
data.items.push(newItem);
} );
return data;
}

if (data.items) {
data.items.forEach( function (item) {
self.push(item);
Rickshaw.Series.prototype.load = function (data) {
var self = this;
if (data.timeInterval) {
self.timeInterval = data.timeInterval;
}

if (self.legend) {
self.legend.addLine(self.itemByName(item.name));
}
} );
}
if (data.timeBase) {
self.timeBase = data.timeBase;
}

this.initialize(data, palette, options);
}
if (data.items) {
data.items.forEach( function (item) {
self.push(item);

Rickshaw.Series.prototype = new Array;
if (self.legend) {
self.legend.addLine(self.itemByName(item.name));
}
} );
}
}

Rickshaw.Series.zeroFill = function(series) {

Expand Down

0 comments on commit 3f482fb

Please sign in to comment.