D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
galenburrell
Full window
Github gist
Module 5: Scatter Plot
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Historic Dipsea Results with D3</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: white; } h1 { font-family: sans-serif; font-size: 24px; margin: 0; } p { font-family: sans-serif; 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; } /* .y.axis path, .y.axis line { opacity: 0; } */ </style> </head> <body> <h1>The Dipsea Race</h1> <p>Age vs. Place. Source: <a href="https://www.dipsea.org">dipsea.org</a></p> <!-- <span id="35" class="p">Top 35 Only </span><span id="1000">All </span><p> --> <script type="text/javascript"> //Set variables var dataset; var svgwidth = 600; var svgheight = 500; var margin = {top: 10, right: 10, bottom: 50, left: 50 }; var w = svgwidth - margin.left - margin.right; var h = svgheight - margin.top - margin.bottom; //Set scales var xScale = d3.scale.linear() .range([ 0, w ]); var yScale = d3.scale.linear() .range([ 0, h ]); //Load in contents of CSV file d3.csv("dipsea_filtered.csv", function(data) { //Filter data set dataset = data; dataset2 = dataset .filter(function(d) { return d.Age > 0 }) .filter(function(d) { return d.Place <= 500 }); //Set x and y domain xScale.domain([ 0, d3.max(dataset2, function(d) { return +d.Age; }) ]); yScale.domain([ d3.max(dataset2, function(d) { return +d.Place; }), 0 ]); var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); //Create SVG canvas var svg = d3.select("body") .append("svg") .attr("width", svgwidth) .attr("height", svgheight) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); //Log 'data' to the console, for verification. //console.log(data); //Bind filtered data into rect placeholders var dots = svg.selectAll("circle") .data(dataset2) .enter() .append("circle"); //Set attributes of rects dots.attr("cx", function(d) { return +xScale(d.Age); }) .attr("cy", function(d) { return yScale(d.Place); }) .attr("r", 2) .attr("fill", "grey") .attr("opacity", 0.25) .append("title") .text(function(d) { return "Year: " + d.Year + " Place: " + d.Place + " Age: " + d.Age + " Head Start : " + d.HeadStart + " minutes"; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + h + ")") .call(xAxis) .append("text") .attr("transform", "rotate(0)") .attr("y", 6) .attr("dy", "3em") .attr("dx", w - margin.right) .style("text-anchor", "end") .text("Finisher Age"); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", "-4em") .style("text-anchor", "end") .text("Overall Place"); /* d3.selectAll("span") .on("click", function() { var cap = this.id; alert(cap); var dataset2 = dataset .filter(function(d) { return d.Age > 0 }) .filter(function(d) { return d.Place <= cap }); svg.selectAll("circle") .data(dataset2) .enter() .append("circle") //.transition() //.duration(500) //.delay(function(d, i) { // return i * 100; }) .attr("cx", function(d) { return +xScale(d.Age); }) .attr("cy", function(d) { return yScale(d.Place); }) .attr("r", 3) .attr("fill", "grey") .attr("opacity", 0.3) .append("title") .text(function(d) { return "Year: " + d.Year + " Place: " + d.Place + " Age: " + d.Age + " Head Start : " + d.HeadStart + " minutes"; }); }); //Update y-axis svg.select(".y.axis") yScale.domain([ d3.max(dataset2, function(d) { return +d.Place; }), 0 ]); svg.select(".y.axis") .transition() .duration(1000) .call(yAxis);*/ }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js