Skip to content

Instantly share code, notes, and snippets.

@scotthmurray
Last active September 13, 2015 18:40
Show Gist options
  • Save scotthmurray/36398a0750d30c43cba9 to your computer and use it in GitHub Desktop.
Save scotthmurray/36398a0750d30c43cba9 to your computer and use it in GitHub Desktop.
Taipei Youbike Rental Times since 2012
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Youbike Bar Chart</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
margin: 40px 0px 0px 40px ;
}
h1 {
font-size: 24px;
margin:0;
font-family: sans-serif;
}
p {
font-size: 16px;
margin: 10px 0 0 0;
font-family: sans-serif;
}
svg {
background-color: white;
}
.bar {
cursor: pointer;
}
.bar text {
font-family: Helvetica, sans-serif;
font-size: 11px;
opacity: 0;
}
.bar:hover text {
opacity: 1;
}
.bar:hover rect {
fill: orange;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
.y.axis path,
.y.axis line {
opacity: 0;
}
</style>
</head>
<body>
<h1>Taipei Youbike Usage</h1>
<p>Monthly Rental Times Since 2012. Source <a href="http://data.taipei.gov.tw/"> Data Taipei</a>.</p>
<script type="text/javascript">
var w = 800;
var h = 600;
var padding = [20, 30, 50, 90];
var widthscale = d3.scale.linear()
.range([0, w - padding[1] - padding[3]]);
var heightscale = d3.scale.ordinal()
.rangeRoundBands([ padding[0], h - padding[2]],0.1);
var xAxis = d3.svg.axis()
.scale(widthscale)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(heightscale)
.orient("left");
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
d3.csv("youbike.csv",function(data){
widthscale.domain([0, d3.max(data, function(d){
return +d.monthlyTotalRentalTimes;
}) ]);
heightscale.domain(data.map(function(d){return d.yearMonth;}));
var groups = svg.selectAll("g.bar")
.data(data)
.enter()
.append("g")
.attr("class", "bar");
groups.append("rect")
.attr("x", padding[3])
.attr("y", function(d,i){
return heightscale(d.yearMonth);
})
.attr("width", function (d){
return widthscale(d.monthlyTotalRentalTimes);
})
.attr("height", heightscale.rangeBand())
.attr("fill","#5292f9");
groups.append("text")
.attr("x", function(d) {
return padding[3] + widthscale(d.monthlyTotalRentalTimes);
})
.attr("y", function(d) {
return heightscale(d.yearMonth) + 10;
})
.text(function(d) {
return d.monthlyTotalRentalTimes;
});
// .append("svg:title")
// .text(function(d) {
// return d.monthlyTotalRentalTimes;
// });
svg.append("g")
.attr("class","x axis")
.attr("transform","translate(" + padding[3] + "," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class","y axis")
.attr("transform","translate(" + (padding[3] -5) + ",0)")
.call(yAxis);
})
</script>
</body>
</html>
yearMonth rentalStationNumbers totalBikeNumbers monthlyTotalRentalTimes
2012-01 11 500 3333
2012-02 11 500 3638
2012-03 11 500 6177
2012-04 11 500 5178
2012-05 11 500 5396
2012-06 11 500 5178
2012-07 11 500 5235
2012-08 41 1460 6812
2012-09 41 1460 135554
2012-10 41 1460 276284
2012-11 41 1460 262675
2012-12 48 1684 283055
2013-01 53 1844 340201
2013-02 58 2004 428817
2013-03 62 2132 628825
2013-04 64 2196 587442
2013-05 68 2324 791293
2013-06 70 2388 888930
2013-07 80 2580 957712
2013-08 103 3456 1123423
2013-09 108 3621 1255377
2013-10 120 4029 1371336
2013-11 124 4165 1441001
2013-12 136 4545 1170206
2014-01 158 5205 1533879
2014-02 158 5205 1248281
2014-03 159 5235 1767106
2014-04 159 5235 1938518
2014-05 160 5265 1811150
2014-06 160 5265 1839191
2014-07 163 5350 2011787
2014-08 165 5416 2184482
2014-09 167 5482 1894608
2014-10 167 5482 2313976
2014-11 180 5911 2129694
2014-12 196 6406 1983106
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment