繁体   English   中英

d3:画笔更改单击以将鼠标悬停

[英]d3: brush changes click to mouseover

我正在尝试使用画笔选择,工具提示和单击事件的组合来创建散点图,但是似乎一旦将画笔添加到svg画布后,对象上的所有单击事件都将映射到鼠标悬停。 有没有办法解决? 下面的示例代码和@ http://jsfiddle.net/7j8cr/

<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>

var my_circles = [{"x": 40, "y": 100, "r": 12},
              {"x": 100, "y": 40, "r": 8}],
    width = 400,
    height = 400,
    svg = d3.select("body").append("svg")
        .attr("width", width)
        .attr("height", height);

function click(d) { console.log("Clicky Clicky!") };
function mouseover(d) { console.log("I saw  mouse!") }

var brush = d3.svg.brush()
    .x(d3.scale.identity().domain([0, width]))
    .y(d3.scale.identity().domain([0, height]))

svg.call(brush);

svg.selectAll("dataObj")
    .data(my_circles)
    .enter().append("circle")
        .attr("r", function(d) { return d.r })
        .attr("cx", function(d) { return d.x })
        .attr("cy", function(d) { return d.y })
        .style("fill", "blue")
        .on("mouseover", mouseover)
        .on("click", click); 

</script>

单击圆圈会触发mouseover() ,您可以通过注释掉该行来获得相同的操作来触发正确的事件

svg.call(brush);

但是,显然您松开了画笔...。是否有办法让所有3个组件正确执行操作?

click事件不会转换为mousover,它们根本不会发生-您将鼠标移到圆圈上以单击它时看到的mouseover事件。 但是,该问题有一个简单的解决方案-您所需要做的就是将pointer-events设置为圈子中的all

这里完整的例子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM