代码链接:http://jsfiddle.net/mj58659094/yKUsQ/;
当点击人(节点)时,它也会选择其配偶。我只想选择(高亮显示)我点击的人(丈夫或妻子或孩子)。 (当我在FireBug中检查html时,配偶节点(g transform =“translate(0,70)”)位于人节点内部。我认为它们应该在外面,但在g class =“node”组内)。 我不知道如何解决这个问题。请有经验的人帮帮我。谢谢。
代码链接:http://jsfiddle.net/mj58659094/yKUsQ/;
当点击人(节点)时,它也会选择其配偶。我只想选择(高亮显示)我点击的人(丈夫或妻子或孩子)。 (当我在FireBug中检查html时,配偶节点(g transform =“translate(0,70)”)位于人节点内部。我认为它们应该在外面,但在g class =“node”组内)。 我不知道如何解决这个问题。请有经验的人帮帮我。谢谢。
更新:请见下文
我认为解决您的 onclick
问题的最好方式是将人的配偶放在与人相同的组中(而不是在嵌套组中)。除此之外,我认为您将 onclick
应用在了错误的地方。我的建议是:
Change lines 325-330 to the following
var node = g.append("svg:g").attr("class", "node")
.selectAll("g")
.data(function (d) { return d.nodes; })
.enter().append("svg:g");
node.append("svg:rect")
.on("click", clickedNode);
Currently, you were applying the onclick
to the group containing both the person and his/her spouses, while instead you want to use the onclick
on each person/spouse separately. Note that once you make this change, you will need to update your code to test each node's rect
(instead of the node's group g
) as to whether it is selected (lines 355-365). You will also have to update your CSS to style .node rect.normal
and .node rect.selected
on lines 25 and 29 of your CSS file.
The second issue is that you are only drawing the first spouse for each person. Currently the updateSpouses
function is iterating over each person, and then adding a group with a rectangle for just the first spouse. What you need to do is first add a group for each person with spouses, and then over each person's spouses. Here's a rough draft of how to modify updateSpouses
to get you started:
var node = svgTree.selectAll(".node g")
.append("svg:g").attr("transform", function (d, i) {
if (i == d3.familyTree.rootGeneration)
return "translate(" + (d.spouseX) + "," + (d3.familyTree.gapBetweenSpouses) + ")";
else
return "translate(" + 0 + "," + (d3.familyTree.gapBetweenSpouses) + ")";
})
.filter(function (d, i) { return personSpouses" in d });
var spouses = node.selectAll(".spouse")
.data(function(d){ return d.personSpouses }).enter()
.append("rect")
.on("click", clickedNode);
编辑
根据你的评论,我修改了你原来的代码http://jsfiddle.net/mdml/xJBXm/。这里是我所做的更改的快速概述:
onclick
属性(第141-146行),因此当单击矩形/文本时会调用clickedNode
。resetNodePersonSelected
和setNodePersonSelected
函数,以便它们除了更新子级外还可以搜索/更新配偶。我已经高层次地描述了更改,如果您对实现有任何更多问题,请告诉我。