1

EDIT: This was a simple fix, I'd bound my link simulation up in a timer function, which meant I was trying to create an array before the links "existed" moving the graph.link.forEach function into the timer function has sorted it right out. Thanks to Gerardo for making me think properly about the problem!

So I'm working on a force chart and trying to use a modified version of the fade function and I've been trying to implement the fade function found here (and in many other examples).

The trouble is:

var linkedByIndex = {};
    graph.links.forEach(function (d) {
    linkedByIndex[d.source.index + "," + d.target.index] = 1;
});

returns an empty array. If I remove the .index, I get an array of source/target id's used to link the nodes.

Unfortunately, it seems to work fine with inline data in jsfiddle, but not when the data is from a get request. Also the source/target indexes show up in the links array in the console, so I'm not sure why the array's empty.

Any ideas?

Edit: An older version is here, linkedByIndex works in jsFiddle, but doesn't seem to after a PHP request.

The data structure is:

graph = "locations",
[
{"name": "a", "id": 1},
{"name": "b", "id": 2}
],
"nodes", 
[
{"name": "A", "n_id": 1 "location": 1},
{"name": "B","n_id": 2, "location": 2}
],
"links", 
[
{"source": "1", "target": "2"}
etc.
];
Oliver Houston
  • 313
  • 1
  • 9
  • 1
    If I remember correctly, the first time before you initialize your graph with the data, this: `linkedByIndex[d.source. + "," + d.target] = 1;` is valid. After that, this is valid: `linkedByIndex[d.source.index + "," + d.target.index]` because the links are bound to nodes. Can you provide a fiddle to better understand your issue? – mkaran Jun 23 '17 at 10:20
  • @mkaran edited to add fiddle. – Oliver Houston Jun 23 '17 at 11:31
  • 1
    The issue here is not if the data comes from a variable or a request. The issue is that, before using your `forEach`, you have to pass the data array to the `links()` function. This method will return, for each object, a property called `index`, which is *the zero-based index into links, assigned by this method* (from the API). – Gerardo Furtado Jun 23 '17 at 12:54
  • OK, I think I get it. The link rendering is bound up in a timer function (other stuff happens before adding links), so there's no index to the links when I am trying to create the array. I'm still slightly confused as the source and target index shows up in console.log(graph), from before the timer function runs... – Oliver Houston Jun 23 '17 at 13:36
  • Thanks again @GerardoFurtado, you got me to the solution, even if I didn't know the actual problem. – Oliver Houston Jun 23 '17 at 14:36
  • @OliverHouston `Console.log` doesn't necessarily shows the state of the object at the moment you logged it. For instance, consider this: `var obj = {foo:1}; console.log(obj); obj.foo = 2;`. What do you think you're gonna see at the console, `1` or `2`? You even may see `1`, but if you expand it you're gonna see `2`. – Gerardo Furtado Jun 23 '17 at 14:57
  • Weird, I guess the force functions update (expand) the console.log(graph) after the link simulation, but not the array. I think this problem materialised before I noticed, so I didn't realise what caused it. – Oliver Houston Jun 23 '17 at 22:06

0 Answers0