toolkit/javascript/d3/examples/bundle/bundle-treemap.js
changeset 47 c0b4a8b5a012
equal deleted inserted replaced
46:efd9c589177a 47:c0b4a8b5a012
       
     1 var w = 960,
       
     2     h = 500,
       
     3     fill = d3.scale.ordinal().range(colorbrewer.Greys[9].slice(1, 4)),
       
     4     stroke = d3.scale.linear().domain([0, 1e4]).range(["brown", "steelblue"]);
       
     5 
       
     6 var treemap = d3.layout.treemap()
       
     7     .size([w, h])
       
     8     .value(function(d) { return d.size; });
       
     9 
       
    10 var bundle = d3.layout.bundle();
       
    11 
       
    12 var div = d3.select("#chart").append("div")
       
    13     .style("position", "relative")
       
    14     .style("width", w + "px")
       
    15     .style("height", h + "px");
       
    16 
       
    17 var line = d3.svg.line()
       
    18     .interpolate("bundle")
       
    19     .tension(.85)
       
    20     .x(function(d) { return d.x + d.dx / 2; })
       
    21     .y(function(d) { return d.y + d.dy / 2; });
       
    22 
       
    23 d3.json("../data/flare-imports.json", function(classes) {
       
    24   var nodes = treemap.nodes(packages.root(classes)),
       
    25       links = packages.imports(nodes);
       
    26 
       
    27   div.selectAll("div")
       
    28       .data(nodes)
       
    29     .enter().append("div")
       
    30       .attr("class", "cell")
       
    31       .style("background", function(d) { return d.children ? fill(d.key) : null; })
       
    32       .call(cell)
       
    33       .text(function(d) { return d.children ? null : d.key; });
       
    34 
       
    35   div.append("svg:svg")
       
    36       .attr("width", w)
       
    37       .attr("height", h)
       
    38       .style("position", "absolute")
       
    39     .selectAll("path.link")
       
    40       .data(bundle(links))
       
    41     .enter().append("svg:path")
       
    42       .style("stroke", function(d) { return stroke(d[0].value); })
       
    43       .attr("class", "link")
       
    44       .attr("d", line);
       
    45 });
       
    46 
       
    47 function cell() {
       
    48   this
       
    49       .style("left", function(d) { return d.x + "px"; })
       
    50       .style("top", function(d) { return d.y + "px"; })
       
    51       .style("width", function(d) { return d.dx - 1 + "px"; })
       
    52       .style("height", function(d) { return d.dy - 1 + "px"; });
       
    53 }