D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
sandravizz
Full window
Github gist
6.10 area chart | ridgline
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 headlines --> <p id="h1">AREAS | RIDGLINE</p> <div id="link"> by <a href="https://slides.com/sandravizmad">SANDRA</a> | <a href="https://data.oecd.org/agroutput/meat-consumption.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 chart stylings*/ .line { fill:none; stroke:white; stroke-width:0.5px; stroke-dasharray:2; } .area { fill:#85f6fa; stroke:none; } .activity:nth-child(odd) .area { fill:#8ff798; } /*Defining axis stylings*/ .y-axis text, .x-axis text { font-family:'Montserrat Alternates', sans-serif; font-weight:100; 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> <script> //Margin conventions var m = {top:80, right:20, bottom:50, left:200} w = 1000 - m.left - m.right, h = 900 - 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})`); //Data format var formatTime = d3.timeFormat('%I %p'); function parseTime(offset) { var date = new Date(2017, 0, 1); return d3.timeMinute.offset(date, offset); } //Data loading d3.tsv( 'https://gist.githubusercontent.com/sandravizz/634e39e1e41fc6ef993df149afdd538c/raw/b497b9d33fcaf94e2ab728a266836dc1211335ef/data.tsv', prepare, data => { //Sort by time data.sort((a,b) => a.time - b.time); //Nest data var data_nested = d3.nest() .key(d => d.activity) .entries(data); //Check console.log(data_nested); //Y visual encoding var yvar = d => d.value; //Y2 scaled visual encoding var yValue2 = d => y2(d.key); //Defining peaktime function peakTime(d) {var i = d3.scan(d.values,(a,b) => yvar(b) - yvar(a)); return d.values[i].time; }; //Sort by peaktime data_nested.sort((a,b) => peakTime(b) - peakTime(a)); //X scale var x = d3.scaleTime() .domain(d3.extent(data, d => d.time)) .range([0, w]); //X axis svg.append("g") .attr("class", "x-axis") .attr("transform", `translate(0, ${h})`) .call(d3.axisBottom() .scale(x) .ticks(3) .tickFormat(formatTime)); //Y2 scale var y2 = d3.scaleBand() .domain(data_nested.map( d => d.key)) .range([0, h]); //Y axis svg.append("g") .attr("class", "y-axis") .call(d3.axisLeft(y2)); //Help varaible var areaHeight = h/y2.domain().length; //Y scale var y = d3.scaleLinear() .domain(d3.extent(data, yvar)) .range([areaHeight,0]); //Area generator var area = d3.area() .x(d => x(d.time)) .y1(d => y(yvar(d))) .y0(y(0)); //Line generator var line = d3.line() .x(d => x(d.time)) .y(d => y(yvar(d))); //Create the small containers var small_svg = svg.append('g') .selectAll('.activity') .data(data_nested) .enter() .append('g') .attr('class', d => 'activity activity--' + d.key) .attr('transform',d => {var ty = yValue2(d)- y2.bandwidth(); return 'translate(0,'+ ty +')';}); //Draw the area small_svg.append('path') .attr('class', 'area') .datum(d => d.values) .attr('d', area); //Draw the line small_svg.append('path') .attr('class', 'line') .datum(d => d.values) .attr('d', line); }); //Data format function prepare(d) {return { activity : d.activity, time : parseTime(d.time), value : +d.p_smooth }; } </script> </body> </html>
https://d3js.org/d3.v4.min.js