2

我一直在使用 Google Chart 的 Sankey Diagram 开发应用程序,并在 Web 应用程序上成功创建了一个 Sankey Diagram 示例。但我对布局不满意,我在网上搜索并找到了 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 调用的对象集合。

如何将此数组用于桑基图?我可以在不调用外部文件的情况下做到这一点吗?

4

1 回答 1

1

我设法回答了我自己的问题(愚蠢的我)。我将发布此内容以供将来使用。

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

就我而言,原始代码:

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

希望这对未来的观点有所帮助。

于 2014-08-29T08:38:25.270 回答