score:0

You need to append a line for each data point from the coordinates of that point to where it meets the line. The code would look something like the following (with some code adapted from this question):

d3.selectAll("line")
  .data(data)
  .enter()
  .append("line")
  .attr("x1", function(d) { return x(d[x_val]); })
  .attr("x2", function(d) { return x(d[x_val]); })
  .attr("y1", function(d) { return y(d[y_val]); })
  .attr("y2", function(d) {
    var line = d3.select("#regression_line").node(),
        x = x(d[x_val]);
    function getXY(len) {
      return line.getPointAtLength(len);
    }
    var curlen = 0;
    while (getXY(curlen).x < x) { curlen += 0.01; }
    return getXY(curlen).y;
  });

Related Query

More Query from same tag