D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
rachelwalexander
Full window
Github gist
Revised and prettified bar graph
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Drug sentences in Spokane County</title> <script type="text/javascript" src="https://d3js.org/d3.v3.min.js"></script> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <h2>Spokane county drug sentences</h2> <p>Number of prison sentences for drug-related crimes in Spokane County, Washington</p> <script type="text/javascript"> var w = 500; var h = 300; var padding = [10, 10, 20, 50]; var widthScale = d3.scale.linear() .range([0,w - padding [1] - padding [3]]); var heightScale = d3.scale.ordinal() .rangeRoundBands([padding[0], h-padding[2]], 0.1); var Xaxis = d3.svg.axis() .scale(widthScale) .orient("bottom"); //orientation is about label position, not axis position var Yaxis = d3.svg.axis() .scale(heightScale) .orient ("left"); var svg = d3.select("body").append("svg").attr("width", w).attr("height",h); d3.csv("Spokane_drug_sentences.csv", function(data) { widthScale.domain([0, d3.max(data, function(d) { return +d.drug_prison; })]); heightScale.domain(data.map(function(d) {return d.year;} )); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", padding[3]) .attr("y", function (d) { return heightScale(d.year); }) .attr("width", function(d){ return widthScale(d.drug_prison); }) .attr("height", heightScale.rangeBand()) .attr("fill", "#000000") .text(function(d) { return "In " + d.year + ", " + d.drug_prison + " people in Spokane County received prison sentences for drug crimes."; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") //this positions axis visually, would be at 0,0 otherwise .call(Xaxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3] - 10) + ",0)") //.attr("transform", "translate(" + padding[3] + ",0)") //this positions axis visually, would be at 0,0 otherwise. translation is relative to the graph itself, I think, not the svg, because a 0 value still gets you some top padding .call(Yaxis); }); </script> <p id="footer">Data from the <a href="https://wa-state-ofm.us/CrimeStatsOnline/">Washington Statistical Analysis Center</a></p> </body> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js