D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
sandravizz
Full window
Github gist
4.11 scatter plot | small multiple
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <html> <head> <!-- Google fonts --> <link href="https://fonts.googleapis.com/css2?family=Montserrat+Alternates:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Roboto:ital,wght@0,100;0,300;1,100;1,300&display=swap" rel="stylesheet"> <!-- Connecting with D3 library--> <script src="https://d3js.org/d3.v4.min.js"></script> <!-- Creating the charting area --> <div id="vis"></div> <style> /*Defining text stylings*/ #h1 { font-size:30px; margin:30px 0px 0px 20px; color:#f5fa91; font-family: 'Montserrat Alternates', sans-serif; font-weight:300; } #link { font-family:'Montserrat Alternates', sans-serif; font-weight:200; font-size:10px; margin:5px 0px 60px 22px; color:white; } a:link, a:visited, a:active { text-decoration: none; color:white; border-bottom:1.5px dotted white; } body { background-color:#011227; } /*Defining axis stylings*/ .y-axis text, .x-axis text { font-family:'Montserrat Alternates', sans-serif; font-weight:100; font-size:9px; opacity:1; fill:white; } .x-axis path, .y-axis path { fill:none; stroke-width:0; stroke-opacity:1; stroke:white; } .y-axis line, .x-axis line { fill:none; stroke-width:0.1; stroke-opacity:1; stroke:white; stroke-dasharray:2; } #axis { font-family:'Montserrat Alternates', sans-serif; font-size:10px; font-variant: small-caps slashed-zero; fill:white; } /*Defining chart stylings*/ circle { stroke-width:1px; stroke:#85f6fa; stroke-opacity:1; fill:#85f6fa; fill-opacity:0.5; } #name { font-family:'Montserrat Alternates', sans-serif; font-size:12px; fill:white; } </style> </head> <body> <!-- Creating the headlines --> <p id="h1">SCATTER PLOT | SMALL MULTIPLE</p> <div id="link">BY <a href="https://slides.com/sandravizmad">SANDRA</a></div> <script> //Global variables var genders = ["Women", "Men"]; var ages = ["Total", "15 to 24", "25 to 54", "55 to 64"]; //Margin conventions var m = {top:20, right:50, bottom:100, left:50} cw = 400 - m.left - m.right ch = 400 - m.top - m.bottom w = cw * ages.length h = ch * genders.length; //Container var svg = d3.select("body") .append("svg") .attr("width",w) .attr("height",h) .append("g"); //X scale var x = d3.scaleLinear() .domain([0, 1]) .range([0, 200]); //Y scale var y = d3.scaleLinear() .domain([0, 0.3]) .range([200, 0]); //Data loading d3.csv( "https://gist.githubusercontent.com/sandravizz/6fa65b3d068dbbedece44947332cdd00/raw/208dce16d8242f70cae5f4c1a7f3e2492f1b9feb/Unemployment%2520rate", data => { for(j = 0; j < ages.length; j++) { var age = ages[j]; //Small multiple container var gAge = svg.append("g") .attr("transform", `translate(${j * (cw + m.left + m.right)}, 0)`); //Title for small multiple container gAge.append("text") .attr("id", "name") .attr("x", 120) .attr("y", 10) .text(age); for( i = 0; i < genders.length; i++) { var gender = genders[i]; var g = gAge.append("g") .attr("transform", `translate(${m.left}, ${m.left + i * (ch + m.bottom)})`); var filter = data.filter(d => d.Age == age&&d.Sex == gender); //Draw the circles g.selectAll("circle") .data(filter) .enter() .append("circle") .attr("r", 6) .attr("cx", d => x(d.EmpPop)) .attr("cy", d => y(d.UnempPop)); //X Axis g.append("g") .attr("transform", "translate(0,"+ch+")") .call(d3.axisBottom() .scale(x) .ticks(3) .tickPadding(2)) .attr("class", "x-axis"); g.append("text") .attr("id", "axis") .attr("x", cw - 65) .attr("y", ch + 25) .text("employed / pop"); //Y Axis g.append("g") .call(d3.axisLeft() .scale(y) .ticks(3) .tickSize(-cw) .tickPadding(2)) .attr("class","y-axis"); g.append("text") .attr("id", "axis") .attr("transform", "rotate(-90)") .attr("y", -20) .attr("x", -75) .text("unemployed / pop"); } } }); </script> </body> </html>
https://d3js.org/d3.v4.min.js