|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<meta charset="utf-8"> |
|
<title>Dancing percentage streamgraph</title> |
|
<style type="text/css"> |
|
text { |
|
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; |
|
} |
|
|
|
.title { |
|
font-size: 20px; |
|
} |
|
|
|
.legend-row { |
|
font-size: 18px; |
|
} |
|
|
|
.axis { |
|
font-size: 12px; |
|
} |
|
|
|
.axis path, |
|
.axis line { |
|
fill: none; |
|
stroke: #000; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.line { |
|
fill: none; |
|
stroke: #AAA; |
|
stroke-width: 1px; |
|
stroke-dasharray: 5,2; |
|
stroke-linejoin: round; |
|
stroke-linecap: round; |
|
} |
|
</style> |
|
</head> |
|
<body> |
|
<script src="http://d3js.org/d3.v3.min.js"></script> |
|
<script> |
|
var margin = {top: 30, right: 30, bottom: 40, left: 50}, |
|
width = 960 - margin.left - margin.right, |
|
height = 500 - margin.top - margin.bottom, |
|
legendWidth = 150, |
|
legendHeight = 200, |
|
OTHER = "Other"; |
|
|
|
d3.csv("statistics.csv", ready) |
|
.row(function(d) { return {year: +d.Year, nationality: d.Nationality, players: +d.Players}; }); |
|
|
|
function ready(error, data) { |
|
if (error) throw error; |
|
|
|
// Group data by years. |
|
var dataByYears = d3.map(); |
|
data.forEach(function(d) { |
|
if (!dataByYears.has(d.year)) { |
|
dataByYears.set(d.year, { |
|
countries: d3.map(), |
|
totalPlayers: 0 |
|
}); |
|
} |
|
if (dataByYears.get(d.year).countries.has(d.nationality)) { |
|
throw new Error("Data error. Year " + d.year + " has 2 records for country " + d.nationality); |
|
} |
|
dataByYears.get(d.year).countries.set(d.nationality, { count: d.players }); // Later will store percentage. |
|
dataByYears.get(d.year).totalPlayers += d.players; |
|
}); |
|
|
|
// Calculate percentage for each year/nationality. |
|
dataByYears.forEach(function(year, yearData) { |
|
yearData.countries.forEach(function(country, countryRecord) { |
|
countryRecord.percentage = countryRecord.count / yearData.totalPlayers; |
|
}); |
|
}); |
|
|
|
// Find max percentage for each encountered nationality. |
|
var maxNationalityPercentage = d3.map(); |
|
dataByYears.forEach(function(year, yearData) { |
|
yearData.countries.forEach(function(country, countryRecord) { |
|
var percentage = countryRecord.percentage; |
|
if (!maxNationalityPercentage.has(country) |
|
|| (percentage > maxNationalityPercentage.get(country).value)) { |
|
maxNationalityPercentage.set(country, { value: percentage, year: year }); |
|
} |
|
}); |
|
}); |
|
|
|
// Convert the dictionary into array to display in table and to arrange countries. |
|
var maxNationalityPercentageArray = []; |
|
maxNationalityPercentage.forEach(function(country, record) { |
|
maxNationalityPercentageArray.push({ |
|
country: country, |
|
value: record.value, |
|
year: record.year |
|
}); |
|
}); |
|
maxNationalityPercentageArray.sort(function(country1, country2) { |
|
return country2.value - country1.value; |
|
}); |
|
|
|
// For investigating. |
|
//showCountryMaxPercentageParticipation(maxNationalityPercentageArray); |
|
|
|
// Select what countries to show separately, what to group into Other. |
|
var countriesToShow = []; |
|
maxNationalityPercentageArray.forEach(function(record) { |
|
if (record.value >= 0.05) { |
|
countriesToShow.push(record.country); |
|
} |
|
}); |
|
var actualCountriesSet = d3.set(countriesToShow); |
|
countriesToShow.push(OTHER); |
|
|
|
// Get all years because cannot use years range because of skipped 2004-2005 season |
|
// https://en.wikipedia.org/wiki/2004%E2%80%9305_NHL_season |
|
var years = dataByYears.keys(); |
|
years.sort(); |
|
|
|
var layersData = d3.map(); |
|
countriesToShow.forEach(function(country) { |
|
layersData.set(country, { |
|
key: country, |
|
values: [] |
|
}); |
|
}); |
|
// Populate each layer for each year. |
|
years.forEach(function(year) { |
|
var yearData = dataByYears.get(year); |
|
var layerYearData = d3.map(); |
|
// Init year data. |
|
countriesToShow.forEach(function(country) { |
|
layerYearData.set(country, { year: year, value: 0.0 }); |
|
}); |
|
// Put data in appropriate layers. |
|
yearData.countries.forEach(function(country, countryRecord) { |
|
var layerToIncrement = actualCountriesSet.has(country) ? country : OTHER; |
|
layerYearData.get(layerToIncrement).value += countryRecord.percentage; |
|
}); |
|
// Collect year data into layersData. |
|
layerYearData.forEach(function(country, countryLayerData) { |
|
layersData.get(country).values.push(countryLayerData); |
|
}); |
|
}); |
|
|
|
// Convert layer data into array in correct order. |
|
var graphData = []; |
|
countriesToShow.forEach(function(country) { |
|
graphData.push(layersData.get(country)); |
|
}); |
|
|
|
var colorScale = d3.scale.category10(); |
|
var stack = d3.layout.stack() |
|
.values(function(d) { return d.values; }) |
|
.x(function(d) { return d.year; }) |
|
.y(function(d) { return d.value; }) |
|
.offset(function(data, baselineLayer) { |
|
var j = -1, |
|
i = 0, |
|
m = data[0].length, |
|
y0 = []; |
|
while (++j < m) { |
|
var offset = 0; |
|
for (i = 0; i < baselineLayer; i++) { |
|
offset += data[i][j][1]; |
|
} |
|
y0[j] = -offset; |
|
} |
|
return y0; |
|
}); |
|
var stackedData = stack(graphData, 0); |
|
var xScale = d3.scale.ordinal() |
|
.domain(years) |
|
.rangePoints([0, width - legendWidth - 20], 0); |
|
var yScale = d3.scale.linear() |
|
.domain([0.0, 1.0]) |
|
.range([height/2, 0]); // Make the range so that smallest values are at the bottom. |
|
// Make yAxisScale twice as bigger to cover negative values. |
|
var yAxisScale = d3.scale.linear() |
|
.domain([-1.0, 1.0]) |
|
.range([height, 0]); |
|
var area = d3.svg.area() |
|
.x(function(d) { return xScale(d.year); }) |
|
.y0(function(d) { return yScale(d.y0); }) |
|
.y1(function(d) { return yScale(d.y0 + d.value); }); |
|
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 + ")"); |
|
var graphGroup = svg.append("g"); |
|
var updateGraph = function(baselineLayer, suppressAnimation) { |
|
var graphPaths = graphGroup.selectAll("path") |
|
.data(stack(graphData, baselineLayer)); |
|
graphPaths.enter().append("path"); |
|
graphPaths |
|
.on("click", function(d, i) { |
|
updateGraph(i); |
|
}); |
|
graphPaths |
|
.transition().duration(suppressAnimation ? 0 : 750) |
|
.attr("d", function(d) { return area(d.values); }) |
|
.style("fill", function(d, i) { return colorScale(i); }); |
|
}; |
|
updateGraph(0, /*suppressAnimation =*/true); |
|
|
|
// Axes. |
|
var xAxis = d3.svg.axis() |
|
.scale(xScale) |
|
.orient("bottom") |
|
.tickValues(yearTickValues(years)) |
|
svg.append("g") |
|
.attr("class", "axis x-axis") |
|
.attr("transform", "translate(0, " + height/2 + ")") |
|
.call(xAxis); |
|
var yAxis = d3.svg.axis() |
|
.scale(yAxisScale) |
|
.orient("left") |
|
.tickFormat(d3.format(".0%")); |
|
svg.append("g") |
|
.attr("class", "axis y-axis") |
|
.call(yAxis); |
|
// Grid lines. |
|
var yGridValues = yAxisScale.ticks(yAxis.ticks()); |
|
yGridValues = yGridValues.filter(function(d) { return (0 < d) && (d < 1.0); }); |
|
var gridData = []; |
|
var xRange = xScale.range(); |
|
xRangeMin = xRange[0], |
|
xRangeMax = xRange[xRange.length - 1]; |
|
yGridValues.forEach(function(yGridTick) { |
|
gridData.push([ |
|
[xRangeMin, yAxisScale(yGridTick)], |
|
[xRangeMax, yAxisScale(yGridTick)] |
|
]); |
|
}); |
|
svg.append("g").selectAll("path") |
|
.data(gridData) |
|
.enter().append("path") |
|
.attr("class", "line") |
|
.attr("d", d3.svg.line()); |
|
|
|
// Legend. |
|
var legendYScale = d3.scale.linear() |
|
.domain([0, countriesToShow.length]) |
|
.range([0, legendHeight]); |
|
var legendGroup = svg.append("g") |
|
.attr("transform", "translate(" + (width - legendWidth) + ", 0)"); |
|
var legendRows = legendGroup.selectAll(".legend-row") |
|
.data(countriesToShow) |
|
.enter().append("g") |
|
.attr("class", "legend-row") |
|
.attr("transform", function(d, i) { return "translate(0, " + legendYScale(i) + ")"; }); |
|
legendRows.append("rect") |
|
.attr({x: 0, y: 0, width: 20, height: 20}) |
|
.style("fill", function(d, i) { return colorScale(i); }); |
|
legendRows.append("text") |
|
.attr({x: 30, y: 17}) |
|
.text(function(d) { return d; }); |
|
legendRows.selectAll("rect") |
|
.on("click", function(d, rectIndexInRow, rowIndex) { |
|
updateGraph(rowIndex); |
|
}); |
|
|
|
// Title. |
|
svg.append("text") |
|
.attr("x", (xRangeMax + xRangeMin) / 2) |
|
.attr("y", -margin.top / 2) |
|
.attr("text-anchor", "middle") |
|
.attr("class", "title") |
|
.text("NHL Players by Nationality"); |
|
} |
|
|
|
// Helper function to investigate countries below which threshold should be put in Other category. |
|
function showCountryMaxPercentageParticipation(maxNationalityPercentageArray) { |
|
var table = d3.select("body").append("table"); |
|
var rows = table.selectAll("tr") |
|
.data(maxNationalityPercentageArray) |
|
.enter() |
|
.append("tr"); |
|
rows.append("td").html(function(d) { return d.country; }); |
|
rows.append("td").html(function(d) { return d.value; }); |
|
rows.append("td").html(function(d) { return d.year; }); |
|
} |
|
|
|
function yearTickValues(years) { |
|
var result = [years[0]]; |
|
years.forEach(function(year) { |
|
if (year % 10 == 0) { |
|
result.push(year); |
|
} |
|
}); |
|
result.push(years[years.length - 1]); |
|
return result; |
|
} |
|
</script> |
|
</body> |
|
</html> |