D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
goldfarb
Full window
Github gist
Preparing for Canada's Future, Today!
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Hope for the Future</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica, Arial, sans-serif; font-size:12px; } h1 { font-size: 24px; margin: 0; } p { font-size: 14px; margin: 10px 0 0 0; } svg { background-color: white; } rect:hover { fill: orange; text:visible; } .axis { font-size: 18px; } .legend { font-size: 14px; font-style: italic; } } </style> </head> <body> <h1>Preparing for Canada's Future, Today!</h1> <script type="text/javascript"> var padding = [ 10, 10, 20, 40 ]; //Top, right, bottom, left var w = window.innerWidth * .9; var h = window.innerHeight * .8; var fullw = w * 1.1; var fullh = h * 1.2; var heightScale = d3.scale.linear() .range([ 0, h ]); var widthScale = d3.scale.ordinal() .rangeRoundBands([ 0, w ], 0.1); 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", fullw) .attr("height", fullh); d3.csv("terrible.csv", function(data) { data.sort(function(a, b) { return d3.ascending(+a.useful, +b.useful); }); heightScale.domain([ 0, d3.max(data, function(d) { return +d.percentage; }) ]); widthScale.domain(data.map(function(d) { return d.knowledge; } )); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("height", function(d){ return (d.horrifying * h / 100) }) .attr("x", function(d) { return widthScale(d.knowledge); }) .attr("y", function(d) { return h - heightScale(d.percentage) + ( padding[0] + padding[2]); }) .attr("width", widthScale.rangeBand()) .attr("fill", function(d){ return "rgb("+d.horrifying * 25+","+d.horrifying * 12+","+d.horrifying * 2+")" } ) .append("title") .text(function(d) { return d.knowledge + " : " + d.percentage + "%"; }); ; var texts = svg.selectAll("text") .data(data) .attr("class","label") .enter() .append("text") .text(function(d){ return d.percentage + "%"; }) .attr("x", function(d){ return widthScale(d.knowledge); }) .attr("y", function(d){ return h - heightScale(d.percentage) + ( padding[0] + padding[2]) }); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding[3] / 2 + "," + h / 3 + ")") .append("text") .attr("transform", "rotate(-90)") .style("text-anchor", "end") .text("percent knowledgeable"); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + (w / 3 - padding[1]) + ", " + (fullh - padding[0] - padding[2]) + ")") .append("text") .text("<-- less useful knowledge -- more useful knowledge -->"); svg.append("g") .attr("class", "legend") .attr("transform", "translate("+ (w / 3 - padding[3] - padding[1])+", " + (padding[2]) + ")") .append("text") .text("each rectangle is scaled & coloured by how concerning the percentage is"); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js