Skip to content

Instantly share code, notes, and snippets.

@mayhewsw
Last active August 29, 2015 14:23
Show Gist options
  • Save mayhewsw/2221eb1a799bf4316231 to your computer and use it in GitHub Desktop.
Save mayhewsw/2221eb1a799bf4316231 to your computer and use it in GitHub Desktop.
Convert intelligible graph to D3 graph.
// almost d3-style graph
var graph = {"nodes" : [
{"name":"somenode"},
{"name":"somenewnode"}
],
"links":[
{"source":"somenode", "target":"somenewnode"}
]
};
function findNodeIndex(name) {
for (var i = 0; i < graph.nodes.length; i++) {
if (graph.nodes[i].name == name) {
return i;
}
}
return -1;
}
// rewrite edge sources and targets to be indices into graph.nodes.
graph.links.map(function (l) { l.source = findNodeIndex(l.source); l.target = findNodeIndex(l.target); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment