D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
sandravizz
Full window
Github gist
4.10 scatter plot | glowed
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <html> <head> <!-- Google fonts reference--> <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> <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 0px 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:400; font-size:10px; opacity:1; fill:white; } .y-axis path { fill:none; stroke-width:0; stroke-opacity:1; stroke:white; } .x-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:0.5; stroke:white; stroke-dasharray:2; } </style> </head> <body> <!-- Creating the headlines --> <p id="h1">SCATTER | GLOWED</p> <div id="link">by <a href="https://slides.com/sandravizmad">SANDRA</a></div> <script> //Margin conventions var m = {top:100, right:50, bottom:50, left:40} w = 700 - m.left - m.right, h = 700 - m.top - m.bottom; //Container var svg = d3.select("body") .append("svg") .attr("width", w + m.left + m.right) .attr("height", h + m.top + m.bottom) .append("g") .attr("transform", `translate(${m.left}, ${m.top})`); //Global variables const user15 = "users_2015"; const change = "change"; const pop = "population"; //Define the format var formatNumber = d3.format(".1s"); //Data loading d3.csv( "https://gist.githubusercontent.com/sandravizz/30c3df855966b2e419f1509604f9210e/raw/8e63e4049e7e503e51dd5b574b6a24825f946fff/country_change", prepare, function(data) { //Radius scale var r = d3.scaleSqrt() .domain(d3.extent(data, d => d[pop])) .range([3, 25]) //X scale var x = d3.scaleLinear() .domain([0, 100]) .range([0, w]); //Y scale var y = d3.scaleLog() .domain(d3.extent(data, d => d[change])) .range([h, 0]); //Color scale var c = d3.scaleLog() .domain(d3.extent(data, d => d[pop])) .range(["#85f6fa", "#8ff798"]); //X axis svg.append("g") .attr("class", "x-axis") .attr("transform", `translate(0, ${h})`) .call(d3.axisBottom() .scale(x) .ticks(4) .tickSize(-h) .tickPadding(5)) .append("text") .text("% USAGE 2015") .attr("x", w) .attr("y", 30); //Y axis svg.append("g") .attr("class", "y-axis") .attr("transform",`translate(0, ${0})`) .call(d3.axisLeft() .scale(y) .ticks(4) .tickSize(-w) .tickPadding(5) .tickFormat(formatNumber)) .append("text") .text("% GROWTH 2010-15") .attr("x", 100) .attr("y", -20); //Creating a filter for the glowing svg.selectAll("filter") .data(d3.nest() .key(d => d.region) .entries(data)) .enter() .append("filter") .attr("width", "300%") .attr("x", "-100%") .attr("height", "300%") .attr("y", "-100%") .attr("id",d => d.key + "glow") .append("feGaussianBlur") .attr("stdDeviation", 4.5); //Drawing the circles svg.selectAll("g") .data(data) .enter() .append("g") .attr("transform", d => `translate(${x(d[user15])}, ${y(d[change])})`) .append("circle") .attr("r", d => r(d[pop])) .attr("fill", d => c(d[pop])) .style("filter", d => {return"url(#" + d.region + "glow)"}); }); //Check the data function prepare(d) { d.regin = d.region; d.change = +d.change; d.population = +d.population; d.users_2015 = +d.users_2015; d.users_2010 = +d.users_2010; return d; }; </script> </body> </html>
https://d3js.org/d3.v4.min.js