我一直在进行d3.js可视化(使用Promise的v5)。不是最擅长使用javascript的语言,也许是因为我盯着屏幕太久了,但是我无法让else语句触发dblclick事件。
// react to double click .on() event
function doubleclicked(d) {
if (d.fx == null & d.fy == null){
console.log("d.fx and d.fy are null, fixing point");
d.fx = d.x;
d.fy = d.y;
d3.select(this).select("text").transition()
.duration(750)
.attr("x", 22)
.style("stroke", "black")
.style("font", "10px sans-serif")
.style("font-weight", "normal")
.style("weight", "normal")
.style("pointer-events", "none");
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", 16)
.style("stroke-width", "1.5px")
.style("fill","light gray")
.style("stroke","black");
} else{
console.log('whyyyy wont this work');
// you can set null, or delete d.fx, delete d.fy keys
d.fx = null;
d.fy = null;
d3.select(this).select("text").transition()
.duration(750)
.attr("x", 22)
.style("stroke", "blue")
.style("stroke-width", ".5px")
.style("font", "20px sans-serif");
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", 16);};
};
根据文档和SO链接here,我能够成功地将节点设置为固定位置(如果执行语句)。但是我无法触发else语句,而且我敢肯定我忽略了这件事。有人有什么想法吗?
我添加了我的拖动功能,因为我认为这与它有关。
function dragstarted(d) {
console.log('drag start');
if (!d3.event.active) force.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
};
function dragged(d) {
console.log('drag mid');
d.fx = d3.event.x;
d.fy = d3.event.y;
};
function dragended(d) {
console.log('drag end');
if (!d3.event.active) force.alphaTarget(0);
if (d.fx == null && d.fy ==null){
d.fx = d.x;
d.fy = d.y;
}
else{
d.fx = null;
d.fy = null;
}