我正在使用Angular2 + d3js绘制图表。当鼠标移动到图表上时,会出现一个工具提示。目前,我直接将“div”附加到“body”上,并在d3.select("svg").on("mouseover", function(d){...})
中动态生成innerHTML。
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 10);
d3.select("svg").on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div.html("Tooltip Content Here")
.style("left", (d3.event.pageX) + 10 + "px")
.style("top", (d3.event.pageY - 28) + "px");
});
因为我在使用Angular2,所以我认为提示应该移动到另一个组件/指令中以分离逻辑。一个组件可能看起来像:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-tooltip',
templateUrl: './tooltip.component.html',
styleUrls: ['./tooltip.component.css']
})
export class TooltipComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
但是如何将创建的组件附加到d3.select("svg")的mouseover函数中呢?或者如何直接在另一个组件中使用'app-tooltip'选择器? 谢谢!
<app-component1></app-component1>
在app.component.html中? - Ngu Developer