block by pstuffa a9d47f6d79fb0b197774

Running V

Full Screen

index.html

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

svg {
  font: 10px sans-serif;
}

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

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

.line {
  fill: none;
  stroke-width: 8px;
}

</style>
<body>
<script src="//d3js.org/d3.v3.js"></script>
<button id='button'>hit me</button>
<script>


var margin = {top: 20, right: 80, bottom: 30, left: 50},
    width = 900 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var parseDate = d3.time.format("%Y-%m-%d").parse;

var x = d3.scale.linear()
    .range([width,0])
    .domain([0,100]);

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

var color = d3.scale.category10();

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

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

var line = d3.svg.line()
    .interpolate("basis")
    .x(function(d) { return x(d['percentRemaining']); })
    .y(function(d) { return y(d['heartRate']); });

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("merged.csv", function(error, data) {

   data = data.filter(function(d) { return d.lookup != "lookup"; });

  data = data.map( function (d) {
    return {
      lookup: d['lookup'],
      percentRemaining: +d['percentRemaining']*100,
      heartRate: Math.round(d['heart_rate(bpm)']) };
});

  data = d3.nest()
  .key(function(d) { return d.lookup; })
  .sortKeys(d3.ascending)
  .sortValues(function(a,b) { return b.percentRemaining - a.percentRemaining; } )
  .entries(data);

console.log(data)

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis);

  svg.append("text")
    .attr("x",0)
    .attr("y",-5)
    .attr("transform","rotate(90)")
    .text("Heart Rate (BPM)")

  svg.append("path")
      .attr("d", function(d) { return "M 0," + y(160) + ", L " + width + "," + y(160); })
      .attr("clip-path", "url(#rect-clip)")
      .style("stroke", "#000")
      .style("fill","none")
      .style("stroke-width", 2);

  svg.append("path")
      .attr("d", function(d) { return "M 0," + y(130) + ", L " + width + "," + y(130); })
      .attr("clip-path", "url(#rect-clip)")
      .style("stroke", "#000")
      .style("fill","none")
      .style("stroke-width", 2);

  var runs = svg.selectAll(".run")
      .data(data, function(d) { return d.key; })
    .enter().append("g")
      .attr("id", function(d) { return d.key; })
      .attr("class", "city");

  // define the clipPath
  svg.append("clipPath")
      .attr("id", "rect-clip")
    .append("rect")
      .attr("width", width)
      .attr("height", height);

  runs.append("path")
      .attr("class", "line")
      .attr("d", function(d) { return line(d.values); })
      .attr("clip-path", "url(#rect-clip)")
      .style("stroke", "#000")
      .style("stroke-opacity", 0);

  runs.append("text")
    .attr("class","linetext")
    .text(function(d) { return d.key })
    .attr("x", width - 130)
    .attr("y", 20)
    .style("fill", "none")
    .style("font", "25px sans-serif");


  d3.selectAll("#button").on("click", function(){

    // d3.selectAll(".line")
  d3.selectAll(".line")
    .transition()
    .ease("linear")
    .delay(function(d,i) {return i*100;})
    .duration(50)
    .style("stroke-opacity",.1)
    .style("stroke-width",2);

    d3.selectAll(".linetext")
      .transition()
      .ease("linear")
      .delay(function(d,i) {return i*100;})
      .duration(90)
      .style("fill","#000")
      .transition()
      .duration(1)
      .remove();

  })

});

</script>