Built with blockbuilder.org
xxxxxxxxxx
<html lang="en">
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<!-- Google fonts Second 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">BAR CHART | UPDATE</p>
<div id="link">by
<a href="https://slides.com/sandravizmad">SANDRA</a> | <a href="https://data.oecd.org/biodiver/built-up-area.htm#indicator-chart">DATA</a></div>
<!-- Create the starting button -->
<button id="start" style="position: absolute; margin-left:20px; margin-top:60px;">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:12px;
color:WHITE;
border:0;
line-height:1;
text-align:center;
background-color:#011227;
}
#start:hover, #reset:hover {
color:#f5fa91;
background-color:NONE;
}
/*Defining axis stylings*/
.x-axis text {
font-family:'Montserrat Alternates', sans-serif;
font-weight:400;
font-size:9px;
opacity:1;
fill:white;
}
.y-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;
stroke-opacity:1;
stroke:white;
stroke-dasharray:2;
}
/*Defining chart stylings*/
.bar {
fill-opacity:0.8;
}
</style>
<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 loading
d3.csv(
'https://gist.githubusercontent.com/sandravizz/d0b1e5419df723b600c872b517458193/raw/e8d72fc67ea127b7e65eb5ce4319c6c2018c3799/OECD%2520BUILT%2520UP%2520AREA%2520by%2520country%25202000',
prepare,
data => {
//Sort data by value
data.sort((b, a) => a.Value - b.Value);
//Check
console.log(data);
//Color scale
var c = d3.scaleLinear()
.domain([0, 550])
.range(["#85f6fa", "#8ff798"]);
//X scale
var x = d3.scaleBand()
.domain(data.map( d => d.LOCATION))
.rangeRound([0, w])
.paddingInner(0.04);
//X axis
svg.append("g")
.attr("class", "x-axis")
.attr("transform", `translate(0, ${h})`)
.call(d3.axisBottom()
.scale(x))
.selectAll("text")
.attr("y", 6)
.attr("x", 0)
.attr("transform", "rotate(35)")
.style("text-anchor", "start");
//Y scale
var y = d3.scaleLinear()
.domain([0, 550])
.rangeRound([h, 0]);
//Y axis
svg.append("g")
.attr("class", "y-axis")
.call(d3.axisLeft()
.scale(y)
.ticks(5));
//Draw the bars
svg.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr('class', 'bar')
.attr("x", d => x(d.LOCATION))
.attr("width", x.bandwidth())
.attr("y", d => y(+d.Value))
.attr("height", d => h - y(d.Value))
.attr("fill", d => c(d.Value))
});
//Make sure all variables are numbers
function prepare (d) {
d.Value = +d.Value;
return d;
}
// Animation by click
d3.select("#start")
.on("click", function() {
//Data loading
d3.csv(
'https://gist.githubusercontent.com/sandravizmad/8e50af53c013d57ce3cdb4c07a42f6e0/raw/d983b9517212f95036db77a8396c5a61f2493e3d/OECD%2520BUILT%2520UP%2520AREA%2520by%2520country%25202014',
prepare,
data => {
//Sort data by value
data.sort((b, a) => {return a.Value - b.Value;});
//Color scale
var c = d3.scaleLinear()
.domain([0, 550])
.range(["#85f6fa", "#8ff798"]);
//X scale
var x = d3.scaleBand()
.domain(data.map( d => d.LOCATION))
.rangeRound([0, w])
.paddingInner(0.05);
//Transition of the x axis
svg.select(".x-axis")
.transition()
.duration(1000)
.call(d3.axisBottom()
.scale(x)
.ticks(5))
.attr("transform", `translate(0, ${h})`);
//Y scale
var y = d3.scaleLinear()
.domain([0, 550])
.rangeRound([h, 0]);
//Draw the bars
svg.selectAll('rect')
.data(data)
.transition()
.duration(2000)
.delay(1000)
.attr("y", d => y(d.Value))
.attr("height", d => h - y(d.Value))
.attr("fill", d => c(d.Value));
})
//Make sure all variables are numbers
function prepare (d) {
d.Value = +d.Value;
return d;
}
});
</script>
</body>
</html>
https://d3js.org/d3.v4.min.js
https://d3js.org/d3.v4.min.js