我正在尝试在使用d3.js v3创建的散点图上实现注释功能。当我单击每个数据点时,将出现文本框,我在其中输入文本。添加文本后,它将显示为该特定数据点的工具提示。我的实现方式如下:
eventGroup.select("circle")
.attr("class", "dot")
.attr("r", 4)
.attr("cx", 10)
.attr("cy", 10)
.attr("fill", function(d) {
return d.evtColor ? d.evtColor : "#229ae5";
})
.attr("stroke", function(d) {
return d.evtColor ? d.evtColor : "#229ae5";
})
.attr("stroke-width", 2)
.on("contextmenu", function() {
var position = d3.mouse(this.parentNode);
d3.select("#context-menu")
.style("position", "absolute")
.style("left", (d3.event.pageX - 220) + "px")
.style("top", (d3.event.pageY - 70) + "px")
.style("display", "inline-block")
.on("click", function() {
d3.select("#context-menu").style("display", "none");
d3.select("#annotateBox")
.style("position", "absolute")
.style("left", (d3.event.pageX - 220) + "px")
.style("top", (d3.event.pageY - 70) + "px")
.style("display", "inline-block");
});
});
在HTML文件中,我定义了一个名为
addAnnotation
的按钮和一个textarea
,我将在其中添加文本。<ul id="context-menu" class="menu" style="display:none;">
<li class="addAnnotation"><a href="">Add Annotation</a></li>
</ul>
<div id="annotateBox" style="display:none;">
<div class="annotateInput" style="display:table-caption">
<textarea rows="3" cols="50" maxlength="100" style="color:black" ng-model="vm.annotateText" autofocus></textarea>
<button type="button" class="btn btn-primary pull-right" ng-click="vm.removeButton()">Done</button>
</div>
</div>
我想做的下一步是,当单击数据点时,在弹出的文本区域中添加文本,我希望该文本绑定到特定的数据点而不是所有数据点。是否有一种方法可以将文本区域分配给调用事件而不是普遍适用于所有数据点。目前,我添加的任何文本作为一个数据点的一部分都会在其它数据点中显示。