右键单击节点标题不返回返回值,而是返回完整函数作为标题。我只想表明我传递的回报。我无法理解如何解决这个问题。任何人都可以帮助我。答案不起作用,因为参数d的函数不是指节点。它给出了错误,如下面的答案所示。
var content = [
{
title: function(d) {
if(d.url) {
return 'Item 1';
}
},
action: function(elm, d, i) {
window.open(d.url, '_blank');
}
},
{
title: function(d) {
if(d.url1) {
return 'Item 2';
}
},
action: function(elm, d, i) {
if(d.url1){
window.open(d.url1, '_blank');
}
}
},
{
title: function(d) {
if(d.url2) {
return 'Item 3';
}
},
action: function(elm, d, i) {
window.open(d.url2, '_blank');
}
}
];
function collapse(d) {
if (d.children) {
d._children = d.children;
d._children.forEach(collapse);
d.children = null;
}
}
collapse(root);
update(root);
d3.select(self.frameElement).style("height", "800px");
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) { d.y = d.depth * 180; });
text = source;
// Update the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
.on('contextmenu', d3.contextMenu(content))
.on("click", click);
nodeEnter.append("circle")
.attr("r", 1e-6)
.style("fill", function(d) { return d._children ? (d.status ? d.status : "#f77a03") : "#fff";
});//changed
nodeEnter.append("text")
.attr("x", function(d) { return d.children || d._children ? -10 : 10; })
.attr("dy", function(d) { return d.children || d._children ? -10 : 10; })
.style('fill',function(d) { return d.status ? d.status : "white"; })//added
.attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
.text(function(d) { return d.name; })
.style("fill-opacity", 1e-6);
nodeEnter.append("image")
.attr("xlink:href", function(d) { img = filter_click(r);return eval(img);})
.attr("x", "-9px")
.attr("y", "-20px")
.attr("width", "20px")
.attr("height", "40px");
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
nodeUpdate.select("circle")
.attr("r", 4.5)
.style("fill", function(d) { return d._children ? (d.status ? d.status : "#f77a03") : "#fff"; });//changed
nodeUpdate.select("text")
.style('fill',function(d) { return d.status ? d.status : "black"; })//added
.style("fill-opacity", 1);
// Transition exiting nodes to the parent's new position.
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
.remove();
nodeExit.select("circle")
.attr("r", 1e-6);
nodeExit.select("text")
.style("fill-opacity", 1e-6);
// Update the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
// Enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
});
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal);
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function(d) {
var o = {x: source.x, y: source.y};
return diagonal({source: o, target: o});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
这是一个plunker,它正好显示了我面临的问题,这里是plunker,其中script.js的代码正在运行我想要的脚本。
我正在尝试制作自定义内容菜单,如果我能够解决上述问题,可以这样做。任何帮助将不胜感激。
答案 0 :(得分:0)
您可以使用与数据相关的函数来代替content
的固定数组:
var content = function(d) {
menu=[ {
title: 'View Detail',
action: function(elm, d, i) {
window.open(d.url, '_blank');
}
}];
if ("url1" in d) {
menu.push({
title: 'View Map',
action: function(elm, d, i) {
window.open(d.url1, '_blank');
}
})
}
return menu;
}
答案 1 :(得分:0)