// Add a tooltip div. Here I define the general feature of the tooltip: stuff that do not depend on the data point.
// Its opacity is set to 0: we don't see it by default.
vartooltip=d3.select("#my_dataviz")
.append("div")
.style("opacity",0)
.attr("class","tooltip")
.style("background-color","white")
.style("border","solid")
.style("border-width","1px")
.style("border-radius","5px")
.style("padding","10px")
// A function that change this tooltip when the user hover a point.
// Its opacity is set to 1: we can now see it. Plus it set the text and position of tooltip depending on the datapoint (d)
varmouseover=function(d){
tooltip
.style("opacity",1)
}
varmousemove=function(d){
tooltip
.html("The exact value of<br>the Ground Living area is: "+d.GrLivArea)
.style("left",(d3.mouse(this)[0]+90)+"px")// It is important to put the +90: other wise the tooltip is exactly where the point is an it creates a weird effect
.style("top",(d3.mouse(this)[1])+"px")
}
// A function that change this tooltip when the leaves a point: just need to set opacity to 0 again
varmouseleave=function(d){
tooltip
.transition()
.duration(200)
.style("opacity",0)
}
// Add dots
svg.append('g')
.selectAll("dot")
.data(data.filter(function(d,i){returni<50}))// the .filter part is just to keep a few dots on the chart, not all of them