node.js计算的d3力图节点错误

时间:2019-05-09 09:19:57

标签: node.js d3.js

我使用nodeJs,jsdom和d3.js生成力图SVG。但是我发现当我使用JavaScript来console.log(nodes)时,它显示的结果与使用nodeJs console.log(nodes)时显示的结果不同。我的代码有什么问题??非常感谢!

var nodes = [
                {"id": "Alice"},
                {"id": "Bob"},
                {"id": "Carol"}
              ];
              var links = [
                {"source": 0, "target": 1}, // Alice → Bob
                {"source": 1, "target": 2} // Bob → Carol
              ];
  var simulation = d3.forceSimulation(nodes)
                      .force("charge", d3.forceManyBody())
                      .force("link", d3.forceLink(links))
                      .force("center", d3.forceCenter(600,480));

  console.log(nodes);

JavaScript脚本的结果是:

0: {id: "Alice", index: 0, x: 622.4025737064424, y: 496.51521771247326, vy: 0.1328160656816146, …}
1: {id: "Bob", index: 1, x: 591.9106281989928, y: 487.4153114941289, vy: 0.04165202910621729, …}
2: {id: "Carol", index: 2, x: 585.4859779309652, y: 456.2824455

和NodeJS结果是:

[ { id: 'Alice', index: 0, x: 0, y: 0, vy: 0, vx: 0 },
  { id: 'Bob',
    index: 1,
    x: -7.373688780783198,
    y: 6.754902942615239,
    vy: 0,
    vx: 0 },
  { id: 'Carol',
    index: 2,
    x: 1.2363864559502138,
    y: -14.087985964343622,
    vy: 0,
    vx: 0 } ]
[ { id: 'Alice', index: 0, x: 0, y: 0, vy: 0, vx: 0 },
  { id: 'Bob',
    index: 1,
    x: -7.373688780783198,
    y: 6.754902942615239,
    vy: 0,
    vx: 0 },
  { id: 'Carol',
    index: 2,
    x: 1.2363864559502138,
    y: -14.087985964343622,
    vy: 0,
    vx: 0 } ]
[ { id: 'Alice', index: 0, x: 0, y: 0, vy: 0, vx: 0 },
  { id: 'Bob',
    index: 1,
    x: -7.373688780783198,
    y: 6.754902942615239,
    vy: 0,
    vx: 0 },
  { id: 'Carol',
    index: 2,
    x: 1.2363864559502138,
    y: -14.087985964343622,
    vy: 0,
    vx: 0 } ]

0 个答案:

没有答案