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