Last active
January 1, 2016 07:29
-
-
Save squarednob/8111877 to your computer and use it in GitHub Desktop.
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
function HierarchyToForcelayout(root,groups,values){ | |
//Change if you want. | |
groups = groups || "depth"; | |
values = values || "size"; | |
// Make ierarchy structure. | |
var test = hierarchy(root); | |
var forcejson = { "nodes": [], "links": []}; | |
//Get index for links target and source. | |
var name_index = {}; | |
test.forEach(function(d,i){ | |
name_index[d.name] = i; | |
}); | |
// Construct nodes and links for force layout. | |
test.forEach(function(d,i) { | |
var temp_node = {name: d.name, group: d[groups]}; | |
forcejson.nodes.push(temp_node); | |
if(d.children) { | |
d.children.forEach(function(c){ | |
var temp_link = {source: i, target:name_index[c.name], value:c[values]}; | |
forcejson.links.push(temp_link); | |
}) | |
} | |
}); | |
return forcejson; | |
}; |
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
#1 d3.csv to [{x:~},{y:~}] | |
var makeData = function(d,country){ | |
var list = []; | |
d.forEach(function(datum){ | |
var obj = {}; | |
obj["x"]= parseInt(datum.id); | |
obj["y"] = parseInt(datum[country]); | |
list.push(obj); | |
}) | |
return list; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment