PageRenderTime 223ms CodeModel.GetById 100ms app.highlight 6ms RepoModel.GetById 115ms app.codeStats 0ms

/timeplot/scripts/processor.js

http://showslow.googlecode.com/
JavaScript | 122 lines | 67 code | 22 blank | 33 comment | 0 complexity | 74513c3e3f1943b8ff0e5712bee0af2d MD5 | raw file
  1/**
  2 * Processing Data Source
  3 * 
  4 * @fileOverview Processing Data Source and Operators
  5 * @name Processor
  6 */
  7
  8/* -----------------------------------------------------------------------------
  9 * Operators
 10 * 
 11 * These are functions that can be used directly as Timeplot.Processor operators
 12 * ----------------------------------------------------------------------------- */
 13
 14Timeplot.Operator = { 
 15
 16    /**
 17     * This is the operator used when you want to draw the cumulative sum
 18     * of a time series and not, for example, their daily values.
 19     */
 20    sum: function(data, params) {
 21        return Timeplot.Math.integral(data.values);
 22    },
 23
 24    /**
 25     * This is the operator that is used to 'smooth' a given time series
 26     * by taking the average value of a moving window centered around
 27     * each value. The size of the moving window is influenced by the 'size'
 28     * parameters in the params map.
 29     */
 30    average: function(data, params) {
 31        var size = ("size" in params) ? params.size : 30;
 32        var result = Timeplot.Math.movingAverage(data.values, size);
 33        return result;
 34    }
 35}
 36
 37/*==================================================
 38 *  Processing Data Source
 39 *==================================================*/
 40
 41/**
 42 * A Processor is a special DataSource that can apply an Operator
 43 * to the DataSource values and thus return a different one.
 44 * 
 45 * @constructor
 46 */
 47Timeplot.Processor = function(dataSource, operator, params) {
 48    this._dataSource = dataSource;
 49    this._operator = operator;
 50    this._params = params;
 51
 52    this._data = {
 53        times: new Array(),
 54        values: new Array()
 55    };
 56
 57    this._range = {
 58        earliestDate: null,
 59        latestDate: null,
 60        min: 0,
 61        max: 0
 62    };
 63
 64    var processor = this;
 65    this._processingListener = {
 66        onAddMany: function() { processor._process(); },
 67        onClear:   function() { processor._clear(); }
 68    }
 69    this.addListener(this._processingListener);
 70};
 71
 72Timeplot.Processor.prototype = {
 73
 74    _clear: function() {
 75        this.removeListener(this._processingListener);
 76        this._dataSource._clear();
 77    },
 78
 79    _process: function() {
 80        // this method requires the dataSource._process() method to be
 81        // called first as to setup the data and range used below
 82        // this should be guaranteed by the order of the listener registration  
 83
 84        var data = this._dataSource.getData();
 85        var range = this._dataSource.getRange();
 86
 87        var newValues = this._operator(data, this._params);
 88        var newValueRange = Timeplot.Math.range(newValues);
 89
 90        this._data = {
 91            times: data.times,
 92            values: newValues
 93        };
 94
 95        this._range = {
 96            earliestDate: range.earliestDate,
 97            latestDate: range.latestDate,
 98            min: newValueRange.min,
 99            max: newValueRange.max
100        };
101    },
102
103    getRange: function() {
104        return this._range;
105    },
106
107    getData: function() {
108        return this._data;
109    },
110    
111    getValue: Timeplot.DataSource.prototype.getValue,
112
113    getClosestValidTime: Timeplot.DataSource.prototype.getClosestValidTime,
114
115    addListener: function(listener) {
116        this._dataSource.addListener(listener);
117    },
118
119    removeListener: function(listener) {
120        this._dataSource.removeListener(listener);
121    }
122}