Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.Statistics/3.3/Scripts/jqPlot/plugins/jqplot.trendline.js @ 9617

Last change on this file since 9617 was 9617, checked in by pfleck, 11 years ago

#2063:
Started integrating Hive statistics into statistics web project.
Added jqPlot library for charting.

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