Built with blockbuilder.org
xxxxxxxxxx
<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">LINE CHART | MULTI</p>
<div id="link"> by
<a href="https://slides.com/sandravizmad">SANDRA</a> | <a href="https://data.oecd.org/air/air-pollution-exposure.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*/
.x-axis text {
font-family:'Montserrat Alternates', sans-serif;
font-weight:400;
font-size:10px;
opacity:0.5;
fill:white;
}
.y-axis text {
font-family:'Montserrat Alternates', sans-serif;
font-weight:400;
font-size:9px;
opacity:0.5;
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.5;
stroke-opacity:0.5;
stroke:white;
stroke-dasharray:2;
}
/*Defining chart stylings*/
.line {
fill:none;
stroke:#85f6fa;
stroke-width:1px;
}
</style>
</head>
<body>
<script>
//Margin conventions
var m = {top:100, right:50, bottom:50, left:40}
w = 800 - m.left - m.right,
h = 800 - 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})`);
//Define the format
var parseDate = d3.timeParse("%Y");
var formatDate = d3.timeFormat("%Y");
//Data loading
d3.csv(
"https://gist.githubusercontent.com/sandravizz/e324c94814d802e6c268e01266204045/raw/4ed6f4c9554bda1c77004037fbc2a670104a4031/OECD%2520Air%2520pollution%2520by%2520country%2520group%2520and%2520year",
data => {
//Nesting the data by symbol
var country = d3.nest()
.key(d => d.LOCATION)
.entries(data);
//Total list of names
allKeys = country.map(d => d.key)
//Check
console.log(allKeys);
//Parse and caculate some values for each symbols
country.forEach(s => {s.values.forEach(d => {d.Value = +d.Value;
d.TIME = parseDate(d.TIME);
});
s.maxValue = d3.max(s.values, d => d.Value);
s.sumValue = d3.sum(s.values, d => d.Value);
});
//X scale
var x = d3.scaleTime()
.domain(d3.extent(data, d => d.TIME))
.range([0, w]);
//Y scale
var y = d3.scaleLinear()
.domain([0, d3.max(country.map(d => d.maxValue))])
.range([h, 40]);
//Line generator
var line = d3.line()
.defined(function(d) {return d !== null;})
.x(d => x(d.TIME))
.y(d => y(d.Value));
//X axis
svg.append("g")
.attr("class", "x-axis")
.attr("transform", `translate(15, ${h})`)
.call(d3.axisBottom()
.scale(x)
.ticks(10)
.tickPadding(10)
.tickSize(0));
//Y axis
svg.append("g")
.attr("class", "y-axis")
.attr("transform",`translate(0, ${0})`)
.call(d3.axisLeft()
.scale(y)
.tickValues([1, 2, 3, 4, 6, 8, 12, 20, 30, 40])
.tickSize(-w)
.tickPadding(10));
//Draw the lines
svg.selectAll(".line")
.data(country)
.enter()
.append("path")
.attr("class", "line")
.attr("d", d => line(d.values))
});
</script>
</body>
</html>
https://d3js.org/d3.v4.min.js