D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
rachelwalexander
Full window
Github gist
Scatterplot of drug prison sentences vs. all prison sentences
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Drug prison 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>Prison sentences for drug use, Spokane County</h2> <p>The number of prison sentences for drug-related crimes plotted against total prison sentences in Spokane County, Washington. Each dot represents one year, 1990-2013.</p> <script type="text/javascript"> var w = 500; var h = 300; var padding = [10, 10, 20, 50]; var xScale = d3.scale.linear() .range([padding[3], w - padding [1] - padding [3] ]); var yScale = d3.scale.linear() .range([padding[0], h-padding[2]]); var Xaxis = d3.svg.axis() .scale(xScale) .orient("bottom") //orientation is about label position, not axis position .ticks(5); var Yaxis = d3.svg.axis() .scale(yScale) .orient ("left"); var svg = d3.select("body").append("svg").attr("width", w).attr("height",h); d3.csv("Spokane_drug_sentences.csv", function(data) { xScale.domain([0, d3.max(data, function(d) { return +d.drug_prison; })]); yScale.domain( [d3.max(data, function(d) { return +d.all_prison; }), 0]); var circles = svg.selectAll("circle") .data(data) .enter() .append("circle"); circles.attr("cx", function(d) { return xScale(d.drug_prison); }) .attr("cy", function (d) { return yScale(d.all_prison); }) .attr("r", 3) .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(0," + (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)") //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