Skip to content

Instantly share code, notes, and snippets.

@sarubenfeld
Created October 1, 2016 17:28
Show Gist options
  • Save sarubenfeld/e0b1153bf937fe2664f0258b9b4fb2e8 to your computer and use it in GitHub Desktop.
Save sarubenfeld/e0b1153bf937fe2664f0258b9b4fb2e8 to your computer and use it in GitHub Desktop.
citibike: june 2016
date Customer Subscriber
2016-06-01 3984 50681
2016-06-02 4017 50384
2016-06-03 2447 36528
2016-06-04 9341 31636
2016-06-05 2629 19768
2016-06-06 4605 47544
2016-06-07 3224 48588
2016-06-08 1376 40874
2016-06-09 3547 49358
2016-06-10 4865 48434
2016-06-11 8402 32628
2016-06-12 8754 31883
2016-06-13 3525 45544
2016-06-14 3579 51234
2016-06-15 3624 51425
2016-06-16 2614 43584
2016-06-17 6152 48084
2016-06-18 15510 33391
2016-06-19 14090 31180
2016-06-20 4098 48149
2016-06-21 3158 50406
2016-06-22 3320 52429
2016-06-23 2976 50230
2016-06-24 4842 49344
2016-06-25 8776 34224
2016-06-26 9616 33376
2016-06-27 3789 43234
2016-06-28 2637 46956
2016-06-29 3476 51406
2016-06-30 3859 50984
<!DOCTYPE html>
<meta charset="utf-8">
<style>
text {
font-family: futura;
font-size: 9px;
}
h1, h2, h3, h4, h5, h6 {
font-family: futura;
color: grey;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
stroke-dasharray: 1px 1px;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke-width: 1.5px;
}
.label {
text-anchor: middle;
}
.label rect {
fill: white;
}
.label-key {
font-weight: bold;
}
.axis .tick {
stroke: #ccc;
stroke-dasharray: 1px 1px;
stroke-opacity: .5;
}
</style>
<body>
<div class="g-chart-container">
<h2> Citibike: June 2016 </h2>
</div>
</body>
<svg width="1200" height="600"></svg>
<script src="//d3js.org/d3.v4.0.0-alpha.9.min.js"></script>
<script>
var parseTime = d3.timeParse("%Y-%m-%d");
var svg = d3.select("svg");
var margin = {top: 30, right: 50, bottom: 30, left: 30},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
labelPadding = 3;
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.requestTsv("data.tsv", function(d) {
d.date = parseTime(d.date);
for (var k in d) if (k !== "date") d[k] = +d[k];
return d;
}, function(error, data) {
if (error) throw error;
var series = data.columns.slice(1).map(function(key) {
return data.map(function(d) {
return {
key: key,
date: d.date,
value: d[key]
};
});
});
var x = d3.scaleTime()
.domain([data[0].date, data[data.length - 1].date])
.range([0, width]);
var y = d3.scaleLinear()
.domain([0, d3.max(series, function(s) { return d3.max(s, function(d) { return d.value; }); })])
.range([height, 0]);
var z = d3.scaleCategory10();
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
var serie = g.selectAll(".serie")
.data(series)
.enter().append("g")
.attr("class", "serie");
serie.append("path")
.attr("class", "line")
.style("stroke", function(d) { return z(d[0].key); })
.attr("d", d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.value); }));
var label = serie.selectAll(".label")
.data(function(d) { return d; })
.enter().append("g")
.attr("class", "label")
.attr("transform", function(d, i) { return "translate(" + x(d.date) + "," + y(d.value) + ")"; });
label.append("text")
.attr("dy", ".35em")
.text(function(d) { return d.value; })
.filter(function(d, i) { return i === data.length - 1; })
.append("tspan")
.attr("class", "label-key")
.text(function(d) { return " " + d.key; });
label.append("rect", "text")
.datum(function() { return this.nextSibling.getBBox(); })
.attr("x", function(d) { return d.x - labelPadding; })
.attr("y", function(d) { return d.y - labelPadding; })
.attr("width", function(d) { return d.width + 2 * labelPadding; })
.attr("height", function(d) { return d.height + 2 * labelPadding; });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment