D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
sandravizz
Full window
Github gist
5.6 bar chart | update
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <!-- 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">ANIMATION | UPDATE</p> <div id="link"> by <a href="https://slides.com/sandravizmad">SANDRA</a></div> <!-- Creating the buttons --> <button id="start" style="position: absolute; margin-left:20px; margin-top:40px;">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 button stylings*/ #start, #reset { font-family:'Montserrat Alternates', sans-serif; font-size:14px; color:WHITE; border:0; line-height:1; text-align:center; background-color:#011227; } #start:hover, #reset:hover { color:#f5fa91; background-color:none; } /*Defining chart stylings*/ .bars { fill:#8ff798; } #lables { font-family:'Montserrat Alternates', sans-serif; font-size:12px; font-variant:small-caps slashed-zero; fill:white; text-anchor:middle; } </style> <script> //Margin conventions var m = {top:100, right:50, bottom:50, left:40} w = 800 - 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})`); //Dataset as an array var data=[5, 10, 13, 19, 21, 25, 22, 18, 15, 13, 11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ]; //X scale var x = d3.scaleBand() .domain(d3.range(data.length)) .rangeRound([0, w]) .paddingInner(0.05); //Y scale var y = d3.scaleLinear() .domain([0, d3.max(data)]) .range([h, 0]); //Draw the bars svg.selectAll("rect") .data(data) .enter() .append("rect") .attr("x", (d,i) => x(i)) .attr("y", d => y(d)) .attr("width", x.bandwidth()) .attr("height",d => h - y(d)) .attr("class", "bars"); //Add the lables svg.selectAll("text") .data(data) .enter() .append("text") .text(d=>d) .attr("x",(d,i)=> x(i)+x.bandwidth()/2) .attr("y",d=> y(d)-10) .attr("id","lables"); // Animation by click d3.select("#start") .on("click",function() { //New dataset data =[11, 12, 15, 20, 18, 17, 16, 18, 23, 25, 5, 10, 13, 19, 21, 25, 22, 18, 15, 13]; //Redraw the bars svg.selectAll("rect") .data(data) .attr("y",d => y(d)) .attr("height",d => h-y(d)); //Re-add the lables svg.selectAll("text") .data(data) .text(d => d) .attr("x",(d,i) => x(i) + x.bandwidth()/2) .attr("y",d => y(d) - 10); }); </script> </body> </html>
https://d3js.org/d3.v4.min.js