我正在尝试创建一个 Sankey 图,虽然我现在可以加载我的源代码,但我在sankey.js和我的 html 代码之间遇到了一个小问题。
当我运行 HTML 代码时,我收到如下错误消息: Uncaught TypeError: Cannot call method 'push' of undefined
这是错误链接将我指向的位置:
// Populate the sourceLinks and targetLinks for each node.
// Also, if the source and target are not objects, assume they are indices.
function computeNodeLinks() {
nodes.forEach(function(node) {
node.sourceLinks = [];
node.targetLinks = [];
});
links.forEach(function(link) {
var source = link.source,
target = link.target;
if (typeof source === "name") source = link.source = nodes[link.source];
if (typeof target === "name") target = link.target = nodes[link.target];
source.sourceLinks.push(link);
target.targetLinks.push(link);
});
}
这是 HTML 代码中的输入(我的数据取自 csv 文件)。
//set up graph in same style as original example but empty
graph = {"nodes" : [], "links" : []};
data.forEach(function (d) {
graph.nodes.push({ "name": d.source });
graph.nodes.push({ "name": d.target });
graph.links.push({
"source": d.source,
"target": d.target,
"value": +d.value
});
});
我的问题是:sankey.js 是否依赖于我的数据来自 json 文件的假设?. 如果是这样,我该如何调整它以使其与 csv 文件兼容?数据保存为下面列出的 csv 文件:
source,target,value
Barry,Elvis,2
Frodo,Elvis,2
Frodo,Sarah,2
Barry,Alice,2
Elvis,Sarah,2
Elvis,Alice,2
Sarah,Alice,4