D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
rjweise
Full window
Github gist
#knightD3 Exercise rjweise
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>KnightD3 MOOC - rj workfile Mod3</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: #EBEBEB; font-family: Helvetica, Georgia; } h1 { color: #333333; font-size:30px; font-weight: bold; font-family: Helvetica; margin: 0; } p { color:black; font-family: Georgia; font-size:14px; margin: 10px 0 10px 0; } a { font-size:8px; font-family: Georgia; color:gray; font-style: italic; } svg { background-color: #EBEBEB; } rect:hover { fill: white; } .axis path, .axis line { fill: none; stroke: white; shape-rendering: crispEdges; } .axis text { font-family: Georgia; font-size: 10px; text-align: left; } .y.axis path { opacity: 0 } .y.axis line { opacity: 0; } </style> </head> <body> <h1><strong>rj's workfile for the module 4 exercise (#knightD3 MOOC)</strong></h1> <p>The graph below represents the number of immigrants to Canada by country of origin for the year 2006.<br> When placing and holding the mouse over a country, a tooltip will show up with the exact number. <br> The Netherlands are highlighted in Orange, as it is my personal country of origin, and the USA are highlighted in red. As there are quite a number of countries in the dataset I applied a threshold to only show countries with more than 600 immigrants. </p> <p></p> <script type="text/javascript"> //1a. Setting up width, height and padding var w = 1000; var h = 800; var padding = [50, 10, 20, 150] //for top, right, bottom, left var th = 600; // this sets the threshold for what data is shown (countries with this value or lower will not be included in the graph) //1b. Setting up horizontal and vertical scales var widthScale = d3.scale.linear() .range([0, w - padding[1] - padding[3]]); // from 0 to total width of SVG minus padding right and left var heightScale = d3.scale.ordinal() //.rangeRoundBands([padding[0], h - padding[2]], 0,5); //When using rangeRoundBands here there will be a significatn area above and below the rows .rangeBands([padding[0], h - padding[2]], 0.1); //from below top padding to height of SVG minus padding at bottom, with 10% spacing //1c. Setting up horizontal (x) and vertical (y) axis var xAxis = d3.svg.axis() .scale(widthScale) //length is width of the horizontal scale .orient("bottom"); //positioned at the bottom var yAxis = d3.svg.axis() .scale(heightScale) // length is height of the vertical scale .orient("left"); // postioned on the left //add another xAxis at the top as the table is long var xAxis2 = d3.svg.axis() .scale(widthScale) .orient("top"); //1d. SETTING UP THE 'CANVAS' TO DRAW ON, WITH SVG var svg = d3.select("body") .append("svg") .attr("width", w) // these values are no longer hard coded .attr("height", h); //2a. LOAD THE CONTENT OF THE CSV FILE, in this case the Canada immigration data, into dataload; filter it and store the result in datafilter; sort it and store it in data d3.csv("CanPermRes2004-2013byCountry.csv", function(dataload) { var datafilter = dataload.filter(function(d,i) { if (d["2006"]>th) { return d; } }) var data = datafilter.sort(function(a,b) { return d3.descending(+a["2006"], +b["2006"]) }) ; //2b. Setting the domains for width and height, related to the data we just loaded widthScale.domain([0, d3.max(data, function(d) { return +d["2006"]; })]); heightScale.domain(data.map(function(d) { return d.SourceCountry; })); //3a. CREATE A RECTANGLE CONTAINER ELEMENT called rects in the svg canvas //basically selecting all not-yet-existing rectangles in the svg, binding data to it from data (CSV), let the rectangles enter the stage and append the data from the csv to the rectangles that are now created, and... //3b. ... then GENERATE THE ATTIRBUTES FOR THE RECTANGLES based on the data from the csv, column with header 2006 var rects = svg.selectAll("rect") .data(data) // this is to show all data //.data(data.filter(function(d) { // return d["2006"]>500;})) // now it only shows data with 2006-values of over 500 .enter() .append("rect"); //All bars start on padding left, and we use heightScale for their top left corner. The width of the horizontal bar (so really its length in horizontal direction) is pulled from widthScale. //The Width (which in a way is the length of the bar) is read from the csv file, column header 2006 //The tooltip or title is then created and also appended to the individual bars rects.attr("x", padding[3]) .attr("y", function(d) { return heightScale(d.SourceCountry)+1; }) .attr("width", function(d) { return widthScale(d["2006"]); }) .attr("height", heightScale.rangeBand()-2) //.attr("fill", "grey") //Adding highlight colour for The Netherlands .attr("fill", function(d) { if (d.SourceCountry == "The Netherlands") {return "#F46B20"} else if (d.SourceCountry == "United States of America") {return "steelblue"} else {return "grey"} }) .append("title") .text(function(d) { return "In 2006, " + +d["2006"] + " people from " + d.SourceCountry + " emigrated to Canada."; }); //Now adding the actual axis (lines and ticks) to the SVG svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") //.attr("transform", "translate(" + padding[3] + "," + (h-150) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding[3] + ",0)") .call(yAxis); svg.append("g") // adding another x axis at the top .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + padding[0]*0.9 + ")") .call(xAxis2); }); </script> <p></p> <a href="https://www.cic.gc.ca/">Source: https://www.cic.gc.ca</a> <br> <a href="https://www.cic.gc.ca/english/resources/statistics/facts2013/permanent/10.asp">Specific source url</a> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js