D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
sandravizz
Full window
Github gist
6.8 small multiple | area
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> <!-- Creating the headlines --> <p id="h1">AREA CHART | MULTIPLE</p> <div id="link"> by <a href="https://slides.com/sandravizmad">SANDRA</a> | <a href="https://data.oecd.org/forest/forest-resources.htm">DATA </a></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 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:7px; 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.3; stroke-opacity:0.6; stroke:white; stroke-dasharray:2; } /*Defining chart stylings*/ .line_n { stroke-width:1.5px; stroke:#8ff798; fill:none; } .area_prop { fill:#85f6fa; opacity:0.3; } .area_prop:hover { fill:#85f6fa; opacity:1; } .titles { font-family:'Montserrat Alternates', sans-serif; font-weight:400; font-size:12px; fill:#f5fa91; } </style> </head> <body> <script> //Margin conventions var m = {top:50, right:50, bottom:50, left:20} w = 0, h = 0; //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})`); //Margin conventions for the small multiple container var m_s = {top:20, right:15, bottom:20, left:25} w_s = 150 - m_s.left - m_s.right, h_s = 120 - m_s.top - m_s.bottom; //Define the format var parseDate = d3.timeParse("%Y"), formatDate = d3.timeFormat("%y"), formatNumber = d3.format(".0s"); //Data loading d3.csv( 'https://gist.githubusercontent.com/sandravizz/2b445498a7069725b8bf3ddedf37956e/raw/f3abfb6e59db1da6b5ea50ee848aa8ee22910a59/OECD:%2520Tree%2520felling%2520over%2520time%2520by%2520country', prepare, data => { //Nesting the data by symbol var country = d3.nest() .key(d => d.LOCATION) .entries(data); //Small multiple container var small = d3.select("svg") .data(country) .enter() .append("svg") .attr("width", w_s + m_s.left + m_s.right) .attr("height", h_s + m_s.top + m_s.bottom) .append("g") .attr("transform", `translate(${m_s.left}, ${m_s.top})`); //X scale var x = d3.scaleTime() .range([0, w_s]) .domain(d3.extent(data, d => d.TIME)); //Y scale var y = d3.scaleLinear() .domain([0, d3.max(data, d => +d.Value)]) .range([h_s, 0]); //X Axis small.append("g") .attr("class", "x-axis") .attr("transform", `translate(0, ${h_s})`) .call(d3.axisBottom(x) .tickPadding(5) .tickSize(-h_s) .tickValues([parseDate(2010)]) .tickFormat(formatDate)); //Y axis small.append("g") .attr("class", "y-axis") .call(d3.axisLeft(y) .tickValues([100000]) .tickSize(-w_s) .tickPadding(3) .tickFormat(formatNumber)); //Area generator var area = d3.area() .x(d => x(d.TIME)) .y0(y(0)) .y1(d => y(d.Value)); //Line generator var line = d3.line() .x(d => x(d.TIME)) .y(d => y(d.Value)); //Draw the line small.append("path") .attr("class", "line_n") .attr("d", d => line(d.values)) //Draw the area small.append("path") .attr("class", "area_prop") .attr("d", d => area(d.values)) //Adding the titles small.append("text") .attr("text-anchor", "start") .attr("y", -25) .attr("x", -20) .attr("class", 'titles') .text(d => d.key) }) //Make sure all variables are numbers function prepare (d) { d.Value = +d.Value; d.TIME = parseDate(d.TIME); return d; } </script> </body> </html>
https://d3js.org/d3.v4.min.js