Fixed attribute name typo in legend follow (#792)
[dygraphs.git] / tests / linear-regression.html
CommitLineData
54425b14 1<!DOCTYPE html>
05a1d116
DV
2<html>
3 <head>
93a5bb4c 4 <link rel="stylesheet" href="../css/dygraph.css">
05a1d116 5 <title>Linear Regression</title>
7e5ddc94
DV
6 <!--
7 For production (minified) code, use:
8 <script type="text/javascript" src="dygraph-combined.js"></script>
9 -->
fbd6834a 10 <script type="text/javascript" src="../dist/dygraph.js"></script>
7e5ddc94 11
05a1d116
DV
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">
176c94a4 26 var data = [];
05a1d116 27 for (var i = 0; i < 120; i++) {
176c94a4
DV
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)]);
05a1d116
DV
31 }
32
33 g = new Dygraph(
34 document.getElementById("demodiv"),
35 data,
36 {
176c94a4 37 labels: ['X', 'Y1', 'Y2'],
05a1d116 38 underlayCallback: drawLines,
de0445fc 39 drawPoints: true,
176c94a4 40 strokeWidth: 0.0
05a1d116
DV
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;
c5a37449
DV
59 if (y.length == 2) {
60 // using fractions
61 y = y[0] / y[1];
62 }
05a1d116
DV
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];
4df1af70 75 if (typeof(console) != 'undefined') {
2a07b0ad
DV
76 console.log("coeffs(" + series + "): [" + b + ", " + a + "]");
77 }
05a1d116
DV
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
0a171c09
DV
87 function toHex(rgb) {
88 return 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')';
89 }
90
05a1d116
DV
91 function drawLines(ctx, area, layout) {
92 if (typeof(g) == 'undefined') return; // won't be set on the initial draw.
93
94 var range = g.xAxisRange();
95 for (var i = 0; i < coeffs.length; i++) {
96 if (!coeffs[i]) continue;
97 var a = coeffs[i][1];
98 var b = coeffs[i][0];
99
100 var x1 = range[0];
101 var y1 = a * x1 + b;
102 var x2 = range[1];
103 var y2 = a * x2 + b;
104
105 var p1 = g.toDomCoords(x1, y1);
106 var p2 = g.toDomCoords(x2, y2);
107
0a171c09 108 var c = Dygraph.toRGB_(g.getColors()[i - 1]);
34bbe78b
DV
109 c.r = Math.floor(255 - 0.5 * (255 - c.r));
110 c.g = Math.floor(255 - 0.5 * (255 - c.g));
111 c.b = Math.floor(255 - 0.5 * (255 - c.b));
0a171c09 112 var color = toHex(c);
05a1d116
DV
113 ctx.save();
114 ctx.strokeStyle = color;
115 ctx.lineWidth = 1.0;
116 ctx.beginPath();
117 ctx.moveTo(p1[0], p1[1]);
118 ctx.lineTo(p2[0], p2[1]);
119 ctx.closePath();
120 ctx.stroke();
121 ctx.restore();
122 }
123 }
124
125 </script>
126
127 <div style="position: absolute; left: 500px; top: 150px;">
128 <input type=button style="color: green;" value="Regression (Y1)" onClick="regression(1)" />
129 <br/><br/>
130 <input type=button style="color: blue;" value="Regression (Y2)" onClick="regression(2)" />
131 <br/><br/>
132 <input type=button value="Clear Lines" onClick="clearLines()" />
133 </div>
134</body>
135</html>