javascript - 图表无法正确重新加载到 D3 强制布局中

标签 javascript d3.js 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>

Try it请注意,第一次单击某个项目时,它工作正常,但如果再次单击它,您只会获得一个名为 [object Object] 的节点。这是为什么?我该如何解决这个问题?

最佳答案

当您使用links = linkdata时,它可能不会执行您期望的操作。与其他一些语言一样,这不会复制数据;而是复制数据。相反,它只是使 links 指向 linkdata 指向的同一位置。当您循环遍历链接并将它们修改为彼此指向时,您正在修改数组中的原始对象。当您再次循环它们时,它们已经被修改,导致您遇到不良行为。

为了避免这种命运,您需要在修改数组之前对其进行深度复制。有quite a few ways to do that ,但您需要确保您使用的任何解决方案都会将数组克隆为数组,而不会将其转换为普通的对象

关于javascript - 图表无法正确重新加载到 D3 强制布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17690703/

相关文章:

javascript - 同时移动所有节点(包括链接)?

javascript - 意外的 JS 原型(prototype)行为

javascript - D3 : When I add a transition, 我的鼠标悬停停止工作...为什么?

javascript - 使用 React 绘制 d3 轴,无需直接操作 D3 DOM

javascript - 如何使用 d3.js 从外部 json 文件读取特定属性?

javascript - 从 d3 中的节点中删除文本

javascript - 使用 ReactDOM 输入后添加跨度

javascript - 如何在实用程序文件中获取已启动的 Express 应用程序的服务器主机和端口

javascript - Bootstrap 3 固定顶部导航栏

layout - D3 : Multiple force layouts in one SVG?