Fix flaky testCustomBarsWithNegativeValuesInLogScale. DygraphLayout._calcYNormal...
[dygraphs.git] / tests / linear-regression.html
CommitLineData
54425b14 1<!DOCTYPE html>
05a1d116
DV
2<html>
3 <head>
10494b48 4 <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7; IE=EmulateIE9">
05a1d116
DV
5 <title>Linear Regression</title>
6 <!--[if IE]>
a2b2c3a1 7 <script type="text/javascript" src="../excanvas.js"></script>
05a1d116 8 <![endif]-->
7e5ddc94
DV
9 <!--
10 For production (minified) code, use:
11 <script type="text/javascript" src="dygraph-combined.js"></script>
12 -->
13 <script type="text/javascript" src="../dygraph-dev.js"></script>
14
05a1d116
DV
15 <style type="text/css">
16 body { max-width: 640 px; }
17 </style>
18 </head>
19 <body>
20 <h2>Linear Regression Demo</h2>
21 <p>Click the buttons to generate linear regressions over either data
22 series. If you zoom in and click the regression button, the regression will
23 only be run over visible points. Zoom back out to see what the local
24 regression looks like over the full data.</p>
25
26 <div id="demodiv" style="width: 480px; height: 320px;"></div>
27
28 <script type="text/javascript">
176c94a4 29 var data = [];
05a1d116 30 for (var i = 0; i < 120; i++) {
176c94a4
DV
31 data.push([i,
32 i / 5.0 + 10.0 * Math.sin(i / 3.0),
33 30.0 - i / 5.0 - 10.0 * Math.sin(i / 3.0 + 1.0)]);
05a1d116
DV
34 }
35
36 g = new Dygraph(
37 document.getElementById("demodiv"),
38 data,
39 {
176c94a4 40 labels: ['X', 'Y1', 'Y2'],
05a1d116 41 underlayCallback: drawLines,
de0445fc 42 drawPoints: true,
176c94a4 43 strokeWidth: 0.0
05a1d116
DV
44 }
45 );
46
47 // coefficients of regression for each series.
48 // if coeffs = [ null, [1, 2], null ] then we draw a regression for series 1
49 // only. The regression line is y = 1 + 2 * x.
50 var coeffs = [ null, null, null ];
51 function regression(series) {
52 // Only run the regression over visible points.
53 var range = g.xAxisRange();
54
55 var sum_xy = 0.0, sum_x = 0.0, sum_y = 0.0, sum_x2 = 0.0, num = 0;
56 for (var i = 0; i < g.numRows(); i++) {
57 var x = g.getValue(i, 0);
58 if (x < range[0] || x > range[1]) continue;
59
60 var y = g.getValue(i, series);
61 if (y == null) continue;
c5a37449
DV
62 if (y.length == 2) {
63 // using fractions
64 y = y[0] / y[1];
65 }
05a1d116
DV
66
67 num++;
68 sum_x += x;
69 sum_y += y;
70 sum_xy += x * y;
71 sum_x2 += x * x;
72 }
73
74 var a = (sum_xy - sum_x * sum_y / num) / (sum_x2 - sum_x * sum_x / num);
75 var b = (sum_y - a * sum_x) / num;
76
77 coeffs[series] = [b, a];
4df1af70 78 if (typeof(console) != 'undefined') {
2a07b0ad
DV
79 console.log("coeffs(" + series + "): [" + b + ", " + a + "]");
80 }
05a1d116
DV
81
82 g.updateOptions({}); // forces a redraw.
83 }
84
85 function clearLines() {
86 for (var i = 0; i < coeffs.length; i++) coeffs[i] = null;
87 g.updateOptions({});
88 }
89
90 function drawLines(ctx, area, layout) {
91 if (typeof(g) == 'undefined') return; // won't be set on the initial draw.
92
93 var range = g.xAxisRange();
94 for (var i = 0; i < coeffs.length; i++) {
95 if (!coeffs[i]) continue;
96 var a = coeffs[i][1];
97 var b = coeffs[i][0];
98
99 var x1 = range[0];
100 var y1 = a * x1 + b;
101 var x2 = range[1];
102 var y2 = a * x2 + b;
103
104 var p1 = g.toDomCoords(x1, y1);
105 var p2 = g.toDomCoords(x2, y2);
106
42142795 107 var c = new RGBColorParser(g.getColors()[i - 1]);
34bbe78b
DV
108 c.r = Math.floor(255 - 0.5 * (255 - c.r));
109 c.g = Math.floor(255 - 0.5 * (255 - c.g));
110 c.b = Math.floor(255 - 0.5 * (255 - c.b));
111 var color = c.toHex();
05a1d116
DV
112 ctx.save();
113 ctx.strokeStyle = color;
114 ctx.lineWidth = 1.0;
115 ctx.beginPath();
116 ctx.moveTo(p1[0], p1[1]);
117 ctx.lineTo(p2[0], p2[1]);
118 ctx.closePath();
119 ctx.stroke();
120 ctx.restore();
121 }
122 }
123
124 </script>
125
126 <div style="position: absolute; left: 500px; top: 150px;">
127 <input type=button style="color: green;" value="Regression (Y1)" onClick="regression(1)" />
128 <br/><br/>
129 <input type=button style="color: blue;" value="Regression (Y2)" onClick="regression(2)" />
130 <br/><br/>
131 <input type=button value="Clear Lines" onClick="clearLines()" />
132 </div>
133</body>
134</html>