123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- (function($) {
-
-
- $.jqplot.Trendline = function() {
-
-
-
-
- this.show = $.jqplot.config.enablePlugins;
-
-
-
- this.color = '#666666';
-
-
-
-
- this.renderer = new $.jqplot.LineRenderer();
-
-
-
- this.rendererOptions = {marker:{show:false}};
-
-
- this.label = '';
-
-
- this.type = 'linear';
-
-
- this.shadow = true;
-
-
-
- this.markerRenderer = {show:false};
-
-
- this.lineWidth = 1.5;
-
-
- this.shadowAngle = 45;
-
-
- this.shadowOffset = 1.0;
-
-
- this.shadowAlpha = 0.07;
-
-
- this.shadowDepth = 3;
- this.isTrendline = true;
-
- };
-
- $.jqplot.postSeriesInitHooks.push(parseTrendLineOptions);
- $.jqplot.postDrawSeriesHooks.push(drawTrendline);
- $.jqplot.addLegendRowHooks.push(addTrendlineLegend);
-
-
-
-
- function addTrendlineLegend(series) {
- var ret = null;
- if (series.trendline && series.trendline.show) {
- var lt = series.trendline.label.toString();
- if (lt) {
- ret = {label:lt, color:series.trendline.color};
- }
- }
- return ret;
- }
-
- function parseTrendLineOptions (target, data, seriesDefaults, options, plot) {
- if (this._type && (this._type === 'line' || this._type == 'bar')) {
- this.trendline = new $.jqplot.Trendline();
- options = options || {};
- $.extend(true, this.trendline, {color:this.color}, seriesDefaults.trendline, options.trendline);
- this.trendline.renderer.init.call(this.trendline, null);
- }
- }
-
-
- function drawTrendline(sctx, options) {
-
- options = $.extend(true, {}, this.trendline, options);
- if (this.trendline && options.show) {
- var fit;
-
- var data = options.data || this.data;
- fit = fitData(data, this.trendline.type);
- var gridData = options.gridData || this.renderer.makeGridData.call(this, fit.data);
- this.trendline.renderer.draw.call(this.trendline, sctx, gridData, {showLine:true, shadow:this.trendline.shadow});
- }
- }
-
- function regression(x, y, typ) {
- var type = (typ == null) ? 'linear' : typ;
- var N = x.length;
- var slope;
- var intercept;
- var SX = 0;
- var SY = 0;
- var SXX = 0;
- var SXY = 0;
- var SYY = 0;
- var Y = [];
- var X = [];
-
- if (type == 'linear') {
- X = x;
- Y = y;
- }
- else if (type == 'exp' || type == 'exponential') {
- for ( var i=0; i<y.length; i++) {
-
- if (y[i] <= 0) {
- N--;
- }
- else {
- X.push(x[i]);
- Y.push(Math.log(y[i]));
- }
- }
- }
- for ( var i = 0; i < N; i++) {
- SX = SX + X[i];
- SY = SY + Y[i];
- SXY = SXY + X[i]* Y[i];
- SXX = SXX + X[i]* X[i];
- SYY = SYY + Y[i]* Y[i];
- }
- slope = (N*SXY - SX*SY)/(N*SXX - SX*SX);
- intercept = (SY - slope*SX)/N;
- return [slope, intercept];
- }
- function linearRegression(X,Y) {
- var ret;
- ret = regression(X,Y,'linear');
- return [ret[0],ret[1]];
- }
- function expRegression(X,Y) {
- var ret;
- var x = X;
- var y = Y;
- ret = regression(x, y,'exp');
- var base = Math.exp(ret[0]);
- var coeff = Math.exp(ret[1]);
- return [base, coeff];
- }
- function fitData(data, typ) {
- var type = (typ == null) ? 'linear' : typ;
- var ret;
- var res;
- var x = [];
- var y = [];
- var ypred = [];
-
- for (i=0; i<data.length; i++){
- if (data[i] != null && data[i][0] != null && data[i][1] != null) {
- x.push(data[i][0]);
- y.push(data[i][1]);
- }
- }
-
- if (type == 'linear') {
- ret = linearRegression(x,y);
- for ( var i=0; i<x.length; i++){
- res = ret[0]*x[i] + ret[1];
- ypred.push([x[i], res]);
- }
- }
- else if (type == 'exp' || type == 'exponential') {
- ret = expRegression(x,y);
- for ( var i=0; i<x.length; i++){
- res = ret[1]*Math.pow(ret[0],x[i]);
- ypred.push([x[i], res]);
- }
- }
- return {data: ypred, slope: ret[0], intercept: ret[1]};
- }
- })(jQuery);
|