Skip to content

Instantly share code, notes, and snippets.

@ko31
Created November 17, 2015 17:57
Show Gist options
  • Save ko31/398527fecae9af72c87a to your computer and use it in GitHub Desktop.
Save ko31/398527fecae9af72c87a to your computer and use it in GitHub Desktop.
【D3.js】Adventar登録状況のツリーマップ
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>【Adventar】2015年アドベントカレンダー登録状況</title>
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: auto;
position: relative;
width: 960px;
}
form {
position: absolute;
right: 10px;
top: 10px;
}
.node {
border: solid 1px white;
font: 10px sans-serif;
line-height: 12px;
overflow: hidden;
position: absolute;
text-indent: 2px;
}
</style>
</head>
<body>
<p>【Adventar】2015年アドベントカレンダー登録状況</p>
<form>
<label><input type="radio" name="mode" value="count" checked> 登録件数</label>
<label><input type="radio" name="mode" value="length"> タイトル文字数</label>
</form>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 40, right: 10, bottom: 10, left: 10},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var treemap = d3.layout.treemap()
.size([width, height])
.sticky(true)
.value(function(d) { return d.count; });
var div = d3.select("body").append("div")
.style("position", "relative")
.style("width", (width + margin.left + margin.right) + "px")
.style("height", (height + margin.top + margin.bottom) + "px")
.style("left", margin.left + "px")
.style("top", margin.top + "px");
d3.json("adventar.json", function(error, root) {
if (error) throw error;
var node = div.datum(root).selectAll(".node")
.data(treemap.nodes)
.enter().append("div")
.attr("class", "node")
.attr("style", "color:#fff;")
.attr("xlink:href", '/')
.call(position)
.style("background", function(d) { return d.style;}) //Adventarの背景色セット
.text(function(d) { return d.children ? null : d.name+'('+d.count+')'; })
.on("click", function(d){
window.open('http://www.adventar.org'+d.url, '_blank'); //AdventarのURLにリンク
});
d3.selectAll("input").on("change", function change() {
var value = this.value === "length"
? function(d) { return d.name.length; }
: function(d) { return d.count; };
node
.data(treemap.value(value).nodes)
.transition()
.duration(1500)
.call(position);
});
});
function position() {
this.style("left", function(d) { return d.x + "px"; })
.style("top", function(d) { return d.y + "px"; })
.style("width", function(d) { return Math.max(0, d.dx - 1) + "px"; })
.style("height", function(d) { return Math.max(0, d.dy - 1) + "px"; });
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment