[Feature Request] Provide option to set color and width for annotation line (#703)
[dygraphs.git] / tests / linear-regression-addseries.html
CommitLineData
54425b14 1<!DOCTYPE html>
34bbe78b
DV
2<html>
3 <head>
93a5bb4c 4 <link rel="stylesheet" href="../css/dygraph.css">
34bbe78b 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
34bbe78b
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">
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
0a171c09
DV
88 function toHex(rgb) {
89 return 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')';
90 }
91
34bbe78b
DV
92 function updateChart() {
93 // Generate a new data set with the regression lines.
94 var new_labels = [];
95 var new_colors = [];
de0445fc 96 var new_opts = {};
34bbe78b
DV
97 for (var i = 0; i < labels.length; i++) {
98 new_labels.push(labels[i]);
99 if (i) new_colors.push(orig_colors[i - 1]);
100 if (coeffs[i]) {
101 // Darken the series by 50% to generate its regression.
227b93cc
DV
102 var label = labels[i] + " Regression";
103 new_labels.push(label);
0a171c09 104 var c = Dygraph.toRGB_(orig_colors[i - 1]);
34bbe78b
DV
105 c.r = Math.floor(255 - 0.5 * (255 - c.r));
106 c.g = Math.floor(255 - 0.5 * (255 - c.g));
107 c.b = Math.floor(255 - 0.5 * (255 - c.b));
0a171c09 108 new_colors.push(toHex(c));
227b93cc 109 new_opts[label] = {
de0445fc
DV
110 drawPoints: false,
111 strokeWidth: 1.0
112 };
34bbe78b
DV
113 }
114 }
115
116 var new_data = [];
117 for (var i = 0; i < data.length; i++) {
118 new_data.push([]);
119 for (var j = 0; j < data[i].length; j++) {
120 new_data[i].push(data[i][j]);
121 if (coeffs[j]) {
122 new_data[i].push(coeffs[j][0] + coeffs[j][1] * data[i][0]);
123 }
124 }
125 }
126
de0445fc
DV
127 new_opts.file = new_data;
128 new_opts.labels = new_labels;
129 new_opts.colors = new_colors;
130 g.updateOptions(new_opts);
34bbe78b
DV
131 }
132
133 function clearLines() {
134 for (var i = 0; i < coeffs.length; i++) coeffs[i] = null;
135 updateChart();
136 }
34bbe78b
DV
137
138 </script>
139
140 <div style="position: absolute; left: 500px; top: 150px;">
141 <input type=button style="color: green;" value="Regression (Y1)" onClick="regression(1)" />
142 <br/><br/>
143 <input type=button style="color: blue;" value="Regression (Y2)" onClick="regression(2)" />
144 <br/><br/>
145 <input type=button value="Clear Lines" onClick="clearLines()" />
146 </div>
147</body>
148</html>