D3JS
D3 allows you to bind arbitrary data to a Document Object Model (DOM), and then apply data-driven transformations to the document. For example, you can use D3 to generate an HTML table from an array of numbers. Or, use the same data to create an interactive SVG bar chart with smooth transitions and interaction.
There are two kinds of graph templates in d3js that have been tried out for the story VSPC-99.
Force Directed Graph.
I found this youtube tutorial helpful to understand how to make a bare-bones force directed graph Tutorial Link. Each node on the graph is a Json, and the nodes are represented as an array or list of json objects. This is this API list for the force directed graph. Main API’s we need are: forceSimulation, force and the listener ‘on’.
One of the ‘force’ api’s called ‘link’ allows the binding of links between the list of nodes specified in forceSimulation. We need to specify the id that is used to identify the source and target of the nodes and have our links specified with the source and target having the same value as the identity in the nodes.
Snippet of code
var simulation = d3.forceSimulation(graph.nodes) |
Sample JSON of nodes
nodes: Array(77) [ ] |
Sample JSON of links
links: Array(254) [ ] |
2. Tree Graph
This is useful to display tree structure of a json. The Json structure must be of the type:
var treeData = [
{
"name": "Top Level",
"parent": "null",
"children": [
{
"name": "Level 2: A",
"parent": "Top Level",
"children": [
{
"name": "Son of A",
"parent": "Level 2: A"
},
{
"name": "Daughter of A",
"parent": "Level 2: A"
}
]
},
{
"name": "Level 2: B",
"parent": "Top Level"
}
]
}
];
children contains the next level of each node. It is an array, and each json represents one child in the array.
A good article I found for this is found Here.
The D3js library can be loaded like this
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
We initialize the tree like this:
d3.layout.tree()
.size([height, width]);
Next, the tree layout can be created like this:
This needs to be a part of the recursive function, which we can call update;
This blueprint helps to create the tree graph.
References