54c299a692d420584c1dcb8da8ce051ba4b352ef
[dygraphs.git] / tests / linear-regression-fractions.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 = "X,Y1,Y2\n";
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 data += i + "," + i + "/120," + (i*(120-i)) + "/" + (60*60) + "\n";
32 }
33
34 g = new Dygraph(
35 document.getElementById("demodiv"),
36 data,
37 {
38 underlayCallback: drawLines,
39 drawPoints: true,
40 strokeWidth: 0.0,
41 fractions: true,
42 errorBars: true,
43 pointSize: 1
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;
62 if (y.length == 2) {
63 // using fractions
64 y = y[0] / y[1];
65 }
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];
78 if (typeof(console) != 'undefined') {
79 console.log("coeffs(" + series + "): [" + b + ", " + a + "]");
80 }
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 toHex(rgb) {
91 return 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')';
92 }
93
94 function drawLines(ctx, area, layout) {
95 if (typeof(g) == 'undefined') return; // won't be set on the initial draw.
96
97 var range = g.xAxisRange();
98 for (var i = 0; i < coeffs.length; i++) {
99 if (!coeffs[i]) continue;
100 var a = coeffs[i][1];
101 var b = coeffs[i][0];
102
103 var x1 = range[0];
104 var y1 = a * x1 + b;
105 var x2 = range[1];
106 var y2 = a * x2 + b;
107
108 // Scale up to percentages, rather than fractions.
109 y1 *= 100;
110 y2 *= 100;
111
112 var p1 = g.toDomCoords(x1, y1);
113 var p2 = g.toDomCoords(x2, y2);
114
115 var c = Dygraph.toRGB_(g.getColors()[i - 1]);
116 c.r = Math.floor(255 - 0.5 * (255 - c.r));
117 c.g = Math.floor(255 - 0.5 * (255 - c.g));
118 c.b = Math.floor(255 - 0.5 * (255 - c.b));
119 var color = toHex(c);
120 ctx.save();
121 ctx.strokeStyle = color;
122 ctx.lineWidth = 2.0;
123 ctx.beginPath();
124 ctx.moveTo(p1[0], p1[1]);
125 ctx.lineTo(p2[0], p2[1]);
126 ctx.closePath();
127 ctx.stroke();
128 ctx.restore();
129 }
130 }
131
132 </script>
133
134 <div style="position: absolute; left: 500px; top: 150px;">
135 <input type=button style="color: green;" value="Regression (Y1)" onClick="regression(1)" />
136 <br/><br/>
137 <input type=button style="color: blue;" value="Regression (Y2)" onClick="regression(2)" />
138 <br/><br/>
139 <input type=button value="Clear Lines" onClick="clearLines()" />
140 </div>
141 </body>
142 </html>