D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
sarubenfeld
Full window
Github gist
SF Evictions: Condo Conversion
<!DOCTYPE html> <meta charset="utf-8"> <style> h1, h2, h3, h4, h5, h6 { font-family: futura; color: grey; } .bar rect { fill: #00B8A9; } .bar text { fill: #fff; font-family:Futura; font-size: 10px; } .axis text { font-family: Futura } .axis line { stroke: #00B8A9; stroke-dasharray: 2, 2; stroke-linecap: "round"; shape-rendering: crispEdges; } </style> <body> <div class="g-chart-container"> <h2> SF Evictions: Condo Conversion </h2> </div> <script src="//d3js.org/d3.v4.min.js"></script> <script> var parseDate = d3.timeParse("%Y-%m-%d"), formatCount = d3.format(",.0f"); var margin = {top: 10, right: 30, bottom: 30, left: 30}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scaleTime() .domain([new Date(1998, 1, 0), new Date(2016, 1, 0)]) .rangeRound([0, width]); var y = d3.scaleLinear() .range([height, 0]); var histogram = d3.histogram() .value(function(d) { return d.date; }) .domain(x.domain()); 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 + ")"); svg.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); d3.csv("cc_cleaned.csv", type, function(error, data) { if (error) throw error; var bins = histogram(data); y.domain([0, d3.max(bins, function(d) { return d.length; })]); var bar = svg.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", function(d) { return x(d.x1) - x(d.x0) - 1; }) .attr("height", function(d) { return height - y(d.length); }); bar.append("text") .attr("dy", ".75em") .attr("y", 6) .attr("x", function(d) { return (x(d.x1) - x(d.x0)) / 2; }) .attr("text-anchor", "middle") .text(function(d) { return formatCount(d.length); }); }); function type(d) { d.date = parseDate(d.date); return d; } </script>
https://d3js.org/d3.v4.min.js