D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
devdiva8
Full window
Github gist
Ex. 4 scales and axes, plus small multiples
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Exercise 3- Sorting Education data</title> <script type="text/javascript" src="https://d3js.org/d3.v3.min.js"></script> <link href='https://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'> <style type="text/css"> body {background-color: white; font-family: Lato; } .axis path, .axis line { fill: none; stroke: #888888; stroke-opacity: .75; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; fill:#888888; } .y.axis path, .y.axis line { opacity: 0; } svg, div { background-color: #F9F1DD; } rect {fill:#94B2BD;} h1, p { position: relative; left: 10px; color: #333333; } div {text-align:center; width:315px; font-size:12px; font-weight:bold; padding-top:6px; padding-bottom:6px; } #notes {width:700px; text-align:left; background-color:white; font-size:12pt;} </style> </head> <body> <h1>Algebra Scores for U.S. 8th Grade Students by Race/ethnicity: 2013</h1> <script type="text/javascript"> var city = "San Diego"; var w = 315; // svg width var h = 130; var padding = [ 20, 10, 30, 60 ]; //Top, right, bottom, left // X axis prep var widthScale = d3.scale.linear() .range([ 0, w - padding[1] - padding[3] ]); var heightScale = d3.scale.ordinal() .rangeRoundBands([ padding[0], h - padding[2] ], 0.1); var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(heightScale) .orient("left"); d3.select("body").append("div").text(city); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("gr4gr8_math_v3short.csv", function(data) { // Filter and sort data data = data.filter(function(d){return d.Year == 2013 && d.Grade == 8 && d.Measure=="algebra" && d.Jurisdiction == city }); data.sort(function(a,b){return d3.descending(a.Score, b.Score)}) widthScale.domain([ 0, d3.max(data, function(d) { return +d.Score; }) ]); heightScale.domain(data.map(function(d) { return d.Race; } )); // NEW !!!!!!!!!!!!! var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", padding[3]) .attr("y", function(d) { return heightScale(d.Race); }) .attr("width", function(d) { return widthScale(d.Score); }) .attr("height", heightScale.rangeBand()) // .attr("fill", "steelblue") .append("title") .text(function(d) { return "8th grade algebra score for " + d.Race + " is " + d.Score; }); 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] + ",0)") .call(yAxis); // Value labels var texts = svg.selectAll("text") .data(data) .enter() .append("text"); texts.attr("x", function(d) { return (+d.Score/1.2+3); }) .attr("y", function(d, i) { return i * 25+15; }) .attr("fill","#777777") .style("font-family", "Lato") .style("font-size", "12px") .text(function(d) { return d.Score;}); }); </script> <div id="notes"><h3>Notes</h3> <p>U.S. schools are under pressure to close achievement gaps observed across different race/ethnicity groups. This chart looks at scores for U.S. 8th grade students in algebra for 2013 for San Diego. I would like to expand the visualization comparing more cities in a small multiples display. So far, I have this <a href="https://bl.ocks.org/devdiva8/raw/397d86b6ca070e8d0580">small multiples prototype</a>.</p> <p>Source: National Center for Education Statistics, National Assessment of Educational Progress (NAEP) <a href=""https://nces.ed.gov/nationsreportcard/naepdata/>Data Explorer</a>.</p> <p>Data for Asian/Pacific Islander and American Indian/Alaska Native groups were removed, because in some cells "reporting standards [were] not met." However, data for these groups are included in "All races" scores.</p> </div> </body> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js