ExperimetnalAreaGraph
by angjelinhila
HTML
<!DOCTYPE html>
<meta charset="utf-8">
<!-- Load d3.js & color scale-->
<script src="https://d3js.org/d3.v4.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>
<style>
#my_dataviz { width: 100vw; height: 100vh; display: flex; align-items: center; }
</style>
<script>
// set the dimensions and margins of the graph
var margin = {top: 20, right: 30, bottom: 0, left: 20},
width = 1000 - margin.left - margin.right,
height = 900 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
.append("svg").attr("viewBox", `0 0 1000 900`)
// .attr("width", width + margin.left + margin.right)
// .attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Parse the Data
d3.csv("https://gist.githubusercontent.com/Seldomtimely/3d70b91192e185efa1ca892ef41132d4/raw/9baf0af76b4e9a7d0e9d84711f7e8ad7b2d19b7d/gistfile1.txt", function(data) {
// List of groups = header of the csv files
var keys = data.columns.slice(1)
// Add X axis
var x = d3.scaleLinear()
.domain(d3.extent(data, function(d) { return d.year; }))
.range([ 0, width ]);
svg.append("g")
.attr("transform", "translate(0," + height*0.8 + ")")
.call(d3.axisBottom(x).tickSize(-height*.7).tickValues([2030, 2040, 2050, 2060, 2070, 2080, 2090, 2100, 2010, 2020, 2030, 2040, 2050, 2060, 2070, 2080, 2090, 2100, 2110, 2120, 2130, 2140, 2150]))
.select(".domain").remove()
// Customization
svg.selectAll(".tick line").attr("stroke", "#b8b8b8")
// Add X axis label:
svg.append("text")
.attr("text-anchor", "end")
.attr("x", width)
.attr("y", height-100 )
.text("Year");
// Add Y axis
var y = d3.scaleLinear()
.domain([-200000, 200000])
.range([ height,...