Created
March 29, 2024 12:25
-
-
Save nikolaswise/db31bcdabce51662d344c328034ae599 to your computer and use it in GitHub Desktop.
Take a JSON-LD object and cast it into two arrays, one of nodes and one of links, for rendering with a D3 network visualization.
This file contains hidden or 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
const fn = (jsonLD, linkType) => { | |
let nodes = jsonLD['@graph'] | |
.filter(n => n) | |
.map(n => { | |
n.level = 0 | |
return n | |
}) | |
let connections = subjects["@graph"].map(node => { | |
return { | |
source: node.uri, | |
target: [...arrayify(node[linkType])].map(node => node.uri), | |
strength: 1.0 | |
} | |
}) | |
let links = [] | |
connections.forEach(concept => { | |
concept.target.forEach(target => { | |
links.push({ | |
source: concept.source, | |
target: target, | |
strength: 1 | |
}) | |
}) | |
}) | |
// { target: "mammal", source: "dog" , strength: 1.0 }, | |
return { | |
nodes: [ | |
...nodes, | |
], | |
links: links | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment