D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
galenburrell
Full window
Github gist
Module 4: Ages of Top 10 Finishers
<!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; } .datalabels text { font-family: sans-serif; font-size: 10px; } rect: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>Ages of the Top 20 Dipsea finishers in 2014. Source: <a href="https://www.dipsea.org">dipsea.org</a></p> <script type="text/javascript"> //Set variables var dataset2; var w = 600; var h = 400; var padding = [ 20, 20, 20, 30 ]; //Set scales var xScale = d3.scale.linear().range([ 0, w - padding[1] - padding[3]]); var yScale = d3.scale.ordinal() .rangeRoundBands([ padding[0], h - padding[2] ], 0.2); //Load in contents of CSV file d3.csv("dipsea_filtered.csv", function(data) { //Filter data set dataset2 = data.filter(function(d) { return d.Year == 2014}).filter(function(d) { return d.Place <= 20 }); //Set x and y domain xScale.domain([ 0, d3.max(dataset2, function(d) { return +d.Age; })]); //yScale.domain(d3.range(dataset2.length)); yScale.domain(dataset2.map(function(d) { return d.Place; })); 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", w) .attr("height", h); //Log 'data' to the console, for verification. //console.log(data); //Bind filtered data into rect placeholders var rects = svg.selectAll("rect") .data(dataset2) .enter() .append("rect"); //Set attributes of rects rects.attr("x", padding[3]) .attr("y", function(d) { return yScale(d.Place); }) .attr("width", function(d) { return xScale(d.Age); }) .attr("height", yScale.rangeBand()) .attr("fill", "grey") .append("title") .text(function(d) { return "Place: " + d.Place + " Age: " + d.Age + " Head Start : " + d.HeadStart + " minutes"; }); svg.selectAll("text") .data(dataset2) .enter() .append("text") .text(function(d) { return d.Age; }) .attr("class","datalabels") .attr("text-anchor", "end") .attr("dominant-baseline","middle") .attr("font-size","10px") .attr("fill","white") .attr("x", function(d) { return padding[3] + xScale(d.Age) - 2; }) .attr("y", function(d) { return yScale(d.Place) + yScale.rangeBand()/2 ; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3] - 5) + ",0)") .call(yAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js