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