| 1 |
Rickshaw.namespace('Rickshaw.Series'); |
| 2 |
|
| 3 |
Rickshaw.Series = Rickshaw.Class.create( Array, { |
| 4 |
|
| 5 |
initialize: function (data, palette, options) { |
| 6 |
|
| 7 |
options = options || {}; |
| 8 |
|
| 9 |
this.palette = new Rickshaw.Color.Palette(palette); |
| 10 |
|
| 11 |
this.timeBase = typeof(options.timeBase) === 'undefined' ? |
| 12 |
Math.floor(new Date().getTime() / 1000) : |
| 13 |
options.timeBase; |
| 14 |
|
| 15 |
var timeInterval = typeof(options.timeInterval) == 'undefined' ? |
| 16 |
1000 : |
| 17 |
options.timeInterval; |
| 18 |
|
| 19 |
this.setTimeInterval(timeInterval); |
| 20 |
|
| 21 |
if (data && (typeof(data) == "object") && Array.isArray(data)) { |
| 22 |
data.forEach( function(item) { this.addItem(item) }, this ); |
| 23 |
} |
| 24 |
}, |
| 25 |
|
| 26 |
addItem: function(item) { |
| 27 |
|
| 28 |
if (typeof(item.name) === 'undefined') { |
| 29 |
throw('addItem() needs a name'); |
| 30 |
} |
| 31 |
|
| 32 |
item.color = (item.color || this.palette.color(item.name)); |
| 33 |
item.data = (item.data || []); |
| 34 |
|
| 35 |
// backfill, if necessary |
| 36 |
if ((item.data.length === 0) && this.length && (this.getIndex() > 0)) { |
| 37 |
this[0].data.forEach( function(plot) { |
| 38 |
item.data.push({ x: plot.x, y: 0 }); |
| 39 |
} ); |
| 40 |
} else if (item.data.length === 0) { |
| 41 |
item.data.push({ x: this.timeBase - (this.timeInterval || 0), y: 0 }); |
| 42 |
} |
| 43 |
|
| 44 |
this.push(item); |
| 45 |
|
| 46 |
if (this.legend) { |
| 47 |
this.legend.addLine(this.itemByName(item.name)); |
| 48 |
} |
| 49 |
}, |
| 50 |
|
| 51 |
addData: function(data, x) { |
| 52 |
|
| 53 |
var index = this.getIndex(); |
| 54 |
|
| 55 |
Rickshaw.keys(data).forEach( function(name) { |
| 56 |
if (! this.itemByName(name)) { |
| 57 |
this.addItem({ name: name }); |
| 58 |
} |
| 59 |
}, this ); |
| 60 |
|
| 61 |
this.forEach( function(item) { |
| 62 |
item.data.push({ |
| 63 |
x: x || (index * this.timeInterval || 1) + this.timeBase, |
| 64 |
y: (data[item.name] || 0) |
| 65 |
}); |
| 66 |
}, this ); |
| 67 |
}, |
| 68 |
|
| 69 |
getIndex: function () { |
| 70 |
return (this[0] && this[0].data && this[0].data.length) ? this[0].data.length : 0; |
| 71 |
}, |
| 72 |
|
| 73 |
itemByName: function(name) { |
| 74 |
|
| 75 |
for (var i = 0; i < this.length; i++) { |
| 76 |
if (this[i].name == name) |
| 77 |
return this[i]; |
| 78 |
} |
| 79 |
}, |
| 80 |
|
| 81 |
setTimeInterval: function(iv) { |
| 82 |
this.timeInterval = iv / 1000; |
| 83 |
}, |
| 84 |
|
| 85 |
setTimeBase: function (t) { |
| 86 |
this.timeBase = t; |
| 87 |
}, |
| 88 |
|
| 89 |
dump: function() { |
| 90 |
|
| 91 |
var data = { |
| 92 |
timeBase: this.timeBase, |
| 93 |
timeInterval: this.timeInterval, |
| 94 |
items: [] |
| 95 |
}; |
| 96 |
|
| 97 |
this.forEach( function(item) { |
| 98 |
|
| 99 |
var newItem = { |
| 100 |
color: item.color, |
| 101 |
name: item.name, |
| 102 |
data: [] |
| 103 |
}; |
| 104 |
|
| 105 |
item.data.forEach( function(plot) { |
| 106 |
newItem.data.push({ x: plot.x, y: plot.y }); |
| 107 |
} ); |
| 108 |
|
| 109 |
data.items.push(newItem); |
| 110 |
} ); |
| 111 |
|
| 112 |
return data; |
| 113 |
}, |
| 114 |
|
| 115 |
load: function(data) { |
| 116 |
|
| 117 |
if (data.timeInterval) { |
| 118 |
this.timeInterval = data.timeInterval; |
| 119 |
} |
| 120 |
|
| 121 |
if (data.timeBase) { |
| 122 |
this.timeBase = data.timeBase; |
| 123 |
} |
| 124 |
|
| 125 |
if (data.items) { |
| 126 |
data.items.forEach( function(item) { |
| 127 |
this.push(item); |
| 128 |
if (this.legend) { |
| 129 |
this.legend.addLine(this.itemByName(item.name)); |
| 130 |
} |
| 131 |
|
| 132 |
}, this ); |
| 133 |
} |
| 134 |
} |
| 135 |
} ); |
| 136 |
|
| 137 |
Rickshaw.Series.zeroFill = function(series) { |
| 138 |
Rickshaw.Series.fill(series, 0); |
| 139 |
}; |
| 140 |
|
| 141 |
Rickshaw.Series.fill = function(series, fill) { |
| 142 |
|
| 143 |
var x; |
| 144 |
var i = 0; |
| 145 |
|
| 146 |
var data = series.map( function(s) { return s.data } ); |
| 147 |
|
| 148 |
while ( i < Math.max.apply(null, data.map( function(d) { return d.length } )) ) { |
| 149 |
|
| 150 |
x = Math.min.apply( null, |
| 151 |
data |
| 152 |
.filter(function(d) { return d[i] }) |
| 153 |
.map(function(d) { return d[i].x }) |
| 154 |
); |
| 155 |
|
| 156 |
data.forEach( function(d) { |
| 157 |
if (!d[i] || d[i].x != x) { |
| 158 |
d.splice(i, 0, { x: x, y: fill }); |
| 159 |
} |
| 160 |
} ); |
| 161 |
|
| 162 |
i++; |
| 163 |
} |
| 164 |
}; |
| 165 |
|