2

我一直在使用Google Chart的Sankey图开发应用程序,并成功在Web应用程序上创建了Sankey图示例。但是我对布局不满意,我搜索了互联网,并找到了d3.js的Sankey Diagram插件。它看起来非常好,我测试了here的示例代码应用程序。d3.js sankey图 - 使用多角度阵列作为节点

在代码中,它调用外部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调用的对象的集合。

如何使用此数组作为Sankey图?我可以不用调用外部文件吗?

+0

所以你已经有了数据,你只是想不通过'd3.json()'把它传递给插件? – Oleg 2014-08-29 08:23:42

回答

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 

希望这将有助于为未来的看法。