d3.js sankey图-使用多角数组作为节点

d3.js sankey diagram - using multi dime array as nodes

本文关键字:数组 节点 sankey js d3      更新时间:2023-09-26

我一直在使用Google Chart的Sankey Diagram开发一个应用程序,并在一个web应用程序上成功创建了一个Sankey Diagraph示例。但我对布局不满意,我在网上搜索了一下,发现了d3.js的Sankey Diagram插件。它看起来真的很好,我从这里测试了示例代码应用程序。

在代码中,它调用外部JSON文件将其用作节点:

d3.json("sankey-formatted.json", function(error, graph) { ... });

在我开发的第一个应用程序中,我从这样的多维数组中获取节点:

//rows is the multidimensional array
//i got this from the example on Google Charts
data.addRows(rows);  

多维数组是来自AJAX调用的对象的集合。

如何将此数组用于桑基图?我可以在不调用外部文件的情况下执行此操作吗?

我设法回答了自己的问题(愚蠢的我)。我会把这个贴出来以备将来使用。

如果您已经有一个保存数据的变量(即数组),请直接使用它。

在我的情况下,原始代码:

d3.json("sankey-formatted.json", function(error, graph) {
  //sankey is an object, as well as the graph
  sankey
      .nodes(graph.nodes)
      .links(graph.links)
      .layout(32);
 // ... other codes
});

将其更改为

// this time, nodes and links are the variables
// that holds the arrays to be used by the chart
// remove the encapsulation of d3.json
sankey
    .nodes(nodes)
    .links(links)
    .layout(32);
// ... other codes

希望这将有助于未来的观点。