This is the code for Chapter 5, Figure 16 from D3.js in Action demonstrating d3.behavior.zoom(). Click and drag the canvas to see more of the dendrogram.
Last active
March 18, 2016 03:34
-
-
Save emeeks/0cdbbba6db4d5ca994b5 to your computer and use it in GitHub Desktop.
Ch. 5, Fig. 16 - D3.js in Action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>D3 in Action Chapter 5 - Example 7</title> | |
<meta charset="utf-8" /> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
</head> | |
<style> | |
svg { | |
height: 500px; | |
width: 500px; | |
border: 1px solid gray; | |
} | |
</style> | |
<body> | |
<div id="viz"> | |
<svg> | |
</svg> | |
</div> | |
<div id="controls" /> | |
</body> | |
<footer> | |
<script> | |
d3.json("tweets.json",function(error,data) {dataViz(data.tweets)}); | |
function dataViz(incData) { | |
nestedTweets = d3.nest() | |
.key(function (el) {return el.user}) | |
.entries(incData); | |
packableTweets = {id: "root", values: nestedTweets} | |
var depthScale = d3.scale.category10([0,1,2]); | |
treeChart = d3.layout.tree(); | |
treeChart.size([500,500]) | |
.children(function(d) {return d.values}); | |
var linkGenerator = d3.svg.diagonal(); | |
linkGenerator | |
.projection(function (d) {return [d.y, d.x]}) | |
d3.select("svg") | |
.append("g") | |
.attr("class", "treeG") | |
.selectAll("g") | |
.data(treeChart(packableTweets)) | |
.enter() | |
.append("g") | |
.attr("class", "node") | |
.attr("transform", function(d) {return "translate(" +d.y+","+d.x+")"}); | |
d3.selectAll("g.node") | |
.append("circle") | |
.attr("r", 10) | |
.style("fill", function(d) {return depthScale(d.depth)}) | |
.style("stroke", "white") | |
.style("stroke-width", "2px"); | |
d3.selectAll("g.node") | |
.append("text") | |
.text(function(d) {return d.id || d.key || d.content}) | |
d3.select("g.treeG").selectAll("path") | |
.data(treeChart.links(treeChart(packableTweets))) | |
.enter().insert("path","g") | |
.attr("d", linkGenerator) | |
.style("fill", "none") | |
.style("stroke", "black") | |
.style("stroke-width", "2px"); | |
treeZoom = d3.behavior.zoom(); | |
treeZoom.on("zoom", zoomed); | |
d3.select("svg").call(treeZoom) | |
function zoomed() { | |
var zoomTranslate = treeZoom.translate(); | |
d3.select("g.treeG").attr("transform", "translate("+zoomTranslate[0]+","+zoomTranslate[1]+")") | |
} | |
} | |
</script> | |
</footer> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"tweets": [ | |
{"user": "Al", "content": "I really love seafood.", "timestamp": " Mon Dec 23 2013 21:30 GMT-0800 (PST)", "retweets": ["Raj","Pris","Roy"], "favorites": ["Sam"]}, | |
{"user": "Al", "content": "I take that back, this doesn't taste so good.", "timestamp": "Mon Dec 23 2013 21:55 GMT-0800 (PST)", "retweets": ["Roy"], "favorites": []}, | |
{"user": "Al", "content": "From now on, I'm only eating cheese sandwiches.", "timestamp": "Mon Dec 23 2013 22:22 GMT-0800 (PST)", "retweets": [], "favorites": ["Roy","Sam"]}, | |
{"user": "Roy", "content": "Great workout!", "timestamp": " Mon Dec 23 2013 7:20 GMT-0800 (PST)", "retweets": [], "favorites": []}, | |
{"user": "Roy", "content": "Spectacular oatmeal!", "timestamp": " Mon Dec 23 2013 7:23 GMT-0800 (PST)", "retweets": [], "favorites": []}, | |
{"user": "Roy", "content": "Amazing traffic!", "timestamp": " Mon Dec 23 2013 7:47 GMT-0800 (PST)", "retweets": [], "favorites": []}, | |
{"user": "Roy", "content": "Just got a ticket for texting and driving!", "timestamp": " Mon Dec 23 2013 8:05 GMT-0800 (PST)", "retweets": [], "favorites": ["Sam", "Sally", "Pris"]}, | |
{"user": "Pris", "content": "Going to have some boiled eggs.", "timestamp": " Mon Dec 23 2013 18:23 GMT-0800 (PST)", "retweets": [], "favorites": ["Sally"]}, | |
{"user": "Pris", "content": "Maybe practice some gymnastics.", "timestamp": " Mon Dec 23 2013 19:47 GMT-0800 (PST)", "retweets": [], "favorites": ["Sally"]}, | |
{"user": "Sam", "content": "@Roy Let's get lunch", "timestamp": " Mon Dec 23 2013 11:05 GMT-0800 (PST)", "retweets": ["Pris"], "favorites": ["Sally", "Pris"]} | |
] | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment