Clear graph when numAxes goes from 1 -> 2 in updateOptions
[dygraphs.git] / tests / linear-regression-addseries.html
CommitLineData
54425b14 1<!DOCTYPE html>
34bbe78b
DV
2<html>
3 <head>
fd6b8dad 4 <link rel="stylesheet" href="../dist/dygraph.css">
34bbe78b 5 <title>Linear Regression</title>
fbd6834a 6 <script type="text/javascript" src="../dist/dygraph.js"></script>
7e5ddc94 7
34bbe78b
DV
8 <style type="text/css">
9 body { max-width: 640 px; }
10 </style>
11 </head>
12 <body>
13 <h2>Linear Regression Demo</h2>
14 <p>Click the buttons to generate linear regressions over either data
15 series. If you zoom in and click the regression button, the regression will
16 only be run over visible points. Zoom back out to see what the local
17 regression looks like over the full data.</p>
18
19 <div id="demodiv" style="width: 480px; height: 320px;"></div>
20
21 <script type="text/javascript">
22 var data = [];
23 for (var i = 0; i < 120; i++) {
24 data.push([i,
25 i / 5.0 + 10.0 * Math.sin(i / 3.0),
26 30.0 - i / 5.0 - 10.0 * Math.sin(i / 3.0 + 1.0)]);
27 }
28 var labels = ['X', 'Y1', 'Y2'];
29 var orig_colors = [];
30
31 g = new Dygraph(
32 document.getElementById("demodiv"),
33 data,
34 {
35 labels: labels,
36 drawPoints: true,
de0445fc 37 strokeWidth: 0.0,
34bbe78b
DV
38 drawCallback: function(g, is_initial) {
39 if (!is_initial) return;
40 var c = g.getColors();
41 for (var i = 0; i < c.length; i++) orig_colors.push(c[i]);
42 }
43 }
44 );
45
46 // coefficients of regression for each series.
47 // if coeffs = [ null, [1, 2], null ] then we draw a regression for series 1
48 // only. The regression line is y = 1 + 2 * x.
49 var coeffs = [ null, null, null ];
50 function regression(series) {
51 // Only run the regression over visible points.
52 var range = g.xAxisRange();
53
54 var sum_xy = 0.0, sum_x = 0.0, sum_y = 0.0, sum_x2 = 0.0, num = 0;
55 for (var i = 0; i < data.length; i++) {
56 var x = data[i][0];
57 if (x < range[0] || x > range[1]) continue;
58
59 var y = data[i][series];
60 if (y == null) continue;
d1dfab80
DV
61 if (y.length == 2) {
62 // using fractions
63 y = y[0] / y[1];
64 }
34bbe78b
DV
65
66 num++;
67 sum_x += x;
68 sum_y += y;
69 sum_xy += x * y;
70 sum_x2 += x * x;
71 }
72
73 var a = (sum_xy - sum_x * sum_y / num) / (sum_x2 - sum_x * sum_x / num);
74 var b = (sum_y - a * sum_x) / num;
75
76 coeffs[series] = [b, a];
4df1af70 77 if (typeof(console) != 'undefined') {
34bbe78b
DV
78 console.log("coeffs(" + series + "): [" + b + ", " + a + "]");
79 }
80
81 updateChart();
82 }
83
0a171c09
DV
84 function toHex(rgb) {
85 return 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')';
86 }
87
34bbe78b
DV
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);
0a171c09 100 var c = Dygraph.toRGB_(orig_colors[i - 1]);
34bbe78b
DV
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));
0a171c09 104 new_colors.push(toHex(c));
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>