revert change to demo.html from 1.1.1
[dygraphs.git] / tests / linear-regression.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <link rel="stylesheet" href="../dist/dygraph.css">
5 <title>Linear Regression</title>
6 <script type="text/javascript" src="../dist/dygraph.js"></script>
7
8 <style type="text/css">
9 body { max-width: 640 px; }
10 </style>
11 </head>
12 <body>
13 <h2>Linear Regression Demo</h2>
14 <p>Click the buttons to generate linear regressions over either data
15 series. If you zoom in and click the regression button, the regression will
16 only be run over visible points. Zoom back out to see what the local
17 regression looks like over the full data.</p>
18
19 <div id="demodiv" style="width: 480px; height: 320px;"></div>
20
21 <script type="text/javascript">
22 var data = [];
23 for (var i = 0; i < 120; i++) {
24 data.push([i,
25 i / 5.0 + 10.0 * Math.sin(i / 3.0),
26 30.0 - i / 5.0 - 10.0 * Math.sin(i / 3.0 + 1.0)]);
27 }
28
29 g = new Dygraph(
30 document.getElementById("demodiv"),
31 data,
32 {
33 labels: ['X', 'Y1', 'Y2'],
34 underlayCallback: drawLines,
35 drawPoints: true,
36 strokeWidth: 0.0
37 }
38 );
39
40 // coefficients of regression for each series.
41 // if coeffs = [ null, [1, 2], null ] then we draw a regression for series 1
42 // only. The regression line is y = 1 + 2 * x.
43 var coeffs = [ null, null, null ];
44 function regression(series) {
45 // Only run the regression over visible points.
46 var range = g.xAxisRange();
47
48 var sum_xy = 0.0, sum_x = 0.0, sum_y = 0.0, sum_x2 = 0.0, num = 0;
49 for (var i = 0; i < g.numRows(); i++) {
50 var x = g.getValue(i, 0);
51 if (x < range[0] || x > range[1]) continue;
52
53 var y = g.getValue(i, series);
54 if (y == null) continue;
55 if (y.length == 2) {
56 // using fractions
57 y = y[0] / y[1];
58 }
59
60 num++;
61 sum_x += x;
62 sum_y += y;
63 sum_xy += x * y;
64 sum_x2 += x * x;
65 }
66
67 var a = (sum_xy - sum_x * sum_y / num) / (sum_x2 - sum_x * sum_x / num);
68 var b = (sum_y - a * sum_x) / num;
69
70 coeffs[series] = [b, a];
71 if (typeof(console) != 'undefined') {
72 console.log("coeffs(" + series + "): [" + b + ", " + a + "]");
73 }
74
75 g.updateOptions({}); // forces a redraw.
76 }
77
78 function clearLines() {
79 for (var i = 0; i < coeffs.length; i++) coeffs[i] = null;
80 g.updateOptions({});
81 }
82
83 function toHex(rgb) {
84 return 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')';
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 = Dygraph.toRGB_(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 = toHex(c);
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>