jqplot.trendline.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * jqPlot
  3. * Pure JavaScript plotting plugin using jQuery
  4. *
  5. * Version: @VERSION
  6. * Revision: @REVISION
  7. *
  8. * Copyright (c) 2009-2013 Chris Leonello
  9. * jqPlot is currently available for use in all personal or commercial projects
  10. * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL
  11. * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can
  12. * choose the license that best suits your project and use it accordingly.
  13. *
  14. * Although not required, the author would appreciate an email letting him
  15. * know of any substantial use of jqPlot. You can reach the author at:
  16. * chris at jqplot dot com or see http://www.jqplot.com/info.php .
  17. *
  18. * If you are feeling kind and generous, consider supporting the project by
  19. * making a donation at: http://www.jqplot.com/donate.php .
  20. *
  21. * sprintf functions contained in jqplot.sprintf.js by Ash Searle:
  22. *
  23. * version 2007.04.27
  24. * author Ash Searle
  25. * http://hexmen.com/blog/2007/03/printf-sprintf/
  26. * http://hexmen.com/js/sprintf.js
  27. * The author (Ash Searle) has placed this code in the public domain:
  28. * "This code is unrestricted: you are free to use it however you like."
  29. *
  30. */
  31. (function($) {
  32. /**
  33. * Class: $.jqplot.Trendline
  34. * Plugin which will automatically compute and draw trendlines for plotted data.
  35. */
  36. $.jqplot.Trendline = function() {
  37. // Group: Properties
  38. // prop: show
  39. // Wether or not to show the trend line.
  40. this.show = $.jqplot.config.enablePlugins;
  41. // prop: color
  42. // CSS color spec for the trend line.
  43. // By default this wil be the same color as the primary line.
  44. this.color = '#666666';
  45. // prop: renderer
  46. // Renderer to use to draw the trend line.
  47. // The data series that is plotted may not be rendered as a line.
  48. // Therefore, we use our own line renderer here to draw a trend line.
  49. this.renderer = new $.jqplot.LineRenderer();
  50. // prop: rendererOptions
  51. // Options to pass to the line renderer.
  52. // By default, markers are not shown on trend lines.
  53. this.rendererOptions = {marker:{show:false}};
  54. // prop: label
  55. // Label for the trend line to use in the legend.
  56. this.label = '';
  57. // prop: type
  58. // Either 'exponential', 'exp', or 'linear'.
  59. this.type = 'linear';
  60. // prop: shadow
  61. // true or false, whether or not to show the shadow.
  62. this.shadow = true;
  63. // prop: markerRenderer
  64. // Renderer to use to draw markers on the line.
  65. // I think this is wrong.
  66. this.markerRenderer = {show:false};
  67. // prop: lineWidth
  68. // Width of the trend line.
  69. this.lineWidth = 1.5;
  70. // prop: shadowAngle
  71. // Angle of the shadow on the trend line.
  72. this.shadowAngle = 45;
  73. // prop: shadowOffset
  74. // pixel offset for each stroke of the shadow.
  75. this.shadowOffset = 1.0;
  76. // prop: shadowAlpha
  77. // Alpha transparency of the shadow.
  78. this.shadowAlpha = 0.07;
  79. // prop: shadowDepth
  80. // number of strokes to make of the shadow.
  81. this.shadowDepth = 3;
  82. this.isTrendline = true;
  83. };
  84. $.jqplot.postSeriesInitHooks.push(parseTrendLineOptions);
  85. $.jqplot.postDrawSeriesHooks.push(drawTrendline);
  86. $.jqplot.addLegendRowHooks.push(addTrendlineLegend);
  87. // called witin scope of the legend object
  88. // current series passed in
  89. // must return null or an object {label:label, color:color}
  90. function addTrendlineLegend(series) {
  91. var ret = null;
  92. if (series.trendline && series.trendline.show) {
  93. var lt = series.trendline.label.toString();
  94. if (lt) {
  95. ret = {label:lt, color:series.trendline.color};
  96. }
  97. }
  98. return ret;
  99. }
  100. // called within scope of a series
  101. function parseTrendLineOptions (target, data, seriesDefaults, options, plot) {
  102. if (this._type && (this._type === 'line' || this._type == 'bar')) {
  103. this.trendline = new $.jqplot.Trendline();
  104. options = options || {};
  105. $.extend(true, this.trendline, {color:this.color}, seriesDefaults.trendline, options.trendline);
  106. this.trendline.renderer.init.call(this.trendline, null);
  107. }
  108. }
  109. // called within scope of series object
  110. function drawTrendline(sctx, options) {
  111. // if we have options, merge trendline options in with precedence
  112. options = $.extend(true, {}, this.trendline, options);
  113. if (this.trendline && options.show) {
  114. var fit;
  115. // this.renderer.setGridData.call(this);
  116. var data = options.data || this.data;
  117. fit = fitData(data, this.trendline.type);
  118. var gridData = options.gridData || this.renderer.makeGridData.call(this, fit.data);
  119. this.trendline.renderer.draw.call(this.trendline, sctx, gridData, {showLine:true, shadow:this.trendline.shadow});
  120. }
  121. }
  122. function regression(x, y, typ) {
  123. var type = (typ == null) ? 'linear' : typ;
  124. var N = x.length;
  125. var slope;
  126. var intercept;
  127. var SX = 0;
  128. var SY = 0;
  129. var SXX = 0;
  130. var SXY = 0;
  131. var SYY = 0;
  132. var Y = [];
  133. var X = [];
  134. if (type == 'linear') {
  135. X = x;
  136. Y = y;
  137. }
  138. else if (type == 'exp' || type == 'exponential') {
  139. for ( var i=0; i<y.length; i++) {
  140. // ignore points <= 0, log undefined.
  141. if (y[i] <= 0) {
  142. N--;
  143. }
  144. else {
  145. X.push(x[i]);
  146. Y.push(Math.log(y[i]));
  147. }
  148. }
  149. }
  150. for ( var i = 0; i < N; i++) {
  151. SX = SX + X[i];
  152. SY = SY + Y[i];
  153. SXY = SXY + X[i]* Y[i];
  154. SXX = SXX + X[i]* X[i];
  155. SYY = SYY + Y[i]* Y[i];
  156. }
  157. slope = (N*SXY - SX*SY)/(N*SXX - SX*SX);
  158. intercept = (SY - slope*SX)/N;
  159. return [slope, intercept];
  160. }
  161. function linearRegression(X,Y) {
  162. var ret;
  163. ret = regression(X,Y,'linear');
  164. return [ret[0],ret[1]];
  165. }
  166. function expRegression(X,Y) {
  167. var ret;
  168. var x = X;
  169. var y = Y;
  170. ret = regression(x, y,'exp');
  171. var base = Math.exp(ret[0]);
  172. var coeff = Math.exp(ret[1]);
  173. return [base, coeff];
  174. }
  175. function fitData(data, typ) {
  176. var type = (typ == null) ? 'linear' : typ;
  177. var ret;
  178. var res;
  179. var x = [];
  180. var y = [];
  181. var ypred = [];
  182. for (i=0; i<data.length; i++){
  183. if (data[i] != null && data[i][0] != null && data[i][1] != null) {
  184. x.push(data[i][0]);
  185. y.push(data[i][1]);
  186. }
  187. }
  188. if (type == 'linear') {
  189. ret = linearRegression(x,y);
  190. for ( var i=0; i<x.length; i++){
  191. res = ret[0]*x[i] + ret[1];
  192. ypred.push([x[i], res]);
  193. }
  194. }
  195. else if (type == 'exp' || type == 'exponential') {
  196. ret = expRegression(x,y);
  197. for ( var i=0; i<x.length; i++){
  198. res = ret[1]*Math.pow(ret[0],x[i]);
  199. ypred.push([x[i], res]);
  200. }
  201. }
  202. return {data: ypred, slope: ret[0], intercept: ret[1]};
  203. }
  204. })(jQuery);