add HTML5 doctype to all tests
[dygraphs.git] / tests / linear-regression.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Linear Regression</title>
5 <!--[if IE]>
6 <script type="text/javascript" src="../excanvas.js"></script>
7 <![endif]-->
8 <script type="text/javascript" src="../strftime/strftime-min.js"></script>
9 <script type="text/javascript" src="../rgbcolor/rgbcolor.js"></script>
10 <script type="text/javascript" src="../dygraph-canvas.js"></script>
11 <script type="text/javascript" src="../dygraph.js"></script>
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 drawLines(ctx, area, layout) {
88 if (typeof(g) == 'undefined') return; // won't be set on the initial draw.
89
90 var range = g.xAxisRange();
91 for (var i = 0; i < coeffs.length; i++) {
92 if (!coeffs[i]) continue;
93 var a = coeffs[i][1];
94 var b = coeffs[i][0];
95
96 var x1 = range[0];
97 var y1 = a * x1 + b;
98 var x2 = range[1];
99 var y2 = a * x2 + b;
100
101 var p1 = g.toDomCoords(x1, y1);
102 var p2 = g.toDomCoords(x2, y2);
103
104 var c = new RGBColor(g.getColors()[i - 1]);
105 c.r = Math.floor(255 - 0.5 * (255 - c.r));
106 c.g = Math.floor(255 - 0.5 * (255 - c.g));
107 c.b = Math.floor(255 - 0.5 * (255 - c.b));
108 var color = c.toHex();
109 ctx.save();
110 ctx.strokeStyle = color;
111 ctx.lineWidth = 1.0;
112 ctx.beginPath();
113 ctx.moveTo(p1[0], p1[1]);
114 ctx.lineTo(p2[0], p2[1]);
115 ctx.closePath();
116 ctx.stroke();
117 ctx.restore();
118 }
119 }
120
121 </script>
122
123 <div style="position: absolute; left: 500px; top: 150px;">
124 <input type=button style="color: green;" value="Regression (Y1)" onClick="regression(1)" />
125 <br/><br/>
126 <input type=button style="color: blue;" value="Regression (Y2)" onClick="regression(2)" />
127 <br/><br/>
128 <input type=button value="Clear Lines" onClick="clearLines()" />
129 </div>
130 </body>
131 </html>