add back Dygraph.floatFormat
[dygraphs.git] / tests / linear-regression-fractions.html
CommitLineData
54425b14 1<!DOCTYPE html>
d1dfab80
DV
2<html>
3 <head>
10494b48 4 <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7; IE=EmulateIE9">
d1dfab80
DV
5 <title>Linear Regression</title>
6 <!--[if IE]>
7 <script type="text/javascript" src="../excanvas.js"></script>
8 <![endif]-->
9 <script type="text/javascript" src="../strftime/strftime-min.js"></script>
10 <script type="text/javascript" src="../rgbcolor/rgbcolor.js"></script>
11 <script type="text/javascript" src="../dygraph-canvas.js"></script>
12 <script type="text/javascript" src="../dygraph.js"></script>
13 <style type="text/css">
14 body { max-width: 640 px; }
15 </style>
16 </head>
17 <body>
18 <h2>Linear Regression Demo</h2>
19 <p>Click the buttons to generate linear regressions over either data
20 series. If you zoom in and click the regression button, the regression will
21 only be run over visible points. Zoom back out to see what the local
22 regression looks like over the full data.</p>
23
24 <div id="demodiv" style="width: 480px; height: 320px;"></div>
25
26 <script type="text/javascript">
27 var data = "X,Y1,Y2\n";
28 for (var i = 0; i < 120; i++) {
29 // data.push([i,
30 // i / 5.0 + 10.0 * Math.sin(i / 3.0),
31 // 30.0 - i / 5.0 - 10.0 * Math.sin(i / 3.0 + 1.0)]);
32 data += i + "," + i + "/120," + (i*(120-i)) + "/" + (60*60) + "\n";
33 }
34
35 g = new Dygraph(
36 document.getElementById("demodiv"),
37 data,
38 {
39 underlayCallback: drawLines,
40 drawPoints: true,
41 strokeWidth: 0.0,
42 fractions: true,
43 errorBars: true
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 drawLines(ctx, area, layout) {
91 if (typeof(g) == 'undefined') return; // won't be set on the initial draw.
92
93 var range = g.xAxisRange();
94 for (var i = 0; i < coeffs.length; i++) {
95 if (!coeffs[i]) continue;
96 var a = coeffs[i][1];
97 var b = coeffs[i][0];
98
99 var x1 = range[0];
100 var y1 = a * x1 + b;
101 var x2 = range[1];
102 var y2 = a * x2 + b;
103
104 // Scale up to percentages, rather than fractions.
105 y1 *= 100;
106 y2 *= 100;
107
108 var p1 = g.toDomCoords(x1, y1);
109 var p2 = g.toDomCoords(x2, y2);
110
111 var c = new RGBColor(g.getColors()[i - 1]);
112 c.r = Math.floor(255 - 0.5 * (255 - c.r));
113 c.g = Math.floor(255 - 0.5 * (255 - c.g));
114 c.b = Math.floor(255 - 0.5 * (255 - c.b));
115 var color = c.toHex();
116 ctx.save();
117 ctx.strokeStyle = color;
118 ctx.lineWidth = 1.0;
119 ctx.beginPath();
120 ctx.moveTo(p1[0], p1[1]);
121 ctx.lineTo(p2[0], p2[1]);
122 ctx.closePath();
123 ctx.stroke();
124 ctx.restore();
125 }
126 }
127
128 </script>
129
130 <div style="position: absolute; left: 500px; top: 150px;">
131 <input type=button style="color: green;" value="Regression (Y1)" onClick="regression(1)" />
132 <br/><br/>
133 <input type=button style="color: blue;" value="Regression (Y2)" onClick="regression(2)" />
134 <br/><br/>
135 <input type=button value="Clear Lines" onClick="clearLines()" />
136 </div>
137</body>
138</html>