Browse Source

Added trend line

Efren Yevale Varela 4 years ago
parent
commit
3a3e72d28b
1 changed files with 20 additions and 0 deletions
  1. 20 0
      app.js

+ 20 - 0
app.js

@@ -54,6 +54,26 @@ const graph = new Vue({
       svg.append("g")
         .call(d3.axisLeft(y));
 
+      regression = ss.linearRegression(sorted.map((d) => { return [+d.date, d.value] }));
+      line       = ss.linearRegressionLine(regression);
+      trendline  = x.domain().map((x) => {
+        return {
+          date:  x,
+          value: line(+x)
+        }
+      });
+
+      svg.append("path")
+        .datum(trendline)
+        .attr("fill", "none")
+        .attr("stroke", "red")
+        .attr("stroke-width", 1)
+        .attr("d", d3.line().x((d) => {
+          return x(d.date);
+        }).y((d) => {
+          return y(d.value);
+        }));
+
       svg.append("path")
         .datum(sorted)
         .attr("fill", "none")