context: I'm a newbie to d3 and still very green to programming in general. I'm trying to make an extension that tracks your history and displays it hierarchically in d3. I have chrome extension that logs browser events and then I convert that into an SQL table that looks like this:
ID URL Parent
-------------------------
001 URL null (root of tree)
002 URL 01
003 URL 01
004 URL 02
005 URL 03
I then regroup the rows so that all the children are directly beneath their parents and the table looks like this:
ID URL Parent
---------------------
001 URL null
002 URL 001
004 URL 002
003 URL 001
005 URL 003
Now what I need to figure out is how to convert this new SQL table into a JSON file formatted like the "flare.json" file used for all the tutorials.
Thanks in advance! I'm still learning how to ask good question so let me know if there's anything I can clarify.
ID URL Parent
---------------------
001 URL null
002 URL 001
004 URL 002
003 URL 001
005 URL 003
If you want to display a graph, and all you have is a list of edges, then you'll want to iterate over the edges in order to produce an array of nodes and an array of links. Say you had a file called "graph.csv":
source,target
A1,A2
A2,A3
A2,A4
You could load this file using d3.csv and then produce an array of nodes and links:
d3.csv("graph.csv", function(links) {
var nodesByName = {};
// Create nodes for each unique source and target.
links.forEach(function(link) {
link.source = nodeByName(link.source);
link.target = nodeByName(link.target);
});
// Extract the array of nodes from the map by name.
var nodes = d3.values(nodeByName);
function nodeByName(name) {
return nodesByName[name] || (nodesByName[name] = {name: name});
}
});
ID,URL,Parent
001,URL,null
002,URL,001
004,URL,002
003,URL,001
005,URL,003
You could load this file using d3.csv and then produce an array of nodes and links:
d3.csv("graph.csv", function(links) {
var nodesByName = {};
// Create nodes for each unique source and target.
// Also process the link object:
// add source+target members to match layout requirements.
// CSV input is assumed to have each row only reference Parent nodes
// which are known, i.e. have lower IDs ~ have been listed before in the CSV.
links.forEach(function(link) {
link = transformLink(link);
});
// Extract the array of nodes from the map by name.
var nodes = d3.values(nodeByName);
function transformLink(link) {
// construct the node object from the CSV row
var source = nodesByName[link.ID] || (nodesByName[link.ID] = {name: link.ID, url: link.URL});
// CSV input is assumed to have each row only reference Parent nodes
// which are known, i.e. have lower IDs ~ have been listed before in the CSV.
var target = nodesByName[link.Parent];
// construct the layout-conforming link object by augmenting it with the required members
link.source = source;
link.target = target;
}
});
d3.csv("graph.csv", function(links) {
var nodesByName = {};
// Create nodes for each unique source and target.
// Also process the link object:
// add source+target members to match layout requirements.
// CSV input is assumed to have each row only reference Parent nodes
// which are known, i.e. have lower IDs ~ have been listed before in the CSV.
links.map(function(link) {
return transformLink(link);
});
// when you get here, the loaded 'links' array is replaced by the processed links []
// Extract the array of nodes from the map by name.
var nodes = d3.values(nodeByName);
function transformLink(link) {
// construct the node object from the CSV row
var source = nodesByName[link.ID] || (nodesByName[link.ID] = {name: link.ID, url: link.URL});
// CSV input is assumed to have each row only reference Parent nodes
// which are known, i.e. have lower IDs ~ have been listed before in the CSV.
var target = nodesByName[link.Parent];
// construct the layout-conforming link object, stripped from all surplus
return { source: source, target: target };
}
});
[Usual caveat/cop-out applies: code has been monkey-banged straight into the email, so take it to the vet for delousing before cuddling; one known problem is handling the NULL reference in the CSV data, e.g. in the first row.