文字也少Z指數:它,因爲你是第一個創建的提示,然後這樣的路徑的路徑將在工具提示前面。在svg中沒有z-index的概念。因此,我們需要先製作路徑,然後再製作工具提示。
要獲得鼠標懸停工具提示請執行下列操作(在下面的代碼片段評論):
svg.append("path")
.data([data]) //this is equivalent to datum(data)
.attr("class", "area")
.attr("d", area)
.on("mouseover", function() {
tooltip.style("display", null);
})
.on("mouseout", function() {
tooltip.style("display", "none");
})
.on("mousemove", function(d) {
var xPosition = d3.mouse(this)[0] - 15;//x position of tooltip
var yPosition = d3.mouse(this)[1] - 25;//y position of tooltip
tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");//placing the tooltip
var x0 = x.invert(d3.mouse(this)[0]);//this will give the x for the mouse position on x
var y0 = y.invert(d3.mouse(this)[1]);//this will give the y for the mouse position on y
tooltip.select("text").text(d3.time.format('%Y-%m-%d')(x0)+ " " +Math.round(y0));//show the text after formatting the date
});;
工作代碼here
希望這有助於!
是的,它是在一個plunker但不作爲角度指令作爲查詢在這裏http://stackoverflow.com/questions/34470062/d3js-angular-directive-for-area-chart-using-d3-mouse-event-for -tooltip –