Skip to content

Instantly share code, notes, and snippets.

@emeeks
Last active March 18, 2016 15:12
Show Gist options
  • Save emeeks/7ccc82e776b6e761a3f2 to your computer and use it in GitHub Desktop.
Save emeeks/7ccc82e776b6e761a3f2 to your computer and use it in GitHub Desktop.
Ch. 2, Fig. 22 - D3.js in Action

This is the code for Chapter 2, Figure 22 from D3.js in Action that nests data using d3.nest() and then represents the nested data as a bar chart by first computing the length of the nested data arrays and using that length to size the bars by.

<html>
<head>
<title>D3 in Action Chapter 2 - Example 10</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>
<svg>
</svg>
</div>
</body>
<footer>
<script>
d3.json("tweets.json",function(error,data) {dataViz(data.tweets)});
function dataViz(incomingData) {
var nestedTweets = d3.nest()
.key(function (el) {return el.user})
.entries(incomingData);
nestedTweets.forEach(function (el) {
el.numTweets = el.values.length
})
var maxTweets = d3.max(nestedTweets, function(el) {return el.numTweets});
var yScale = d3.scale.linear().domain([0,maxTweets]).range([0,100]);
d3.select("svg")
.selectAll("rect")
.data(nestedTweets)
.enter()
.append("rect")
.attr("width", 50)
.attr("height", function(d) {return yScale(d.numTweets)})
.attr("x", function(d,i) {return i * 60})
.attr("y", function(d) {return 100 - yScale(d.numTweets)})
.style("fill", "blue")
.style("stroke", "red")
.style("stroke-width", "1px").style("opacity", .25)
}
</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