Skip to content

Instantly share code, notes, and snippets.

@micahstubbs
Last active March 12, 2016 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micahstubbs/edbf5a68596c15b0b577 to your computer and use it in GitHub Desktop.
Save micahstubbs/edbf5a68596c15b0b577 to your computer and use it in GitHub Desktop.
packed circles - color scale domain example
license: CC0-1.0
<html>
<head>
<title>D3 in Action Chapter 5 - Packed Circles Example</title>
<meta charset="utf-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" type="text/JavaScript"></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])
// not necessary to set the domain explicity
var depthScale = d3.scale.category10();
exposedData = incData;
packChart = d3.layout.pack();
packChart.size([500,500])
.children(function(d){
return d.values
})
.value(function(d){
return 1
})
d3.select("svg")
.append("g")
.attr("transform", "translate(0,0)")
.selectAll("circle")
.data(packChart(packableTweets))
.enter()
.append("circle")
.attr("r", function(d) {
return d.r
})
.attr("cx", function(d){
return d.x
})
.attr("cy", function(d){
return d.y
})
.style("fill", function(d){
return depthScale(d.depth)
})
.style("stroke", "black")
.style("stroke", "2px")
// we can inspect the domain that is
// built up by applying depthScale() to each
// instance of the current object d
console.log(depthScale.domain())
}
</script>
</footer>
</html>
{
"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