D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
dmvpro
Full window
Github gist
D3 Titanic plot age/gender
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> .bar rect { fill: steelblue; } .bar text { fill: #fff; font: 10px sans-serif; } </style> <svg width="960" height="250" id="svg1"></svg> <svg width="960" height="250" id="svg2"></svg> <script src="https://d3js.org/d3.v4.min.js"></script> <script> d3.csv('titanic.csv', function(error, data) { data.forEach(function(d) { d.Age = +d.Age; }); ageAll = data.filter(function(d) { return (d.Age !== 0) & (d.Sex == 'female'); }) .map(function(d) { return d.Age; }); var data = ageAll; var chartWidth = 600; var formatCount = d3.format(",.0f"); var svg = d3.select("#svg1"), margin = {top: 10, right: 30, bottom: 30, left: 30}, 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 x = d3.scaleLinear() .domain([0, 100]) .rangeRound([0, chartWidth]); var bins = d3.histogram() .domain(x.domain()) .thresholds(x.ticks(20)) (data); var y = d3.scaleLinear() .domain([0, d3.max(bins, function(d) { return d.length; })]) .range([height, 0]); var bar = g.selectAll(".bar") .data(bins) .enter().append("g") .attr("class", "bar") .attr("transform", function(d) { return "translate(" + x(d.x0) + "," + y(d.length) + ")"; }); bar.append("rect") .attr("x", 1) .attr("width", x(bins[0].x1) - x(bins[0].x0) - 1) .attr("height", function(d) { return height - y(d.length); }); bar.append("text") .attr("dy", ".75em") .attr("y", 6) .attr("x", (x(bins[0].x1) - x(bins[0].x0)) / 2) .attr("text-anchor", "middle") .text(function(d) { return formatCount(d.length); }); g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); }); d3.csv('titanic.csv', function(error, data) { data.forEach(function(d) { d.Age = +d.Age; }); ageAll = data.filter(function(d) { return (d.Age !== 0) & (d.Sex == 'male'); }) .map(function(d) { return d.Age; }); var data = ageAll; var chartWidth = 600; var formatCount = d3.format(",.0f"); var svg = d3.select("#svg2"), margin = {top: 10, right: 30, bottom: 30, left: 30}, 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 x = d3.scaleLinear() .domain([0, 100]) .rangeRound([0, chartWidth]); var bins = d3.histogram() .domain(x.domain()) .thresholds(x.ticks(20)) (data); var y = d3.scaleLinear() .domain([0, d3.max(bins, function(d) { return d.length; })]) .range([height, 0]); var bar = g.selectAll(".bar") .data(bins) .enter().append("g") .attr("class", "bar") .attr("transform", function(d) { return "translate(" + x(d.x0) + "," + y(d.length) + ")"; }); bar.append("rect") .attr("x", 1) .attr("width", x(bins[0].x1) - x(bins[0].x0) - 1) .attr("height", function(d) { return height - y(d.length); }); bar.append("text") .attr("dy", ".75em") .attr("y", 6) .attr("x", (x(bins[0].x1) - x(bins[0].x0)) / 2) .attr("text-anchor", "middle") .text(function(d) { return formatCount(d.length); }); g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); }); </script>
https://d3js.org/d3.v4.min.js