| 1 |
Rickshaw.namespace('Rickshaw.Graph.Smoother'); |
| 2 |
|
| 3 |
Rickshaw.Graph.Smoother = Rickshaw.Class.create({ |
| 4 |
|
| 5 |
initialize: function(args) { |
| 6 |
|
| 7 |
this.graph = args.graph; |
| 8 |
this.element = args.element; |
| 9 |
this.aggregationScale = 1; |
| 10 |
|
| 11 |
this.build(); |
| 12 |
|
| 13 |
this.graph.stackData.hooks.data.push( { |
| 14 |
name: 'smoother', |
| 15 |
orderPosition: 50, |
| 16 |
f: this.transformer.bind(this) |
| 17 |
} ); |
| 18 |
}, |
| 19 |
|
| 20 |
build: function() { |
| 21 |
|
| 22 |
var self = this; |
| 23 |
var $ = jQuery; |
| 24 |
|
| 25 |
if (this.element) { |
| 26 |
$( function() { |
| 27 |
$(self.element).slider( { |
| 28 |
min: 1, |
| 29 |
max: 100, |
| 30 |
slide: function( event, ui ) { |
| 31 |
self.setScale(ui.value); |
| 32 |
} |
| 33 |
} ); |
| 34 |
} ); |
| 35 |
} |
| 36 |
}, |
| 37 |
|
| 38 |
setScale: function(scale) { |
| 39 |
|
| 40 |
if (scale < 1) { |
| 41 |
throw "scale out of range: " + scale; |
| 42 |
} |
| 43 |
|
| 44 |
this.aggregationScale = scale; |
| 45 |
this.graph.update(); |
| 46 |
}, |
| 47 |
|
| 48 |
transformer: function(data) { |
| 49 |
|
| 50 |
if (this.aggregationScale == 1) return data; |
| 51 |
|
| 52 |
var aggregatedData = []; |
| 53 |
|
| 54 |
data.forEach( function(seriesData) { |
| 55 |
|
| 56 |
var aggregatedSeriesData = []; |
| 57 |
|
| 58 |
while (seriesData.length) { |
| 59 |
|
| 60 |
var avgX = 0, avgY = 0; |
| 61 |
var slice = seriesData.splice(0, this.aggregationScale); |
| 62 |
|
| 63 |
slice.forEach( function(d) { |
| 64 |
avgX += d.x / slice.length; |
| 65 |
avgY += d.y / slice.length; |
| 66 |
} ); |
| 67 |
|
| 68 |
aggregatedSeriesData.push( { x: avgX, y: avgY } ); |
| 69 |
} |
| 70 |
|
| 71 |
aggregatedData.push(aggregatedSeriesData); |
| 72 |
|
| 73 |
}.bind(this) ); |
| 74 |
|
| 75 |
return aggregatedData; |
| 76 |
} |
| 77 |
}); |
| 78 |
|