| 1 |
Rickshaw.namespace('Rickshaw.Series.FixedDuration'); |
| 2 |
|
| 3 |
Rickshaw.Series.FixedDuration = Rickshaw.Class.create(Rickshaw.Series, { |
| 4 |
|
| 5 |
initialize: function (data, palette, options) { |
| 6 |
|
| 7 |
options = options || {}; |
| 8 |
|
| 9 |
if (typeof(options.timeInterval) === 'undefined') { |
| 10 |
throw new Error('FixedDuration series requires timeInterval'); |
| 11 |
} |
| 12 |
|
| 13 |
if (typeof(options.maxDataPoints) === 'undefined') { |
| 14 |
throw new Error('FixedDuration series requires maxDataPoints'); |
| 15 |
} |
| 16 |
|
| 17 |
this.palette = new Rickshaw.Color.Palette(palette); |
| 18 |
this.timeBase = typeof(options.timeBase) === 'undefined' ? Math.floor(new Date().getTime() / 1000) : options.timeBase; |
| 19 |
this.setTimeInterval(options.timeInterval); |
| 20 |
|
| 21 |
if (this[0] && this[0].data && this[0].data.length) { |
| 22 |
this.currentSize = this[0].data.length; |
| 23 |
this.currentIndex = this[0].data.length; |
| 24 |
} else { |
| 25 |
this.currentSize = 0; |
| 26 |
this.currentIndex = 0; |
| 27 |
} |
| 28 |
|
| 29 |
this.maxDataPoints = options.maxDataPoints; |
| 30 |
|
| 31 |
|
| 32 |
if (data && (typeof(data) == "object") && Array.isArray(data)) { |
| 33 |
data.forEach( function (item) { this.addItem(item) }, this ); |
| 34 |
this.currentSize += 1; |
| 35 |
this.currentIndex += 1; |
| 36 |
} |
| 37 |
|
| 38 |
// reset timeBase for zero-filled values if needed |
| 39 |
this.timeBase -= (this.maxDataPoints - this.currentSize) * this.timeInterval; |
| 40 |
|
| 41 |
// zero-fill up to maxDataPoints size if we don't have that much data yet |
| 42 |
if ((typeof(this.maxDataPoints) !== 'undefined') && (this.currentSize < this.maxDataPoints)) { |
| 43 |
for (var i = this.maxDataPoints - this.currentSize - 1; i > 1; i--) { |
| 44 |
this.currentSize += 1; |
| 45 |
this.currentIndex += 1; |
| 46 |
this.forEach( function (item) { |
| 47 |
item.data.unshift({ x: ((i-1) * this.timeInterval || 1) + this.timeBase, y: 0, i: i }); |
| 48 |
}, this ); |
| 49 |
} |
| 50 |
} |
| 51 |
}, |
| 52 |
|
| 53 |
addData: function($super, data, x) { |
| 54 |
|
| 55 |
$super(data, x); |
| 56 |
|
| 57 |
this.currentSize += 1; |
| 58 |
this.currentIndex += 1; |
| 59 |
|
| 60 |
if (this.maxDataPoints !== undefined) { |
| 61 |
while (this.currentSize > this.maxDataPoints) { |
| 62 |
this.dropData(); |
| 63 |
} |
| 64 |
} |
| 65 |
}, |
| 66 |
|
| 67 |
dropData: function() { |
| 68 |
|
| 69 |
this.forEach(function(item) { |
| 70 |
item.data.splice(0, 1); |
| 71 |
} ); |
| 72 |
|
| 73 |
this.currentSize -= 1; |
| 74 |
}, |
| 75 |
|
| 76 |
getIndex: function () { |
| 77 |
return this.currentIndex; |
| 78 |
} |
| 79 |
} ); |
| 80 |
|