block by bricedev c45497805dc9d2e676ab

Number of Public Bicycle Hires

Full Screen

Number of Public Bicycle Hires during tube strike.

index.html

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
  font: 10px sans-serif;
}

.bar {
  fill: #003687;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.x.axis path {
  display: none;
}

</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>

var margin = {top: 20, right: 20, bottom: 40, left: 30},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var parseDate = d3.time.format("%d/%m/%Y").parse,
    nformat = d3.format('s'),
    dateFormat = d3.time.format("%d/%m");

var x = d3.scale.ordinal()
    .rangeRoundBands([0, width],.1);

var y = d3.scale.linear()
    .range([height, 0]);

var xAxis = d3.svg.axis()
    .scale(x)
    .tickFormat(dateFormat)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .tickFormat(nformat)
    .orient("left");

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.csv("data.csv", function(error, data) {
  if (error) throw error;

  data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.cycles = +d.cycles;
  });
  var max = d3.max(data, function(d) { return d.cycles; });
  x.domain(data.map(function(d) { return d.date; }));
  y.domain([0,max]);

  svg.selectAll(".bar")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.date); })
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(d.cycles); })
      .attr("height", function(d) { return height - y(d.cycles); });

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
    .selectAll("text")
      .attr("y", 0)
      .attr("x", 9)
      .attr("dy", ".35em")
      .attr("transform", "rotate(90)")
      .style("text-anchor", "start");

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("font-weight", "bold")
      .style("text-anchor", "end")
      .text("Number of Bicycle Hires");

  var extremum = data.filter( function(d) { return d.cycles===max; })[0];

  var legend = svg.append("g")
      .attr("class", "legend")
      .attr("transform", "translate(" + (x(extremum.date)+ x.rangeBand()/2) + ",70)")
      .style("fill","#e32118");
  
  legend.append("circle")
      .attr("r",4);

  legend.append("line")
      .attr("x1",0)
      .attr("y1",0)
      .attr("x2",70)
      .attr("y2",0)
      .style("stroke","#e32118")
      .style("stroke-width","2px");

  legend.append("text")
      .attr("x",75)
      .attr("y",4)
      .style("font-size","12px")
      .text("09/07 TFL Tube Strike");

});

</script>

data.csv

date,cycles
01/06/2015,27893
02/06/2015,28070
03/06/2015,35630
04/06/2015,39687
05/06/2015,35048
06/06/2015,33088
07/06/2015,35757
08/06/2015,32745
09/06/2015,33447
10/06/2015,35862
11/06/2015,38969
12/06/2015,35367
13/06/2015,30566
14/06/2015,25402
15/06/2015,34723
16/06/2015,38166
17/06/2015,38799
18/06/2015,38875
19/06/2015,36220
20/06/2015,26546
21/06/2015,30621
22/06/2015,29711
23/06/2015,37663
24/06/2015,39183
25/06/2015,38856
26/06/2015,37281
27/06/2015,36885
28/06/2015,25846
29/06/2015,39280
30/06/2015,43582
01/07/2015,42891
02/07/2015,38467
03/07/2015,39940
04/07/2015,38640
05/07/2015,29963
06/07/2015,38895
07/07/2015,38120
08/07/2015,41921
09/07/2015,73094
10/07/2015,43266
11/07/2015,42676
12/07/2015,23498
13/07/2015,27646
14/07/2015,35600
15/07/2015,34878
16/07/2015,39609
17/07/2015,38845
18/07/2015,38906
19/07/2015,36989
20/07/2015,32140
21/07/2015,41001
22/07/2015,38133
23/07/2015,39368
24/07/2015,16135
25/07/2015,32982
26/07/2015,12497
27/07/2015,32103
28/07/2015,36310
29/07/2015,37021
30/07/2015,36350
31/07/2015,36932