This scatterplot is constructed from a TSV file storing the dimensions of sepals and petals of various iris flowers. The chart employs conventional margins and a number of D3 features:
forked from mbostock's block: Scatterplot
xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
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 + ")");
d3.csv("https://numeracy.co/projects/4qB9fPoRLRU/deaths-by-state?format=csv", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d.deaths_per_mil = parseFloat(d.deaths_per_mil);
d.violent_crime = +d.violent_crime;
});
x.domain(d3.extent(data, function(d) { return d.violent_crime; })).nice();
y.domain(d3.extent(data, function(d) { return d.deaths_per_mil; })).nice();
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("Violent Crime (per 100,000)");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Police-involved deaths (per million)")
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 4)
.attr("cx", function(d) { return x(d.violent_crime); })
.attr("cy", function(d) { return y(d.deaths_per_mil); })
.style("fill", function(d) { return "#5cb3ff" })
.append("svg:title")
.text(function(d) { return d.state });
});
</script>
https://d3js.org/d3.v3.min.js