add HTML5 doctype to all tests
[dygraphs.git] / tests / linear-regression-addseries.html
CommitLineData
54425b14 1<!DOCTYPE html>
34bbe78b
DV
2<html>
3 <head>
4 <title>Linear Regression</title>
5 <!--[if IE]>
a2b2c3a1 6 <script type="text/javascript" src="../excanvas.js"></script>
34bbe78b
DV
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 var labels = ['X', 'Y1', 'Y2'];
33 var orig_colors = [];
34
35 g = new Dygraph(
36 document.getElementById("demodiv"),
37 data,
38 {
39 labels: labels,
40 drawPoints: true,
de0445fc 41 strokeWidth: 0.0,
34bbe78b
DV
42 drawCallback: function(g, is_initial) {
43 if (!is_initial) return;
44 var c = g.getColors();
45 for (var i = 0; i < c.length; i++) orig_colors.push(c[i]);
46 }
47 }
48 );
49
50 // coefficients of regression for each series.
51 // if coeffs = [ null, [1, 2], null ] then we draw a regression for series 1
52 // only. The regression line is y = 1 + 2 * x.
53 var coeffs = [ null, null, null ];
54 function regression(series) {
55 // Only run the regression over visible points.
56 var range = g.xAxisRange();
57
58 var sum_xy = 0.0, sum_x = 0.0, sum_y = 0.0, sum_x2 = 0.0, num = 0;
59 for (var i = 0; i < data.length; i++) {
60 var x = data[i][0];
61 if (x < range[0] || x > range[1]) continue;
62
63 var y = data[i][series];
64 if (y == null) continue;
d1dfab80
DV
65 if (y.length == 2) {
66 // using fractions
67 y = y[0] / y[1];
68 }
34bbe78b
DV
69
70 num++;
71 sum_x += x;
72 sum_y += y;
73 sum_xy += x * y;
74 sum_x2 += x * x;
75 }
76
77 var a = (sum_xy - sum_x * sum_y / num) / (sum_x2 - sum_x * sum_x / num);
78 var b = (sum_y - a * sum_x) / num;
79
80 coeffs[series] = [b, a];
4df1af70 81 if (typeof(console) != 'undefined') {
34bbe78b
DV
82 console.log("coeffs(" + series + "): [" + b + ", " + a + "]");
83 }
84
85 updateChart();
86 }
87
88 function updateChart() {
89 // Generate a new data set with the regression lines.
90 var new_labels = [];
91 var new_colors = [];
de0445fc 92 var new_opts = {};
34bbe78b
DV
93 for (var i = 0; i < labels.length; i++) {
94 new_labels.push(labels[i]);
95 if (i) new_colors.push(orig_colors[i - 1]);
96 if (coeffs[i]) {
97 // Darken the series by 50% to generate its regression.
227b93cc
DV
98 var label = labels[i] + " Regression";
99 new_labels.push(label);
34bbe78b
DV
100 var c = new RGBColor(orig_colors[i - 1]);
101 c.r = Math.floor(255 - 0.5 * (255 - c.r));
102 c.g = Math.floor(255 - 0.5 * (255 - c.g));
103 c.b = Math.floor(255 - 0.5 * (255 - c.b));
104 new_colors.push(c.toHex());
227b93cc 105 new_opts[label] = {
de0445fc
DV
106 drawPoints: false,
107 strokeWidth: 1.0
108 };
34bbe78b
DV
109 }
110 }
111
112 var new_data = [];
113 for (var i = 0; i < data.length; i++) {
114 new_data.push([]);
115 for (var j = 0; j < data[i].length; j++) {
116 new_data[i].push(data[i][j]);
117 if (coeffs[j]) {
118 new_data[i].push(coeffs[j][0] + coeffs[j][1] * data[i][0]);
119 }
120 }
121 }
122
de0445fc
DV
123 new_opts.file = new_data;
124 new_opts.labels = new_labels;
125 new_opts.colors = new_colors;
126 g.updateOptions(new_opts);
34bbe78b
DV
127 }
128
129 function clearLines() {
130 for (var i = 0; i < coeffs.length; i++) coeffs[i] = null;
131 updateChart();
132 }
34bbe78b
DV
133
134 </script>
135
136 <div style="position: absolute; left: 500px; top: 150px;">
137 <input type=button style="color: green;" value="Regression (Y1)" onClick="regression(1)" />
138 <br/><br/>
139 <input type=button style="color: blue;" value="Regression (Y2)" onClick="regression(2)" />
140 <br/><br/>
141 <input type=button value="Clear Lines" onClick="clearLines()" />
142 </div>
143</body>
144</html>