Last active
August 29, 2015 14:23
-
-
Save mayhewsw/2221eb1a799bf4316231 to your computer and use it in GitHub Desktop.
Convert intelligible graph to D3 graph.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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