Running metrics compared to distance ran.
This is an additional view of my running training data. This one allows a user to switch between different metrics. I got all the training run data from my Garmin Fenix 2. This was during my training for the Leadville 100.
xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<div id="dropdown"></div>
<div id="chart"></div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 900 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;
var x = d3.scaleLinear()
.range([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
var color = d3.scaleOrdinal(d3.schemeCategory10);
var xAxis = d3.axisBottom(x);
var yAxis = d3.axisLeft(y);
var svg = d3.select("#chart").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.tsv("running.tsv", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d["Distance"] = +d["Distance"];
d["Elevation Gain"] = +d["Elevation Gain"];
d["Avg HR"] = +d["Avg HR"];
d["Max HR"] = +d["Max HR"];
});
data = data.filter(function(d) { return d["Avg HR"] > 0 });
data = data.filter(function(d) { return d["Max HR"] > 0 });
var options = ["Distance","Elevation Gain","Avg HR","Max HR"];
x.domain(d3.extent(data, function(d) { return d["Distance"]; })).nice();
y.domain(d3.extent(data, function(d) { return d["Elevation Gain"]; })).nice();
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("Distance");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Elevation Gain")
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("class",function(d) { return d["Day of Week"]; })
.attr("r", 5)
// adjusting size
.attr("cx", function(d) { return x(d["Distance"]); })
.attr("cy", function(d) { return y(d["Elevation Gain"]); })
// Adjusting by color
.style("stroke", "#fff")
.style("fill", "0000ff")
.style("fill", function(d) { return color(d["Day of Week"]); });
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color)
// Adding interactivity
.on("mouseenter", function(d) {
d3.selectAll("circle")
.style("fill-opacity", .1);
d3.selectAll("." + d)
.style("fill-opacity", 1);
})
.on("mouseleave", function(d) {
d3.selectAll("circle")
.style("fill-opacity", 1);
})
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
// Add in other features
var options = ["Elevation Gain","Distance","Avg HR","Max HR"];
var dropdown = d3.select("#dropdown")
.append("select")
.on("change", change);
dropdown.selectAll("option")
.data(options)
.enter().append("option")
.text(function(d) { return d; });
function change() {
var column = this.value;
y.domain(d3.extent(data, function(d) { return d[column]; })).nice();
d3.selectAll("circle")
.transition()
.duration(2000)
.attr("cy", function(d) { return y(d[column]); })
d3.select(".y")
.transition()
.call(yAxis)
.select(".label")
.text(column)
}
});
</script>
https://d3js.org/d3.v4.min.js