D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
sandravizz
Full window
Github gist
7.9 animated line plot | new dataset
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.v3.min.js"></script> <!-- Creating the headlines --> <p id="h1">LINE CHART | ANIMATION </p> <div id="link">by <a href="https://slides.com/sandravizmad">SANDRA</a></div> <!-- Create the starting button --> <button id="start" style="position: absolute; margin-left:12px; margin-top:50px;">START</button> <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: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:1; stroke-opacity:1; stroke:white; stroke-dasharray:2; } /*Defining chart stylings*/ .line { fill:none; stroke:#85f6fa; stroke-width:2px; } /*Defining button stylings*/ #start, #reset { font-family:'Montserrat Alternates', sans-serif; font-size:12px; color:white; border:0; text-align:center; background-color:#011227; } #start:hover, #reset:hover { color:#f5fa91; background-color:#011227; } </style> </head> <body> <script> //Margin conventions var m = {top:100, right:50, bottom:50, left:50} w = 600 - m.left - m.right, h = 600 - 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 parseDate = d3.time.format("%d-%b-%y").parse; //Data loading d3.csv( "https://gist.githubusercontent.com/sandravizz/17951d49ca9999fb872ef1edb769889c/raw/5d6800bfd272edd42fa017846df1cadefa02098a/Example-data2", data => { data.forEach( d => { d.date = parseDate(d.date); d.close = +d.close; }); //X scale var x = d3.time.scale() .domain(d3.extent(data, d => d.date)) .range([0, w]); //Y scale var y = d3.scale.linear() .domain([0, d3.max(data, d => d.close)]) .range([h, 0]); //Line generator var line = d3.svg.line() .x(d => x(d.date)) .y(d => y(d.close)); //Draw the line svg.append("path") .attr("class", "line") .attr("d", line(data)); //X axis svg.append("g") .attr("class", "x-axis") .attr("transform", `translate(0, ${h})`) .call(d3.svg.axis() .scale(x) .orient("bottom") .ticks(5) .tickSize(0) .tickPadding(10)); //Y axis svg.append("g") .attr("class", "y-axis") .call(d3.svg.axis() .scale(y) .orient("left") .ticks(",.0s") .tickValues([50, 100, 600, 650]) .tickSize(-w) .tickPadding(10)); }); //Start Animation on Click d3.select("#start") .on("click", () => { //Data loading d3.csv( "https://gist.githubusercontent.com/sandravizz/44ecd62a430f8adf1065722964262406/raw/8cd127433fddb63cc4ecea3d4fac532297dd6a30/Test-dataset", data => { data.forEach(d => { d.date = parseDate(d.date); d.close2 = +d.close2; }); //X scale var x = d3.time.scale() .domain(d3.extent(data, d => d.date)) .range([0, w]); //Y scale var y = d3.scale.linear() .domain([0, d3.max(data, d => d.close2)]) .range([h, 0]); //New line genrator var lineNew = d3.svg.line() .x(d => x(d.date)) .y(d => y(d.close2)); //Select the section where we apply the update var svg = d3.select("body") .transition(); //Drawing again the path svg.select(".line") .duration(1000) .attr("d", lineNew(data)); //X axis svg.select(".x-axis") .duration(2000) .call(d3.svg.axis() .scale(x) .orient("bottom") .ticks(5) .tickSize(0) .tickPadding(10)); //Y axis svg.select(".y-axis") .duration(2000) .call(d3.svg.axis() .scale(y) .orient("left") .ticks(5) .tickSize(-w) .tickPadding(10)); }) }); </script> </body> </html>
https://d3js.org/d3.v3.min.js