secret
Created

  • Download Gist
network-D3.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
//network
$('#network ul').each(function() {
var depth = $(this).parents('ul').length;
$(this).children().addClass('level-' + depth);
});
 
var w = $('#network').width();
var h = $('#network').height();
var r = 6;
 
var fill = d3.scale.category20();
 
var vis = d3.select("#network").append("svg:svg")
.attr("width", w)
.attr("height", h);
 
var nodes = self.nodes = d3.selectAll("#network ul li")[0],
links = self.links = nodes.slice(1).map(function(d) {
return {source: d, target: d.parentNode.parentNode};
});
 
var force = d3.layout.force()
.charge(-120)
.distance(3)
.nodes(nodes)
.links(links)
.size([w, h])
.start();
 
var link = vis.selectAll("line.link")
.data(links)
.enter().append("svg:line")
.style("stroke", "#ccc")
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
 
var node = vis.selectAll(".nodeg")
.data(nodes)
.enter().append('g')
.attr("class", "nodeg")
.call(force.drag);
 
 
node.append("svg:circle")
.style("fill", function(d) {
var color = d.getAttribute("data-color");
if(color==null){
var listahija = d.childNodes[2];
if(listahija!=null){
var itemlista = listahija.childNodes[1];
if(itemlista!=null){
//console.log(itemlista);
var color = itemlista.getAttribute("data-color");
var link = itemlista.childNodes[2];
if(link!=null){
//console.log(link);
var color = link.childNodes[0].getAttribute("data-color");
}
}
}
}
return(color);
})
.style("stroke", "#fff" )
.attr("x", -8)
.attr("y", -8)
.attr("r", function(d){
if( $(d).hasClass('current_page_item') == true ){
return 8;
}else{
return 5;
}
})
.on("click", function(d, i) { window.location = d.firstChild.getAttribute("href"); });
 
node.append("text")
.attr("x", 12)
.attr("y", ".35em")
.attr("class", "nodetext")
.attr("class", function(d) {
if($(d).hasClass('level-1')){
return "nodewhite";
}else{
return "nodetext";
}
})
.text(function(d) { return d.firstChild.innerText });
 
force.on("tick", function() {
var w = $('#network').width();
var h = $('#network').height();
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
 
node.attr("transform", function(d) { return "translate(" + Math.max(r, Math.min(w - r, d.x)) + "," + Math.max(r, Math.min(h - r, d.y)) + ")" });
 
// node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.