图形获胜'无法正确重新加载到D3部队布局中
Graph won't reload properly into D3 force layout
我在玩D3,并制作力定向图。我想清除并重新加载图形以响应单击按钮。但是,当我重新加载已经加载的图形时,该图形的格式不正确。这是我正在使用的代码:
<!DOCTYPE html>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v2.js?2.9.1"></script>
<style>
.link {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
.node circle {
fill: #ccc;
stroke: #fff;
stroke-width: 1.5px;
}
text {
font: 10px sans-serif;
pointer-events: none;
}
</style>
<body>
<script>
var all = [
{source: "Tockwotton", target: "List", type: "woodworking"},
{source: "Tockwotton", target: "Prince Lab", type: "woodworking"},
{source: "Prince Lab", target: "Prince Lab", type: "metalworking"},
{source: "Tockwotton", target: "List", type: "cnc"},
{source: "Granoff", target: "Prince Lab", type: "3d-printing"},
{source: "Tockwotton", target: "List", type: "drafting"},
{source: "List", target: "Prince Lab", type: "welding"},
{source: "Prince Lab", target: "Prince Lab", type: "sand-blaster"},
{source: "Tockwotton", target: "List", type: "finishing"},
{source: "Tockwotton", target: "Prince Lab", type: "finishing"},
{source: "Tockwotton", target: "Tockwotton", type: "laser-cutter"}
];
var wood = [
{source: "Tockwotton", target: "List", type: "woodworking"},
{source: "Tockwotton", target: "Prince Lab", type: "woodworking"}
];
var metal = [
{source: "Prince Lab", target: "Prince Lab", type: "metalworking"}
];
var cnc = [
{source: "Tockwotton", target: "List", type: "cnc"}
];
var print = [
{source: "Granoff", target: "Prince Lab", type: "3d-printing"}
];
var draft = [
{source: "Tockwotton", target: "List", type: "drafting"}
];
var weld = [
{source: "List", target: "Prince Lab", type: "welding"}
];
var sand = [
{source: "Prince Lab", target: "Prince Lab", type: "sand-blaster"}
];
var finishing = [
{source: "Tockwotton", target: "List", type: "finishing"},
{source: "Tockwotton", target: "Prince Lab", type: "finishing"}
];
var laser = [
{source: "Tockwotton", target: "Tockwotton", type: "laser-cutter"}
];
function render(linkdata){
d3.selectAll("svg").remove();
links = linkdata;
nodes = {};
// Compute the distinct nodes from the links.
links.forEach(function(link) {
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source, type: link.type});
link.target = nodes[link.target] || (nodes[link.target] = {name: link.target, type: link.type});
});
width = 960,
height = 500;
force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([width, height])
.linkDistance(60)
.charge(-300)
.on("tick", tick)
.start();
svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
link = svg.selectAll(".link")
.data(force.links())
.enter().append("line")
.attr("class", "link");
node = svg.selectAll(".node")
.data(force.nodes())
.enter().append("g")
.attr("class", "node")
.on("mouseover", mouseover)
.on("mouseout", mouseout)
.call(force.drag);
node.append("circle")
.attr("r", 8);
node.append("text")
.attr("x", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name; });
function tick() {
link
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
}
function mouseover() {
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", 16);
}
function mouseout() {
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", 8);
}
}
</script>
<div id="nav">
<div id="wood">
<a onclick="render(wood)">Woodworking</a>
</div>
<div id="metal">
<a onclick="render(metal)">Metalworking</a>
</div>
<div id="cnc">
<a onclick="render(cnc)">CNC</a>
</div>
<div id="print">
<a onclick="render(print)">3D Printing</a>
</div>
<div id="draft">
<a onclick="render(draft)">Drafting</a>
</div>
<div id="weld">
<a onclick="render(weld)">Welding</a>
</div>
<div id="sand">
<a onclick="render(sand)">Sand Blast</a>
</div>
<div id="finishing">
<a onclick="render(finishing)">Finishing</a>
</div>
<div id="laser">
<a onclick="render(laser)">Laser Cutting</a>
</div>
<div id="all">
<a onclick="render(all)">All</a>
</div>
</div>
</body>
尝试一下并注意,第一次单击某个项目时,它运行良好,但如果再次单击,则只会得到一个名为[object Object]
的节点。为什么会这样?我该如何解决?
当您使用links = linkdata
时,它可能没有达到您期望的效果。与其他一些语言一样,这不会复制数据;相反,它只是使links
指向与linkdata
指向的相同位置。当你循环遍历links
,将它们修改为彼此指向时,你就是在修改数组中的原始对象。当你再次循环浏览它们时,它们已经被修改了,从而导致你正在经历的不良行为。
为了避免这种命运,你需要在修改数组之前对其进行深度复制。有很多方法可以做到这一点,但你需要确保无论使用什么解决方案,都会将数组克隆为数组,而不会将其变成普通的Object
。
相关文章:
- 将基本CSV数据加载到D3
- d3转换从页面加载开始,而不是从模式弹出加载开始
- D3.js:通过Force Layout从JSON文件加载多个网络
- D3.js可以't加载json文件
- 我能在d3中加载.gml吗
- 定位D3 SVG画布x/y和加载时的缩放级别
- d3.js继续条件加载数据
- 将csv文件加载到d3中,而不发送到服务器
- 将形状加载到d3中
- D3.csv未加载csv文件
- d3.js和加载具有特殊字符的数据
- 如何从外部将json加载到D3应用程序
- 将JSON从rails控制器加载到javascript文件中的d3.dedefe()函数
- 创建加载 d3.js 的浮动图像
- 加载D3 Url与API密钥
- 在D3之前加载D3依赖
- 缓慢加载d3 / c3图表
- 未定义加载D3圆包时的功能
- 加载内部 JSON 数据,而不是从外部资源/文件加载 d3 中的旭日
- 为什么这个json文件不能在Meteor [ubuntu]中加载d3 ?