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