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.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>
<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:0;
stroke-opacity:1;
stroke:white;
stroke-dasharray:2;
}
/*Defining chart stylings*/
.line {
fill:none;
stroke:white;
stroke-width: 1px;
}
.horizontalGrid {
fill:none;
stroke:white;
stroke-width:0.5px;
stroke-dasharray: 2;
}
</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})`);
//Date format
var parseDate = d3.time.format("%Y-%m-%d").parse;
//Data loading
d3.tsv(
"https://gist.githubusercontent.com/sandravizz/3dc4d165961256625f0baff2664aaa2d/raw/7db03d6473a0cc409225d2bfc52e71a80f6a97b2/airbus%2520data",
data => {
//Color scale
var c = d3.scale.category10()
.domain(d3.keys(data[0])
.filter(function(key) {return key !== "date";}));
//Data format
data.forEach(d => {d.date = parseDate(d.date);});
var comp = c.domain()
.map(function(name) { return {
name : name,values : data.map(d=> { return {
date : d.date,price : +d[name]};})
};
});
//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([
d3.min(comp, c => d3.min(c.values, v => v.price)),
d3.max(comp, c => d3.max(c.values, v => v.price))])
.range([h, 0]);
//Line genrator
var line = d3.svg.line()
.interpolate("basis")
.x(d => x(d.date))
.y(d => y(d.price));
//Calling X axis
svg.append("g")
.attr("class", "x-axis")
.attr("transform", `translate(0, ${h})`)
.call(d3.svg.axis()
.scale(x)
.ticks(5)
.innerTickSize(2)
.outerTickSize(0.5)
.orient("bottom"));
//Calling Y axis
svg.append("g")
.attr("class", "y-axis")
.call(d3.svg.axis()
.scale(y)
.tickFormat(d => d+"%")
.ticks(5)
.innerTickSize(2)
.outerTickSize(0.5)
.orient("left"));
//Reference line
svg.append("line")
.attr({"class":"horizontalGrid",
"x1" : 0,
"x2" : w,
"y1" : y(0),
"y2" : y(0)});
var company = svg.selectAll(".company")
.data(comp)
.enter()
.append("g")
.attr("class", "company");
//Drawing the line
var p = svg.selectAll(".company")
.append("path")
.attr("class", "line")
.attr("d", d => line(d.values))
.style("stroke", d => {if(d.name == "Airbus") { return "#8ff798" } else { return "#85f6fa";}});
//Preparation for the animation
var totalL = [p[0][0].getTotalLength(),
p[0][1].getTotalLength()];
//Animation of the first line
d3.select(p[0][0])
.attr("stroke-dasharray", totalL[0] +" "+ totalL[0])
.attr("stroke-dashoffset", totalL[0])
.transition()
.duration(20000)
.ease("linear")
.attr("stroke-dashoffset", 0);
//Animation of the second line
d3.select(p[0][1])
.attr("stroke-dasharray", totalL[1] +" "+ totalL[1])
.attr("stroke-dashoffset", totalL[1])
.transition()
.duration(20000)
.ease("linear")
.attr("stroke-dashoffset", 0);
});
</script>
</body>
</html>
https://d3js.org/d3.v3.min.js