javascript - 在D3气泡布局中,有没有办法通过jQuery监听气泡的名称并添加交互?

标签 javascript jquery html d3.js

我正在构建一个 D3 可缩放圆形填充气泡布局,如下所示: http://bl.ocks.org/mbostock/7607535

我了解了 json 的想法并设法使用我自己的 json 数据。 不过,我想通过 jQuery 为气泡添加交互性。

大致如下:

$(IdOfCurrentBubble).mouseover(function({
    play sound (iDOfCurrentBubble.mp3);
    doStuff;
    doOtherStuff;
});

最佳答案

为什么是jquery?使用d3添加事件处理程序:

var circle = svg.selectAll("circle")
  .data(nodes)
  .enter().append("circle")
  .attr("class", function(d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; })
  .style("fill", function(d) { return d.children ? color(d.depth) : null; })
  .on("click", function(d) { if (focus !== d) zoom(d), d3.event.stopPropagation(); })
  .on("mouseover", function(d){ //<--here's your mouseover
    console.log(d.name);
  });

编辑

抱歉,我错过了通过 ID 应用函数的部分。

var circle = svg.selectAll("circle")
  .data(nodes)
  .enter().append("circle")
  .attr("class", function(d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; })
  .style("fill", function(d) { return d.children ? color(d.depth) : null; })
  .on("click", function(d) { if (focus !== d) zoom(d), d3.event.stopPropagation(); })

d3.select("#IdOfCurrentBubble")
  .on("mouseover", someFunc);

function someFunc(){
  var myId = d3.select(this).attr('id');
  // use myId here
}

编辑2

如果您希望鼠标悬停仅作用于最低级别的子级,请按类别定位它们:

d3.selectAll(".node--leaf")
  .on("mouseover", someFunc);   

关于javascript - 在D3气泡布局中,有没有办法通过jQuery监听气泡的名称并添加交互?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28126864/

相关文章:

javascript - 如何对字符串的最后 x 个字符执行数学计算并动态更新文本框?

javascript - 尝试使用 css 将图像添加到 SVG 圆

javascript - 如何使用 jQuery 为 slimScroll 设置动画

javascript - JSON.parse 意外字符

javascript - jQuery 困惑

javascript - moment js 日期库,在 IE 上格式化给出了 NaN

javascript - 返回 Javascript 变量和性能

php - 使整个表格单元格触发相关复选框

html - 如何遮蔽奇数但仅可见的标题?

javascript - 为什么不点击按钮就调用函数?