Skip to content

Instantly share code, notes, and snippets.

@martinnormark
Created March 22, 2016 11:53
Show Gist options
  • Save martinnormark/e00236af5402f6d22dec to your computer and use it in GitHub Desktop.
Save martinnormark/e00236af5402f6d22dec to your computer and use it in GitHub Desktop.
Adding a vertical line to a D3 chart, that follows the mouse pointer.
var vertical = d3.select(".chart")
.append("div")
.attr("class", "remove")
.style("position", "absolute")
.style("z-index", "19")
.style("width", "1px")
.style("height", "380px")
.style("top", "10px")
.style("bottom", "30px")
.style("left", "0px")
.style("background", "#fff");
d3.select(".chart")
.on("mousemove", function(){
mousex = d3.mouse(this);
mousex = mousex[0] + 5;
vertical.style("left", mousex + "px" )})
.on("mouseover", function(){
mousex = d3.mouse(this);
mousex = mousex[0] + 5;
vertical.style("left", mousex + "px")});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment