1
我将circles
添加到使用D3.js的图中,圆圈需要添加超时(用于动画目的),所以我使用的是setTimeout
。D3 - 如何选择最近添加的元素
这些圈子还需要在其上注册mouseover
和click
事件处理程序,但我无法选择最近添加的元素来注册on
听众。
下面是代码:
(salesDataToPlot.graphObj.eventData).forEach(
function(d,i){
setTimeout(
function(){
console.log(d.collectTime);
svg.append("circle")
.transition()
.duration(1000)
.attr("cx", function() {
//console.log(new Date(d.collectTime.substr(0,10)));
return xScale(new Date(d.collectTime.substr(0,10)));
})
.attr("cy", function() {
//console.log(d.value);
return yScale((d.value - min)/(max-min));
})
.attr("r", function() {
return 5;
})
.attr("clip-path","url(#clip)")
.attr("class","circle")
//.attr("ng-class","{ 'graphClass':!chartData.graphObj.showEventMarkers }")
.style("fill", eventStrokeColor)
;
}, 1000+ i*100);
});
这个代码将与各界超时。
现在,我不知道在哪里以及如何添加代码on
听众喜欢
.on("mouseover", function(d)
{
tooltip.style("visibility", "visible");
tooltip.style("background-color",eventStrokeColor);
tooltip.html("<div style='color:black'>" + d.eventType + "</div>" + d.eventSentence);
})
看来你是附加在svg元素上的听众,而不是在圈子 – gaurav5430
没有gaurav。西里尔已经将听众与圈子联系在一起。请注意,'append'函数返回附加的元素本身。 – Gilsha
好的....看着代码...它好像听众被附加到svg元素......但实际上它运作良好。 – gaurav5430