[英]Specifying Values in D3 Graph from JSON
I am trying to get values from the "links" instead of "nodes. I guess a more helpful way of asking this is how to specify where I am requesting getting the values from my JSON. 我正在尝试从“链接”而不是“节点”获取值。我猜这是一个更有用的方法,它是如何指定我请求从JSON获取值的位置。
JSON for reference: JSON供参考:
{
"nodes": [
{"fixed":true,"data": {"id": "foo","idType":"USERNAME","color":"red"},"selected":false},
{"fixed":true,"data": {"id": "bar","idType":"USERNAME","color":"yellow"},"selected": false}
],
"links": [
{"classes":null,"data":{"color":"blue","source":"foo","target":"bar","visible":true},"grabbable":false},
{"classes":null,"data":{"color":"green","source":"bar","target":"foo","visible":true},"grabbable":false}
]}
So an example is I can get this to work 所以一个例子是我可以让它工作
node.style("fill", function(d) { return d.data['color'] });
but not 但不是
link.style("stroke", function(d) { return d.data['color'] });
However, this works... 但是,这可行...
link.style("stroke", "red"});
In the console it says d.data['color'] is undefined. 在控制台中,它说d.data ['color']未定义。 I guess I do not understand how it is being called exactly. 我想我不明白它的确切名称。 I have seen some examples of code that has 我看过一些具有
function(d, i) { return bla bla }
and I assumed that if d is always node, maybe i might be edges but adding that to my code didn't do much. 并且我假设如果d始终是节点,也许我可能是边缘,但是将其添加到我的代码中并没有太大作用。 If someone could explain what the i is touching that would be good also. 如果有人可以解释我所触摸的内容,那也将很好。
Code snippet of actual code below: 实际代码的代码段如下:
// Define the dimensions of the visualization.
var width = innerWidth,
height = innerHeight,
color = d3.scale.category20(),
root;
// Create an array logging what is connected to what
var linkedByIndex = { };
// Create the SVG container for the visualization and define its dimensions
var svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height);
var link = svg.selectAll(".link"),
node = svg.selectAll(".node");
// Create the force layout
var force = d3.layout.force()
.size([width, height])
.charge(-400)
.linkDistance(50);
//Read in the JSON data.
d3.json("../test/resources/full.json", function(error, json) {
// expands scope of json
root = json
alert(root)
update();
});
function update() {
// sets the source and target to use id instead of index
var edges = [];
root.links.forEach(function(e) {
var sourceNode = root.nodes.filter(function(n) {
return n.data.id === e.data.source;
})[0],
targetNode = root.nodes.filter(function(n) {
return n.data.id === e.data.target;
})[0];
edges.push({
source: sourceNode,
target: targetNode
});
});
force
.nodes(root.nodes)
.links(edges)
.start();
link = link
.data(edges)
.enter().append("line")
.attr("class", "link");
node = node
.data(root.nodes)
.enter().append("g")
.attr("class", "node")
//.attr("fixed", function(d) { return d.fixed=true })
.call(force.drag)
.on('mouseover', connectedNodes)
.on('mouseleave', restore)
.on('click', highlight);
// Checks for the shape and color to be made for the node.
node.append("circle")
.attr("r", 10);
node.style("fill", function(d) { return d.data['color'] });
link.style("stroke", function(d) { return d.data['color'] });
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.style("fill", "black")
// Checks what to return as the node label based on idType.
.text(function (d) {
if (d.data['idType']==="Comment") {
return d.data.attrs[1].val;
}
else if(d.data['idType']==="MEDIA") {
return "MEDIA " + d.data['id'];
}
else
return d.data['id'];
});
root.links.forEach(function (d) {
linkedByIndex[d.data.source + "," + d.data.target] = 1;
});
resize();
window.addEventListener('resize', resize);
}
Per Lars' comment, edges does not contain the original link data. 根据拉尔斯的评论,边不包含原始链接数据。 So it was currently pushing only 因此,它目前仅在推动
edges.push({
source: sourceNode,
target: targetNode
});
so to add any information that isn't in my edges I would have to add it. 因此要添加不在我边缘的任何信息,我必须添加它。 An example is if I were to push the color attribute from JSON my code would look like 一个例子是,如果我要从JSON推送color属性,我的代码将看起来像
edges.push({
source: sourceNode,
target: targetNode,
color: e.data['color']
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.