D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
alebusi
Full window
Github gist
How much is thirteen to twenty-seven?
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>How much is thirteen to twenty-seven?</title> <script type="text/javascript" src="https://d3js.org/d3.v3.min.js"></script> <style type="text/css"> body { background-color: white; font-family: Verdana; } svg { background-color: white; } circle { fill: rgb(60, 161, 189); } circle:hover { fill: rgb(33, 93, 109); } text { fill: black; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } </style> </head> <body> <h2>How much is thirteen to twenty-seven?</h2> <p>This dataset gives an insight about the mathematics skills of boys and girls across 37 OECD countries. Each circle represent a country, placed according to boys' score on y axis and girls' score on x axis. </p> <script type="text/javascript"> var w = 500 var h = 500 var padding = [ 50, 50, 50, 50 ]; //Top, right, bottom, left var xScale = d3.scale.linear() .range ([ 0, w - padding[1] - padding[3] ]); var yScale = d3.scale.linear() .range ([ 0, h - padding[0] - padding[2] ]); var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .ticks(15); var yAxis = d3.svg.axis() .scale(yScale) .orient("left") .ticks(15); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("OECD_ISA_2012.csv", function(data) { data.sort(function(a,b){ return d3.ascending (a.mathGirls, b.mathGirls); }) xScale.domain([ d3.min(data, function(d) { return +d.mathGirls; }), d3.max(data, function(d) { return +d.mathBoys; }) ]); yScale.domain([ d3.max(data, function(d) { return +d.mathBoys; }), d3.min(data, function(d) { return +d.mathGirls; }) ]); svg.append("line") .attr("x1", padding[3]) .attr("y1", (h - padding[2])) .attr("x2", (w - padding[1])) .attr("y2", padding[0]) .attr("stroke", "black") .attr("stroke-width","1"); svg.append("text") .attr("x", (padding[3] + 20)) .attr("y", (padding[0] + 12)) .attr("text-anchor", "start") .attr("font-size", 12) .text("Boys performed better") svg.append("text") .attr("x", (w - padding[1])) .attr("y", (h - padding[2] - 20)) .attr("text-anchor", "end") .attr("font-size", 12) .text("Girls performed better") svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + (padding[3]) + "," + (h - padding[0]) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3]) + ", " + (padding[0]) + ")") .call(yAxis); var circles = svg.selectAll("circle") .data(data) .enter() .append("circle") circles.attr("cx", function(d){ return xScale(d.mathGirls)+padding[3]; }) .attr("cy", function(d, i){ return yScale(d.mathBoys)+padding[2]; }) .attr("r", 4) .append("title") .text(function(d){ return d.country+": girls scored "+d.mathGirls+" points ; boys scored "+d.mathBoys+" points" }); // var labels = svg.selectAll("text") // .data(data) // .enter() // .append("text") // labels.attr("x", 10) // .attr("y", function(d, i){ // return i * heightScale.rangeBand()+heightScale.rangeBand()*0.7; // }) // .attr("font-size", function(d){ // return heightScale.rangeBand()*0.7; // }) // .text(function(d){ // return d.country; // }) }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js