在 sankey d3 javascript 文件中嵌入 json
embedding json in sankey d3 javascript file
我在使用 csv 文件填充 sankey 图时遇到 CORS 问题,因此我想将其替换为嵌入式 json 以用于测试目的。我重新格式化了 csv 数据以使其成为 d3 sankey 插件所需的格式,但我不知道如何让 javascript 读取 json 输入来代替 csv。
我用来加载 csv 的代码如下:
/ load the data with d3.csv
d3.csv("testinput.csv", function(error, data) {
graph = {"nodes" : [], "links" : []};
data.forEach(function (d, i) {
var item = { source: d.source, target: d.target, values: [] };
for (var j=0; j < 101; j++) {
item.values.push(d['value'+j.toString()]);
}
portfolioValues.push(item);
graph.nodes.push({ "name": d.source });
graph.nodes.push({ "name": d.target });
graph.links.push({
source: portfolioValues[i].source,
target: portfolioValues[i].target,
value: portfolioValues[i].values[startingAllocation]
});
});
//this handy little function returns only the distinct / unique nodes
graph.nodes = d3.keys(d3.nest()
.key(function (d) { return d.name; })
.map(graph.nodes));
//it appears d3 with force layout wants a numeric source and target
//so loop through each link replacing the text with its index from node
graph.links.forEach(function (d, i) {
graph.links[i].source = graph.nodes.indexOf(graph.links[i].source);
graph.links[i].target = graph.nodes.indexOf(graph.links[i].target);
portfolioValues[i].source = graph.links[i].source;
portfolioValues[i].target = graph.links[i].target;
});
//now loop through each nodes to make nodes an array of objects rather than an array of strings
graph.nodes.forEach(function (d, i) {
graph.nodes[i] = { "name": d };
});
我想使用的 json 是:
{
"nodes": [
{"node":1,"name":"AA"},
{"node":2,"name":"BB"},
{"node":3,"name":"XX"},
{"node":4,"name":"YY"}
],
"links": [
{"target":3,"source":1,"value":300},
{"target":4,"source":2,"value":0},
{"target":3,"source":2,"value":0},
{"target":4,"source":2,"value":200}
]}
Here 是 Sankey diagram
:
的 jsfiddle
json完全嵌入在javascript中,封装在函数getData()中。
我在使用 csv 文件填充 sankey 图时遇到 CORS 问题,因此我想将其替换为嵌入式 json 以用于测试目的。我重新格式化了 csv 数据以使其成为 d3 sankey 插件所需的格式,但我不知道如何让 javascript 读取 json 输入来代替 csv。
我用来加载 csv 的代码如下:
/ load the data with d3.csv
d3.csv("testinput.csv", function(error, data) {
graph = {"nodes" : [], "links" : []};
data.forEach(function (d, i) {
var item = { source: d.source, target: d.target, values: [] };
for (var j=0; j < 101; j++) {
item.values.push(d['value'+j.toString()]);
}
portfolioValues.push(item);
graph.nodes.push({ "name": d.source });
graph.nodes.push({ "name": d.target });
graph.links.push({
source: portfolioValues[i].source,
target: portfolioValues[i].target,
value: portfolioValues[i].values[startingAllocation]
});
});
//this handy little function returns only the distinct / unique nodes
graph.nodes = d3.keys(d3.nest()
.key(function (d) { return d.name; })
.map(graph.nodes));
//it appears d3 with force layout wants a numeric source and target
//so loop through each link replacing the text with its index from node
graph.links.forEach(function (d, i) {
graph.links[i].source = graph.nodes.indexOf(graph.links[i].source);
graph.links[i].target = graph.nodes.indexOf(graph.links[i].target);
portfolioValues[i].source = graph.links[i].source;
portfolioValues[i].target = graph.links[i].target;
});
//now loop through each nodes to make nodes an array of objects rather than an array of strings
graph.nodes.forEach(function (d, i) {
graph.nodes[i] = { "name": d };
});
我想使用的 json 是:
{
"nodes": [
{"node":1,"name":"AA"},
{"node":2,"name":"BB"},
{"node":3,"name":"XX"},
{"node":4,"name":"YY"}
],
"links": [
{"target":3,"source":1,"value":300},
{"target":4,"source":2,"value":0},
{"target":3,"source":2,"value":0},
{"target":4,"source":2,"value":200}
]}
Here 是 Sankey diagram
:
json完全嵌入在javascript中,封装在函数getData()中。