D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
luckymike
Full window
Github gist
BART Weekday Ridership January 2015
<!DOCTYPE html> <html lang ="en"> <head> <meta charset="utf-8"> <title>BART Destinations from Embarcadero - D3 Course</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { } svg { background-color: white; } rect { fill: green; } rect:hover { fill: orange; } .axis path, line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11pt; } .y .domain { stroke: none; } .y line { stroke: none; } </style> </head> <body> <h2>January 2015 BART Destinations for Riders Boarding at Embarcadero</h2> <p>Weekday ridership averages. Source: <a href="https://www.bart.gov/about/reports/ridership">BART</a></p> <script type="text/javascript"> w = 1000 h = 800 topPadding = 20 bottomPadding = 50 leftPadding = 200 rightPadding = 50 var widthScale = d3.scale.linear() .range([ 0, w - (leftPadding + rightPadding) ]); var heightScale = d3.scale.ordinal() .rangeRoundBands([ topPadding , h - bottomPadding ], 0.2); var stationCodes = d3.scale.ordinal() .domain( [ 'RM','EN','EP','NB','BK','AS','MA','19','12','LM','FV','CL','SL','BF','HY','SH','UC','FM','CN','PH','WC','LF','OR','RR','OW','EM','MT','PL','CC','16','24','GP','BP','DC','CM','CV','ED','NC','WP','SS','SB','SO','MB','WD','OA' ] ) .range( [ 'Richmond', 'El Cerrito del Norte', 'El Cerrito Plaza', 'North Berkeley', 'Berkeley', 'Ashby', 'MacArthur', '19th Street', '12th Street', 'Lake Merritt', 'Fruitvale', 'Coliseum', 'San Leandro', 'Bay Fair', 'Hayward', 'South Hayward', 'Union City', 'Fremont', 'Concord', 'Pleasant Hill', 'Walnut Creek', 'Lafayette', 'Orinda', 'Rockridge', 'West Oakland', 'Embarcadero', 'Montgomery', 'Powell Street', 'Civic Center', '16th Street Mission', '24th Street Mission', 'Glen Park', 'Balboa Park', 'Daly City', 'Colma', 'Castro Valley', 'Dublin/Pleasanton', 'North Concord/Martinez', 'Pittsburg/Bay Point', 'South San Francisco', 'San Bruno', 'SFO', 'Milbrae', 'West Dublin/Pleasanton', 'Oakland Airport']) var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(heightScale) .orient("left"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("BART_Weekday_Ridership_January_2015.csv", function(data) { data.sort(function(a,b) { return d3.descending(+a.EM, +b.EM); }); widthScale.domain([ 0, d3.max(data, function(d) { return +d.EM; }) ]); heightScale.domain(data.map(function(d) { return stationCodes(d.Exitstations) })); var bars = svg.selectAll("rect") .data(data) .enter() .append("rect"); bars.attr("x", leftPadding) .attr("y", function(d) { return heightScale(stationCodes(d.Exitstations)); }) .attr("width", function(d) { return widthScale(+d.EM); }) .attr("height", heightScale.rangeBand()) .append("title") .text(function(d) { return stationCodes(d.Exitstations) + "–" + d.EM; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + leftPadding + "," + (h - bottomPadding) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (leftPadding - 10) + ", 0)") .call(yAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js