Skip to content

Commit

Permalink
Add SVG-based treemap example.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasondavies committed Jul 20, 2011
1 parent 3c2cb84 commit d445948
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
22 changes: 22 additions & 0 deletions examples/treemap/treemap-svg.html
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="../../d3.js"></script>
<script type="text/javascript" src="../../d3.layout.js"></script>
<style type="text/css">

rect {
fill: none;
stroke: #fff;
}

text {
font: 10px sans-serif;
}

</style>
</head>
<body>
<script type="text/javascript" src="treemap-svg.js"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions examples/treemap/treemap-svg.js
@@ -0,0 +1,34 @@
var w = 960,
h = 500,
color = d3.scale.category20c();

var treemap = d3.layout.treemap()
.size([w + 1, h + 1])
.value(function(d) { return d.size; })
.sticky(true);

var svg = d3.select("body").append("svg:svg")
.style("width", w)
.style("height", h)
.append("svg:g")
.attr("transform", "translate(-.5,-.5)");

d3.json("../data/flare.json", function(json) {
var cell = svg.data([json]).selectAll("g")
.data(treemap)
.enter().append("svg:g")
.attr("class", "cell")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

cell.append("svg:rect")
.attr("width", function(d) { return d.dx; })
.attr("height", function(d) { return d.dy; })
.style("fill", function(d) { return d.children ? color(d.data.name) : null; });

cell.append("svg:text")
.attr("x", function(d) { return d.dx / 2; })
.attr("y", function(d) { return d.dy / 2; })
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) { return d.children ? null : d.data.name; });
});

0 comments on commit d445948

Please sign in to comment.