d3.js dendogram how to add links to leaf nodes and change colors

Viewed 24

I'm building this dendogram with D3.js and I'm fairly new to this so I'm having some trouble finding out how I'd add URLs to the leaf nodes (Ideally if I could use a python dictionary or a list to tell d3 which leaf nodes have which links that'd be perfect) and also how to change the colors of the nodes (text and circle) according to a variable I have in my database (I'd prefer to add this info via an auxiliary list or dictionary as well).

I'll leave my code and a picture of the actual dendogram.

Thank you so much!

<script type="text/javascript" src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>   
    var width = 2000;  
    var height = 3000;  
    var cluster = d3.layout.cluster()        
        .size([height, width-800]);  
    var diagonal = d3.svg.diagonal()        
        .projection (function(d) { return [d.y, d.x];});  
    var svg = d3.select("body").append("svg")        
        .attr("width",width)        
        .attr("height",height)        
        .append("g")        
        .attr("transform","translate(100,0)");  
    d3.json("{% url 'build_json' %}", function(error, root){        
        var nodes = cluster.nodes(root);        
        var links = cluster.links(nodes);        
        var link = svg.selectAll(".link")              
            .data(links)              
            .enter().append("path")              
            .attr("class","link")              
            .attr("d", diagonal);         
        var node = svg.selectAll(".node")              
            .data(nodes)              
            .enter().append("g")              
            .attr("class","node")              
            .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });        
        node.append("circle")              
            .attr("r", 4.5);        
        node.append("text")              
            .attr("dx", function(d) { return d.children ? -8 : 8; })
            .attr("dy", 3)              
            .style("text-anchor", function(d) { return d.children ? "end" : "start"; })             
            .text( function(d){ return d.name;});  
}); 
</script>
    .node circle {        
        fill: #fff;       
        stroke: steelblue;       
        stroke-width: 1.5px;  
    }  
    .node {       
        font: 20px sans-serif;  
    }  
    .link {       
        fill: none;       
        stroke: #ccc;       
        stroke-width: 1.5px;  
    } 
<div id="my_dataviz"></div>

dendogram

0 Answers
Related