This example uses D3's d3.svg.axis component to render a Twitter Follower timeline.
Created
May 24, 2012 03:21
-
-
Save reinh/2779242 to your computer and use it in GitHub Desktop.
Twitter follower timeline with Axis Component
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.0.0"></script> | |
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.csv.js?2.0.0"></script> | |
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.time.js?2.0.0"></script> | |
<style type="text/css"> | |
body { | |
font: 10px sans-serif; | |
margin: 0; | |
} | |
path.line { | |
fill: none; | |
stroke: #666; | |
stroke-width: 1.5px; | |
} | |
path.area { | |
fill: #e7e7e7; | |
} | |
.axis { | |
shape-rendering: crispEdges; | |
} | |
.x.axis line { | |
stroke: #fff; | |
} | |
.x.axis .minor { | |
stroke-opacity: .5; | |
} | |
.x.axis path { | |
display: none; | |
} | |
.y.axis line, .y.axis path { | |
fill: none; | |
stroke: #000; | |
} | |
</style> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
var m = [80, 80, 80, 80], | |
w = 960 - m[1] - m[3], | |
h = 500 - m[0] - m[2], | |
parse = d3.time.format("%b %Y").parse; | |
// Scales and axes. Note the inverted domain for the y-scale: bigger is up! | |
var x = d3.time.scale().range([0, w]), | |
y = d3.scale.linear().range([h, 0]), | |
xAxis = d3.svg.axis().scale(x).tickSize(-h).tickSubdivide(true), | |
yAxis = d3.svg.axis().scale(y).ticks(4).orient("right"); | |
// An area generator, for the light fill. | |
var area = d3.svg.area() | |
.interpolate("monotone") | |
.x(function(d) { return x(d.date); }) | |
.y0(h) | |
.y1(function(d) { return y(d.price); }); | |
// A line generator, for the dark stroke. | |
var line = d3.svg.line() | |
.interpolate("monotone") | |
.x(function(d) { return x(d.date); }) | |
.y(function(d) { return y(d.price); }); | |
d3.csv("readme.csv", function(data) { | |
// Filter to one symbol; the S&P 500. | |
var values = data.filter(function(d) { | |
return d.symbol == "S&P 500"; | |
}); | |
// Parse dates and numbers. We assume values are sorted by date. | |
values.forEach(function(d) { | |
d.date = parse(d.date); | |
d.price = +d.price; | |
}); | |
// Compute the minimum and maximum date, and the maximum price. | |
x.domain([values[0].date, values[values.length - 1].date]); | |
y.domain([0, d3.max(values, function(d) { return d.price; })]).nice(); | |
// Add an SVG element with the desired dimensions and margin. | |
var svg = d3.select("body").append("svg:svg") | |
.attr("width", w + m[1] + m[3]) | |
.attr("height", h + m[0] + m[2]) | |
.append("svg:g") | |
.attr("transform", "translate(" + m[3] + "," + m[0] + ")"); | |
// Add the clip path. | |
svg.append("svg:clipPath") | |
.attr("id", "clip") | |
.append("svg:rect") | |
.attr("width", w) | |
.attr("height", h); | |
// Add the area path. | |
svg.append("svg:path") | |
.attr("class", "area") | |
.attr("clip-path", "url(#clip)") | |
.attr("d", area(values)); | |
// Add the x-axis. | |
svg.append("svg:g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + h + ")") | |
.call(xAxis); | |
// Add the y-axis. | |
svg.append("svg:g") | |
.attr("class", "y axis") | |
.attr("transform", "translate(" + w + ",0)") | |
.call(yAxis); | |
// Add the line path. | |
svg.append("svg:path") | |
.attr("class", "line") | |
.attr("clip-path", "url(#clip)") | |
.attr("d", line(values)); | |
// Add a small label for the symbol name. | |
svg.append("svg:text") | |
.attr("x", w - 6) | |
.attr("y", h - 6) | |
.attr("text-anchor", "end") | |
.text(values[0].symbol); | |
}); | |
</script> | |
</body> | |
</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
{"version":"1.2", | |
"username":"TheCounter", | |
"url":"http://twittercounter.com/", | |
"avatar":"http://a0.twimg.com/profile_images/481277925/twc_logo_normal.png", | |
"followers_current":13430, | |
"date_updated":"2012-05-23", | |
"follow_days":"1336", | |
"started_followers":693, | |
"growth_since":12737, | |
"average_growth":"10", | |
"tomorrow":"13440", | |
"next_month":"13730", | |
"followers_yesterday":13418, | |
"rank":"31899", | |
"followers_2w_ago":13349, | |
"growth_since_2w":81, | |
"average_growth_2w":"6", | |
"tomorrow_2w":"13436", | |
"next_month_2w":"13610", | |
"followersperdate": | |
{"date2012-05-23":13430, | |
"date2012-05-22":13418, | |
"date2012-05-21":13417, | |
"date2012-05-20":13412, | |
"date2012-05-19":13401, | |
"date2012-05-18":13400, | |
"date2012-05-17":13400, | |
"date2012-05-16":13390, | |
"date2012-05-15":13378, | |
"date2012-05-14":13383, | |
"date2012-05-13":13369, | |
"date2012-05-12":13360, | |
"date2012-05-11":13360, | |
"date2012-05-10":13356}, | |
"last_update":1337829302} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment