D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
yppmark
Full window
Github gist
Exercise 5 Scatterplot
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Exercise 5</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica, Arial, sans-serif; } h1 { font-size: 24px; margin: 0; } p { font-size: 14px; margin: 10px 0 0 0; } svg { background-color: white; } circle:hover { fill: orange; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } </style> </head> <body> <h1>RSPB Big Garden Bird Watch Results 2015</h1> <p>The Royal Society for the Protection of Birds (RSPB) is a conservation charity based in the UK.</p> <p>The Big Garden Bird Watch is the world's biggest citizen based wildlife survey.</p> <p>Over the course of one weekend over half a million people spent an hour observing the number and different types of bird species in their backgarden.</p> <p>This scatter plot shows the mean number of species spotted at any one time against the percentage of gardens in which they were spotted in.</p> <p>Source: <a = href="https://www.rspb.org.uk/discoverandenjoynature/discoverandlearn/birdwatch/results.aspx">RSPB</a>, 2015</p> <script type="text/javascript"> var w = 700; var h = 490; var padding = [ 20, 10, 20, 100 ]; //Top, right, bottom, left var xScale = d3.scale.linear() .range([ 0, 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"); var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("RSPB_BigGardenBirdWatch_UK_2015.csv", function(data) { data.sort(function(a, b) { return d3.descending(+a.Percentage_Gardens, +b.Percentage_Gardens); }); xScale.domain([ d3.min(data, function(d) { return +d.Percentage_Gardens; }), d3.max(data, function(d) { return +d.Percentage_Gardens; }) ]); yScale.domain([ d3.max(data, function(d) { return +d.Mean; }), d3.min(data, function(d) { return +d.Mean; }), ]); var circles = svg.selectAll("circle") .data(data) .enter() .append("circle"); circles.attr("cx", function(d) { return xScale(d.Percentage_Gardens); }) .attr("cy", function(d) { return yScale(d.Mean); }) .attr("r", 3) .attr("fill", "steelblue") .append("title") .text(function(d) { return "A mean number of " + d.Mean + " " +d.Species+ "'s were spotted in " + d.Percentage_Gardens + "% of gardens."; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2]) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding[3] + ",0)") .call(yAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js