D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
rjweise
Full window
Github gist
SVG positioning
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>KnightD3 MOOC - Final page</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: #EBF5FA; font-family: Georgia, serif, Impact, Charcoal, sans-serif; } h1 { color: #333333; font-size:40px; font-weight: bold; font-family: Impact, Charcoal, sans-serif; margin: 0; } h2 { color: dimgray; font-size:30px; font-weight: bold; font-family: Impact, Charcoal, sans-serif; margin: 0; text-align: left; } h3 { color: dimgray; font-size:24px; font-family: Impact, Charcoal, sans-serif; margin: 0; } p { color:black; font: Georgia, serif; font-size:16px; margin: 10px 0 10px 0; width: 940px } a { font-size:12px; font-family: Georgia, serif; color:gray; font-style: italic; } svg { margin: 80px 0 0 50px; left: 500px; } svg2 { left: 500px; margin: 80px 0 0 50px; } svg9 { background-color: green; position: absolute; top: 1200px; float:left; } rect:hover { fill: LightGray; } .axis path, .axis line { fill: none; stroke: dimgray; shape-rendering: crispEdges; } .axis text { font-family: Georgia, serif; font-size: 12px; text-align: left; } .y.axis path { opacity: 0 } .y.axis line { opacity: 100; } .yy.axis path { opacity: 100 } .yy.axis line { opacity: 100; } #intro { width: 400px; height: 880px; float: left; } #graph1 { position: absolute; left: 420px; min-width: 580px; height: 100px; margin: 0 0 0 0; border: 1px solid red; text-align: left; } #graph2 { position: absolute; top: 625px; left:420px; width: 580px; border: 1px solid green; } #source { position: fixed; bottom: 0; right: 0; text-align: right; width: 200px; } #graph3 { position: absolute; top:920px; } </style> </head> <body> <h1>Immigrants to Canada per country of origin</h1><br> <h3>#KnightD3 MOOC - Final page, by RJ Weise</h3> <hr> <div ID="intro"> <p ID="intro">In May 2006 I emigrated to Canada from The Netherlands. To get a better idea of how many people immigrated from The Netherlands and from all over the world, the three graphs below represent subsets of the total number of immigrants to Canada by country of origin for the year 2006 (and 2013 in the third). <br><br> The <strong>first graph</strong> shows the top 20 ranked countries by number of immigrants. The <strong>second graph</strong> shows The Neterlands and the three countries above and below it in ranking. The <strong>third graph</strong> shows the top 52 ranked countries (The Netherlands ranks 51 in 2006) with the number of immigrants for 2006 (x-axis) and 2013 (x-axis). Colour coding shows if the numbers have increased or decreased (see graph sub-header). <br> <br> Where applicable The Netherlands are highlighted in Orange (as it is my personal country of origin), and The United States of America are highlighted in blue as that is the country where I am taking the course. <br><br> Note that after a short delay, holding the mouse over a bar of a specific country, a tooltip will show additional information.<br> </p> </div ID="intro"> <div ID="graph1"> <h2>Top 20 ranked countries by number of immigrants to Canada in 2006</h2> <script type="text/javascript"> //1a. Setting up width, height and padding var w = 500; var h = 400; var padding = [50, 10, 20, 150] //for top, right, bottom, left var th = 20; // this sets the threshold for what data is shown based on ranking of countries descending from highest number of immigrants (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 datasorter = dataload.sort(function(a,b) { return d3.descending(+a["2006"], +b["2006"]) }) var datafilter = datasorter.filter(function(d,i) { if (i < th) { return d; } }) var data = datafilter; //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 //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"); //console.log(data); 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."; }); //ADDING HERE FOR LABELS var datalabel = svg.selectAll("text") .data(data) .enter() .append("text"); datalabel.text(function(d, i) { return data[i]["2006"]; }) .attr("x", function(d) { return (padding[3]-30) + widthScale(d["2006"]) }) //.attr("x", 155) .attr("y", function(d) { return heightScale(d.SourceCountry)+10; }) .attr("fill", "lightgray") .attr("font-size", "9px") .attr("font-family", "Georgia") .attr("font-style", "italic") ; //console.log(data[i].SourceCountry) //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><br><br><br></p> </div ID="graph1"> <div ID="graph2"> <h2>Where do The Netherlands fit in?</h2> <p>The Netherlands and the three countries above and below in the rankings.</p> <script type="text/javascript"> //1a. Setting up width, height and padding var w2 = 500; var h2 = 200; var padding2 = [25, 10, 20, 150] //for top, right, bottom, left var th2 = 99; // this sets the threshold for what data is shown (countries with this value or lower will not be included in the graph) var th2up = 999; var th2dn = 999; //1b. Setting up horizontal and vertical scales var widthScale2 = d3.scale.linear() .range([0, w2 - padding2[1] - padding2[3]]); // from 0 to total width of SVG minus padding right and left var heightScale2 = 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([padding2[0], h2 - padding2[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 xAxis4 = d3.svg.axis() .scale(widthScale) //length is width of the horizontal scale .orient("bottom"); //positioned at the bottom var yAxis4 = d3.svg.axis() .scale(heightScale2) // 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 xAxis5 = d3.svg.axis() .scale(widthScale) .orient("top"); */ //1d. SETTING UP THE 'CANVAS' TO DRAW ON, WITH SVG var svg2 = d3.select("body") .append("svg") .attr("width", w2) // these values are no longer hard coded .attr("height", h2); //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(dataload2) { //this loads the CSV again, but into dataload2 var datasorter2 = dataload2.sort(function(a,b) { //this uses the filtered dataset datafilter2 and sorts it descendingly return d3.descending(+a["2006"], +b["2006"]) }) var datafilter2 = dataload2.filter(function(d,i) { //filter the dataload2 dataset for all records with a value smaller or equal to the threshold (which is set in var) //The following filters all countries with a ranking lower than the set threshold th2 based on its rank after sorting decendingly if (d.SourceCountry == "The Netherlands") {th2 = i, th2up = i-4, th2dn = i+4; } //This defines the rank where The Netherlands sits }) var datafilter3 = dataload2.filter(function(d,i) { if (i > th2up && i <= th2) { return d; } if (i > th2 && i < th2dn) { return d; } }) var data2 = datafilter3 //this is the element we are using later on /* if (d["2006"]<=th2) { return d; } }) */ ; //2b. Setting the domains for width and height, related to the data we just loaded widthScale2.domain([0, d3.max(data2, function(d) { return +d["2006"]; })]); heightScale2.domain(data2.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 rects2 = svg2.selectAll("rect") .data(data2) // 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 rects2.attr("x", padding2[3]) .attr("y", function(d) { return heightScale2(d.SourceCountry)+1; }) .attr("width", function(d) { return widthScale(d["2006"]); }) .attr("height", heightScale2.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. The Netherlands are ranked " + th2; }); //ADDING HERE FOR LABELS var datalabel2 = svg2.selectAll("text") .data(data2) .enter() .append("text"); datalabel2.text(function(d, i) { return data2[i]["2006"]; }) .attr("x", function(d) { return (padding[3]+5) + widthScale(d["2006"]) }) //.attr("x", 200) .attr("y", function(d) { return +heightScale2(d.SourceCountry)+10; }) //.attr("y", 150) .attr("fill", "black") .attr("font-size", "9px") .attr("font-family", "Georgia") .attr("font-style", "italic") ; //Now adding the actual axis (lines and ticks) to the SVG svg2.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding2[3] + "," + (h2 - padding2[2]) + ")") //.attr("transform", "translate(" + padding[3] + "," + (h-150) + ")") .call(xAxis4); svg2.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding2[3] + ",0)") .call(yAxis4); /* svg2.append("g") // adding another x axis at the top .attr("class", "x axis") .attr("transform", "translate(" + padding2[3] + "," + padding2[0]*0.9 + ")") .call(xAxis5); */ }); </script> <p><br><br></p> </div> <div ID="graph3"> <h2>Comparing the number of immigrants to Canada in 2006 and 2013</h2> <p>This section is for the Module five assignment, in which I compare two quantitative values in a scatter-plot<br> The Netherlands (orange outline) and the USA (blue outline) are highlighted in larger sizes than all other countries and the outline colours are orange and blue respectively. All countries have a purple fill colour if they increased from 2006 to 2013, and grey if they stayed the same or decreased. </p> <script type="text/javascript"> //1a. Setting up width, height and padding var w9 = 940; var h9 = 600; var padding9 = [100, 10, 20, 150] //for top, right, bottom, left var th9 = 52; // this sets the threshold for what data is shown based on ranking of countries descending from highest number of immigrants (countries with this value or lower will not be included in the graph) //1b. Setting up horizontal and vertical scales AND PIXEL RANGES var widthScale9 = d3.scale.linear() .range([0, w9 - padding9[1] - padding9[3]]); // from 0 to total width of SVG minus padding right and left var heightScale9 = d3.scale.linear() .range([0, h9 - padding9[2] - padding[0]]); //from 0 to total height of svg minus padding on bottom //ORG.range([padding9[0], h - padding9[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 xAxis9 = d3.svg.axis() .scale(widthScale9) //length is width of the horizontal scale .orient("bottom"); //positioned at the bottom var yAxis9 = d3.svg.axis() .scale(heightScale9) // length is height of the vertical scale .orient("left"); // postioned on the left //1d. SETTING UP THE 'CANVAS' TO DRAW ON, WITH SVG var svg9 = d3.select("body") .append("svg") .attr("width", w9) // these values are no longer hard coded .attr("height", h9); //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(dataload9) { var datasorter9 = dataload9.sort(function(a,b) { //This is to load the data ranked from highest immigrant number to lowest, based on column 206 return d3.descending(+a["2006"], +b["2006"]) }) var datafilter9 = datasorter9.filter(function(d,i) { //This filters out any records that are ranked lower than the threshold variable th9 if (i < th9) { return d; } }) var data9 = datafilter9; //This just puts the result of the above sort and filter in a variable called data9 //2b. Setting the domains for width and height, related to the data we just loaded widthScale9.domain([0, d3.max(data9, function(d) { return +d["2006"]; })+5000]); heightScale9.domain([d3.max(data9, function(d) { return +d["2013"]; })+5000, 0]); //3a. CREATE A RECTANGLE CONTAINER ELEMENT //3b. ... then GENERATE THE ATTIRBUTES FOR THE RECTANGLES based on the data from the csv, column with header 2006 var circles9 = svg9.selectAll("circle") .data(data9) // 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("circle"); circles9 .attr("cx", function(d) { return widthScale9(d["2006"])+padding9[3]; }) .attr("cy", function(d) { return heightScale9(d["2013"]); }) .attr("r", 2) //.attr("r", function(d) { // if (d.SourceCountry == "The Netherlands") {return 8} // else if (d.SourceCountry == "United States of America") {return 5} // else {return 3} //}) .style("fill", "grey") .append("title") .text(function(d) { return d.SourceCountry + " > " + +d["2006"] + " (in '06), " +d["2013"] + " (in '13)"; }) ; circles9. transition() //.delay(500) .duration(2000) //.attr("r", 12) .attr("r", function(d) { if (d.SourceCountry == "The Netherlands") {return 35} else if (d.SourceCountry == "United States of America") {return 35} else {return 2} }) .style("stroke", function(d) { if (d.SourceCountry == "The Netherlands") {return "#F46B20"} else if (d.SourceCountry == "United States of America") {return "steelblue"} else {return "black"} }) .style("stroke-width", function(d) { if (d.SourceCountry == "The Netherlands") {return 3} else if (d.SourceCountry == "United States of America") {return 3} else {return 1} }) ; circles9. transition() .delay(3000) .duration(2000) //.attr("r", 12) .attr("r", function(d) { if (d.SourceCountry == "The Netherlands") {return 10} else if (d.SourceCountry == "United States of America") {return 7} else {return 4} }) ; circles9. transition() .delay(6000) .duration(2000) //Adding highlight colour for The Netherlands .style("fill", function(d) { if (d["2013"] > d["2006"]) {return "green"} else if (d["2013"] <= d["2006"]) {return "purple"} }) .style("fill-opacity", function(d) { if (d.SourceCountry == "The Netherlands") {return 0.3} else if (d.SourceCountry == "United States of America") {return 0.5} else {return 0.6} }) ; //ADDING HERE FOR LABELS var datalabel9 = svg9.selectAll("text") .data(data9) .enter() .append("text"); //datalabel9.text(function(d, i) { //return "'06: " + data9[i]["2006"] + ", '13: " + data9[i]["2013"]}) //I turned this off for the scatter-plot as it clutters the graph datalabel9.text(function(d) { if (d.SourceCountry == "The Netherlands" || d.SourceCountry == "United States of America") {return(d.SourceCountry)} }) .attr("x", function(d) { return (widthScale9(d["2006"])+padding9[3]+5) }) //.attr("x", 155) .attr("y", function(d) { return heightScale9(d["2013"]); }) .attr("fill", "black") .attr("font-size", "7px") .attr("font-family", "Georgia") .attr("font-style", "italic") ; //Now adding the actual axis (lines and ticks) to the SVG svg9.append("g") .attr("class", "x axis") //.attr("transform", "translate(" + padding9[3] + "," + (h9 - padding9[2]) + ")") .attr("transform", "translate(" + padding[3] + "," + (h9-70)+ ")") .call(xAxis9) .append("text") .attr("class", "label") //.attr("transform", "rotate(-90)") .attr("x", w9-padding[1]-padding9[3]) .attr("y", -10) .style("text-anchor", "end") .style("Font-family", "Impact, Charcoal, sans-serif") .style("font-size", "20") .text("Yr 2006"); svg9.append("g") .attr("class", "yy axis") .attr("transform", "translate(" + padding9[3] + ",0)") .call(yAxis9) .append("text") .attr("class", "label") //.attr("transform", "rotate(-45)") .attr("x", -3) .attr("y", padding[2]) .style("text-anchor", "end") .style("Font-family", "Impact, Charcoal, sans-serif") .style("font-size", "20") .text("Yr 2013"); //*********** //svg.append("g") // .attr("class", "y axis") // .attr("transform", "translate(" + padding[3] + ",0)") // .call(yAxis) // .append("text") // .attr("class", "label") // .attr("transform", "rotate(-90)") // .attr("x", -20) // .attr("y", 5) // .attr("dy", ".91em") // .style("text-anchor", "end") // .text("Taux de participation"); //*************** }); </script> </div> <div ID="source"> <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> </div> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js