D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ohdebby
Full window
Github gist
Mortality 1933-2014
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style type="text/css"> body { font-family: sans-serif; } text { font-size: 12px; fill: #888; } path, line { stroke: #888; stroke-width: 2; fill: none; } </style> <p>All-cause mortality, ages 50-54</p> <svg width="700" height="500"></svg> <script src="//d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), margin = {top: 20, right: 80, bottom: 30, left: 50}, width = svg.attr("width") - margin.left - margin.right, height = svg.attr("height") - margin.top - margin.bottom, g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var parseTime = d3.timeParse("%Y"); var x = d3.scaleTime().range([0, width]), y = d3.scaleLinear().range([height, 0]), z = d3.scaleOrdinal(d3.schemeCategory10); var line = d3.line() .defined(function (d) { return d[1] !==NaN; }) // .curve(d3.curveBasis) .x(function(d) { return x(d.year); }) .y(function(d) { return y(d.mortality); }); d3.csv("mortality1933-2014.csv", type, function(error, data) { if (error) throw error; var country = data.columns.slice(1).map(function(id) { return { id: id, values: data.map(function(d) { return {year: d.year, mortality: d[id]}; }) }; }); x.domain(d3.extent(data, function(d) { return d.year; })); y.domain([0, d3.max(country, function(c) { return d3.max(c.values, function(d) { return d.mortality; }); }) ]); z.domain(country.map(function(c) { return c.id; })); g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); g.append("g") .attr("class", "axis axis--y") .call(d3.axisLeft(y)) .append("text") .attr("transform", "rotate(-90)") .attr("y", -42) .attr("x", -150) .text("deaths per 100,000"); var group = g.selectAll(".group") .data(country) .enter().append("g") .attr("class", "group"); group.append("path") .attr("class", "line") .attr("d", function(d) { return line(d.values); }) .style("stroke", function(d) { return z(d.id); }); group.append("text") .datum(function(d) { return {id: d.id, value: d.values[d.values.length - 1]}; }) .attr("transform", function(d) { return "translate(" + x(d.value.year) + "," + y(d.value.mortality) + ")"; }) .attr("x", 3) .attr("dy", "0.35em") .style("font", "10px sans-serif") .text(function(d) { return d.id; }); }); function type(d, _, columns) { d.year = parseTime(d.year); for (var i = 1, n = columns.length, c; i < n; ++i) d[c = columns[i]] = +d[c]; return d; } </script>
https://d3js.org/d3.v4.min.js