block by pstuffa 7bc57cc0dde8ad77ee90effbb03f2b2a

Running IV

Full Screen

Heart Rate levels normalized for each training run from January 2015 - August 2015.

I got all the training run data from my Garmin Fenix 2. This was during my training for the Leadville 100.

I am reloading this, as there’s been an issue with my bl.ocks site

index.html

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

svg {
  font: 12px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
  stroke-opacity: .3;
  stroke-dasharray: 3;
  stroke-width: .5;
}

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

.line {
  stroke: url(#heartRate-gradient);
  fill: none;
  stroke-width: 7px;
}

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

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

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)
    .ticks(1)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .ticks(20)
    .tickSize(-width, 0, 0)
    .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)']) };

});

  console.log("pre-nested", data)

  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("post-nested", data)

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

  svg.append("line")
    .attr("class", "xAxisLine")
    .attr("x1", 0)
    .attr("x2", width)
    .attr("y1", 0)
    .attr("y2", 0)
    .attr("transform", "translate(0," + height + ")")
    .style("stroke-width", 2)
    .style("stroke-opacity", .5)
    .style("stroke", "#000");

  svg.append("g")
    .attr("class", "y axis")
    .call(yAxis)
    .selectAll("text")
    .style("opacity", .5);

  svg.append("line")
    .attr("class", "xAxisLine")
    .attr("x1", 0)
    .attr("x2", 0)
    .attr("y1", 0)
    .attr("y2", height - 2)
    .style("stroke-width", 2)
    .style("stroke", "#fff");

  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-opacity", .75)
    .style("stroke-width", 1);

  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-opacity", .75)
    .style("stroke-width", 1);

  svg.append("rect")
    .attr("x",0)
    .attr("y",-10)
    .attr("width",100)
    .attr("height",30)
    .style("fill","#fff");

  svg.append("text")
    .attr("x",2)
    .attr("y",4)
    .style("background-color","white")
    .style("opacity", .80)
    .text("Heart Rate (BPM)")

  svg.append("text")
    .attr("x",2)
    .attr("y",4)
    .style("background-color","white")
    .style("opacity", .80)
    .text("Heart Rate (BPM)")

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

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

  svg.append("linearGradient")
      .attr("id", "heartRate-gradient")
      .attr("gradientUnits", "userSpaceOnUse")
      .attr("x1", 0).attr("y1", y(100))
      .attr("x2", 0).attr("y2", y(200))
    .selectAll("stop")
      .data([
        {offset: "0%", color: "black"},
        {offset: "30%", color: "black"},
        {offset: "30%", color: "#4fb13c"},
        {offset: "60%", color: "#4fb13c"},
        {offset: "60%", color: "red"},
        {offset: "100%", color: "red"}
      ])
    .enter().append("stop")
      .attr("offset", function(d) { return d.offset; })
      .attr("stop-color", function(d) { return d.color; });

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

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


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

    d3.selectAll(".line")
      .transition()
      .ease("linear")
      .delay(function(d,i) {return i*200;})
      .duration(100)
      .style("stroke-opacity",1)
      .transition()
      .duration(100)
      // .style("stroke","#000")
      .style("stroke-opacity",.5)
      .style("stroke-width",1.5)
      .transition()
      .duration(1000)
      .style("stroke-opacity",0)
      .transition()
      .duration(1)
      .style("stroke", "url(#heartRate-gradient);")
      .style("stroke-width",7);

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

  })

});

</script>