Javascript D3js树形图在添加换行符和自定义转换后变得无响应
我无法将换行符添加到此功能中。此操作的基本代码取自此。我还添加了使用宽度优先样式和名为“autoOpen”的函数进入过渡。最初,我实现了本文中所示的换行。在我实现“自动打开”功能之前,它一直运行良好。当我实现这两个函数时,树将变得无响应。有什么方法可以同时实现这两个功能吗? 多谢各位 编辑:我从Javascript D3js树形图在添加换行符和自定义转换后变得无响应,javascript,d3.js,svg,Javascript,D3.js,Svg,我无法将换行符添加到此功能中。此操作的基本代码取自此。我还添加了使用宽度优先样式和名为“autoOpen”的函数进入过渡。最初,我实现了本文中所示的换行。在我实现“自动打开”功能之前,它一直运行良好。当我实现这两个函数时,树将变得无响应。有什么方法可以同时实现这两个功能吗? 多谢各位 编辑:我从 你说没有反应是什么意思?您看到了什么?@KWeiss仅显示根节点,如果单击它,则不会显示任何内容。控制台中有错误吗? function buildTree(treeData){ var margi
你说没有反应是什么意思?您看到了什么?@KWeiss仅显示根节点,如果单击它,则不会显示任何内容。控制台中有错误吗?
function buildTree(treeData){
var margin = {top:40, right:120,bottom:20,left:20};
var width = 960 - margin.right - margin.left;
var height = 900 - margin.top - margin.bottom;
var i = 0,duration = 750;
//refers to the tree itself
var tree = d3.layout.tree()
.size([height,width])
.nodeSize([100,100]);
var diagonal = d3.svg.diagonal()
.projection(function(d){
return [d.x, d.y];
});
//refers to the rectangle outside
var zm;
var svg = d3.select("#tree").append("svg")
.attr("width",width+margin.right+margin.left)
.attr("height",height+margin.top+margin.bottom)
.append("svg:g")
.attr("transform","translate("+margin.left+","+margin.top+")")
.call(zm = d3.behavior.zoom().scaleExtent([0.5,2]).on("zoom", redraw)).append("g")
.attr("transform","translate("+400+","+50+")");
zm.translate([400,20]);
var root = treeData;
update(root);
autoOpen(root,1000);
function update(source){
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
nodes.forEach(function(d){
d.y = d.depth * 100;
});
var node = svg.selectAll("g.node")
.data(nodes,function(d){
return d.id || (d.id = ++ i);
});
var nodeEnter = node.enter().append("svg:g")
.attr("class","node")
.attr("transform",function(d){
if(!source.x0 && !source.y0)
return "";
return "translate("+source.x0 + "," + source.y0 + ")";
})
.on("click",nodeClick)
.on('contextmenu', d3.contextMenu(contextMenuList));
nodeEnter.append("circle")
.attr("r",40)
.attr("stroke",function(d){
return d.children || d._children ? "steelblue" : "steelblue";
})
.style("fill",function(d){
return d.children || d._children ? "lightsteelblue" : "#fff";
})
nodeEnter.append("text")
.attr("y",function(d){
//return d.children || d._children ? -18 : 18;
return 0;
})
.attr("dy",".35em")
.attr("text-anchor","middle")
.text(function(d){
return d.name;
})
.style("fill-opacity",1e-6);
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform",function(d){
return "translate(" + d.x + "," + d.y + ")";
});
nodeUpdate.select("circle")
.attr("r",40)
.style("fill",function(d){
return d._children ? "lightsteelblue" : "#fff";
});
nodeUpdate.select("text")
.style("fill-opacity",1);
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform",function(d){
return "translate("+ source.x+","+source.y+")";
})
.remove();
nodeExit.select("circle")
.attr("r",1e-6);
nodeExit.select("text")
.style("fill-opacity",1e-6);
var link = svg.selectAll("path.link")
.data(links,function(d){
return d.target.id;
});
link.enter().insert("svg:path","g")
.attr("class","link")
.attr("d",function(d){
if(!source.x0 && !source.y0)
return "";
var o = {x:source.x0,y:source.y0};
return diagonal({source:o,target:o});
});
link.transition()
.duration(duration)
.attr("d",diagonal);
link.exit().transition()
.duration(duration)
.attr("d",function(d){
var o = {x:source.x,y:source.y};
return diagonal({source:o,target:o});
})
.remove();
nodes.forEach(function(d){
d.x0 = d.x;
d.y0 = d.y;
});
}
function nodeClick(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
function redraw() {
svg.attr("transform",
"translate(" + d3.event.translate + ")"
+ " scale(" + d3.event.scale + ")");
}
function autoOpen(head,time){
window.setTimeout(function(){
nodeClick(head);
if(head.children){
var timeOut = 1000;
head.children.forEach(function(child){
autoOpen(child,timeOut);
timeOut = timeOut + 1000;
})
}
},time);
}
}
buildTree(treeData);