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