Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Web/Content/jqplot/src/plugins/jqplot.trendline.js @ 14777

Last change on this file since 14777 was 9062, checked in by fschoepp, 12 years ago

#1888:
Backend changes:

  • Simplified job state detection (only one hive call will be made to detect all states now, instead of one additional call per job)
  • Reorganized classes (moved model classes into Model folder)

Website changes:

  • Website now heavily uses JavaScript to achieve better user experience
  • JavaScript degrades gracefully, except for plots
  • Tables: Added jquery-datatable-plugin to extend tables (pagination + search functionality)
  • OaaS-Website now uses the design of the HL websites (found in WebApplication branch)
  • Added jqplot to render zoomable line plots for HL-Datatables
  • Styling.js: Plots will be generated by using an ajax call; additional jquery-styling occurs within this file.
  • Added jquery-ui-1.9.2 which is capable of handling/rendering tabs, accordions and resizers.
File size: 7.4 KB
Line 
1/**
2 * jqPlot
3 * Pure JavaScript plotting plugin using jQuery
4 *
5 * Version: @VERSION
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 lt = series.trendline.label.toString();
96        var ret = null;
97        if (this.renderer.constructor != $.jqplot.PieRenderer && series.trendline.show && lt) {
98            ret = {label:lt, color:series.trendline.color};
99        }
100        return ret;
101    }
102
103    // called within scope of a series
104    function parseTrendLineOptions (target, data, seriesDefaults, options, plot) {
105        if (this.renderer.constructor == $.jqplot.LineRenderer) {
106            this.trendline = new $.jqplot.Trendline();
107            options = options || {};
108            $.extend(true, this.trendline, {color:this.color}, seriesDefaults.trendline, options.trendline);
109            this.trendline.renderer.init.call(this.trendline, null);
110        }
111    }
112   
113    // called within scope of series object
114    function drawTrendline(sctx, options) {
115        // if we have options, merge trendline options in with precedence
116        options = $.extend(true, {}, this.trendline, options);
117
118        if (options.show && this.renderer.constructor != $.jqplot.PieRenderer) {
119            var fit;
120            // this.renderer.setGridData.call(this);
121            var data = options.data || this.data;
122            fit = fitData(data, this.trendline.type);
123            var gridData = options.gridData || this.renderer.makeGridData.call(this, fit.data);
124            this.trendline.renderer.draw.call(this.trendline, sctx, gridData, {showLine:true, shadow:this.trendline.shadow});
125        }
126    }
127   
128    function regression(x, y, typ)  {
129        var type = (typ == null) ? 'linear' : typ;
130        var N = x.length;
131        var slope;
132        var intercept; 
133        var SX = 0;
134        var SY = 0;
135        var SXX = 0;
136        var SXY = 0;
137        var SYY = 0;
138        var Y = [];
139        var X = [];
140   
141        if (type == 'linear') {
142            X = x;
143            Y = y;
144        }
145        else if (type == 'exp' || type == 'exponential') {
146            for ( var i=0; i<y.length; i++) {
147                // ignore points <= 0, log undefined.
148                if (y[i] <= 0) {
149                    N--;
150                }
151                else {
152                    X.push(x[i]);
153                    Y.push(Math.log(y[i]));
154                }
155            }
156        }
157
158        for ( var i = 0; i < N; i++) {
159            SX = SX + X[i];
160            SY = SY + Y[i];
161            SXY = SXY + X[i]* Y[i];
162            SXX = SXX + X[i]* X[i];
163            SYY = SYY + Y[i]* Y[i];
164        }
165
166        slope = (N*SXY - SX*SY)/(N*SXX - SX*SX);
167        intercept = (SY - slope*SX)/N;
168
169        return [slope, intercept];
170    }
171
172    function linearRegression(X,Y) {
173        var ret;
174        ret = regression(X,Y,'linear');
175        return [ret[0],ret[1]];
176    }
177
178    function expRegression(X,Y) {
179        var ret;
180        var x = X;
181        var y = Y;
182        ret = regression(x, y,'exp');
183        var base = Math.exp(ret[0]);
184        var coeff = Math.exp(ret[1]);
185        return [base, coeff];
186    }
187
188    function fitData(data, typ) {
189        var type = (typ == null) ?  'linear' : typ;
190        var ret;
191        var res;
192        var x = [];
193        var y = [];
194        var ypred = [];
195       
196        for (i=0; i<data.length; i++){
197            if (data[i] != null && data[i][0] != null && data[i][1] != null) {
198                x.push(data[i][0]);
199                y.push(data[i][1]);
200            }
201        }
202       
203        if (type == 'linear') {
204            ret = linearRegression(x,y);
205            for ( var i=0; i<x.length; i++){
206                res = ret[0]*x[i] + ret[1];
207                ypred.push([x[i], res]);
208            }
209        }
210        else if (type == 'exp' || type == 'exponential') {
211            ret = expRegression(x,y);
212            for ( var i=0; i<x.length; i++){
213                res = ret[1]*Math.pow(ret[0],x[i]);
214                ypred.push([x[i], res]);
215            }
216        }
217        return {data: ypred, slope: ret[0], intercept: ret[1]};
218    }
219
220})(jQuery);
Note: See TracBrowser for help on using the repository browser.