Skip to content

Instantly share code, notes, and snippets.

@dyorama
Last active November 15, 2015 18:10
Show Gist options
  • Save dyorama/35b5f12f16964ba1aa8e to your computer and use it in GitHub Desktop.
Save dyorama/35b5f12f16964ba1aa8e to your computer and use it in GitHub Desktop.
Version 6 : Top 20 french cities by natural disasters : type and number per year
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Stacked area chart</title>
<script type="text/javascript" src="//d3js.org/d3.v3.min.js"></script>
<style type="text/css">
body {
margin: 0;
background-color: white;
}
#container {
width: 900px;
margin-left: auto;
margin-right: auto;
display: block;
margin-bottom: 50px;
margin-top: 50px;
padding-top: 20px;
padding-left: 10px;
padding-right: 30px;
padding-bottom: 10px;
background-color: white;
box-shadow: 2px 2px 3px 3px #fcfcfc;
border-width:1px;
}
h1 {
font-size: 24px;
margin: 0;
}
p {
font-size: 14px;
margin: 10px 0 0 0;
}
svg {
background-color: white;
}
rect:hover {
fill: #white;
opacity: 0.6;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis text {
font-family: helvetica;
font-size: 12px;
}
.y.axisbis text {
font-family: helvetica;
font-size: 12px;
}
.y.axisbis path,
.y.axisbis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.legend text {
font-family: helvetica;
font-size: 12px;
}
</style>
</head>
<body>
<div id="container">
</div>
<script type="text/javascript">
//Set up stack method
var stack = d3.layout.stack()
.values(function(d) {
return d.disaster;
});
//.order("reverse");
//Width, height, padding
var w = 900;
var h = 550;
var padding = [ 40, 20, 50, 100 ]; //Top, right, bottom, left
//Set up date format function (année)
var dateFormat = d3.time.format("%Y");
//Set up scales
var xScale = d3.time.scale()
.rangeRound([ padding[3], w - padding[1] ]);
var yScale = d3.scale.linear()
.range([ 0, h - padding[0] - padding[2] ]);
var yScalebis = d3.scale.linear()
.range([ h - padding[0] - padding[2], 0 ]);
//Configure axis generators
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(33)
.tickFormat(function(d) {
return dateFormat(d);
})
.outerTickSize(0);
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(12);
var yAxisbis = d3.svg.axis()
.scale(yScalebis)
.orient("left")
.ticks(4)
.outerTickSize(0);
//Easy colors accessible via a 10-step ordinal scale
var color = d3.scale.ordinal()
.range(["#009ACD", "#2ADF82", "#FFC115", "#006FCD", "#cc310b"]);
//Create the empty SVG image
var svg = d3.select("#container")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load data
d3.csv("stackdisaster.csv", function(data) {
//Uncomment to log the newly loaded data to the console
//console.log(data);
//Data is loaded in, but we need to restructure it.
//Remember, each line requires an array of x/y pairs;
//that is, an array of arrays, like so:
//
// [ [x: 1, y: 1], [x: 2, y: 2], [x: 3, y: 3] ]
//
//Our x value will be the year, and y will be the amount
//of CO2. We also need to know which nom belongs to
//each line, so we will build an array of objects that is
//structured like this:
//
/*
[
{
nom: "Australia",
disaster: [
{ x: "1961", y: 90589.568 },
{ x: "1962", y: 94912.961 },
{ x: "1963", y: 101029.517 },
]
},
{
nom: "Bermuda",
disaster: [
{ x: "1961", y: 176.016 },
{ x: "1962", y: 157.681 },
{ x: "1963", y: 150.347 },
]
},
]
*/
//
//Note that this is an array of objects. Each object
//contains two values, 'nom' and 'disaster'.
//The 'disaster' value is itself an array, containing
//more objects, each one holding x and y values.
//
//The x (year) values have to be strings in this case,
//because the date format function expects a string
//to parse into a Date object.
//New array with all the année, for referencing later
var année = ["1981","1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016"];
//Create a new, empty array to hold ur restructured dataset
var dataset = [];
//Loop once for each row in data
for (var i = 0; i < data.length; i++) {
//Create new object with this nom's name and empty array
dataset[i] = {
nom: data[i].disaster,
disaster: []
};
//Loop through all the année
for (var j = 0; j < année.length; j++) {
//Default value, used in case no value is present
var amount = null;
// If value is not empty
if (data[i][année[j]]) {
amount = +data[i][année[j]];
}
//Add a new object to the disaster data array
//for this nom
dataset[i].disaster.push({
x: année[j],
y: amount,
name: data[i].disaster
});
}
}
//Stack the data!
stack(dataset);
//Uncomment to log the original data to the console
// console.log(data);
//Uncomment to log the newly restructured dataset to the console
console.log(dataset);
//Now that the data is ready, we can check its
//min and max values to set our scales' domains!
xScale.domain([
d3.min(année, function(d) {
return dateFormat.parse(d);
}),
d3.max(année, function(d) {
return dateFormat.parse(d);
})
]);
// Need to recalcluate the max value for yScale
//differently, now that everything is stacked.
//Loop once for each year, and get the total value
//of CO2 for that year.
var totals = [];
for (i = 0; i < année.length; i++) {
totals[i] = 0;
for (j = 0; j < dataset.length; j++) {
totals[i] += dataset[j].disaster[i].y;
}
}
yScale.domain([ 0, d3.max(totals) ]);
yScalebis.domain([ 0, d3.max(totals) ]);
//Areas
//
//Now that we are creating multiple paths, we can use the
//selectAll/data/enter/append pattern to generate as many
//as needed.
//Make a path for each nom
// var paths = svg.selectAll("path")
// .data(dataset)
// .enter()
// .append("path")
// .attr("class", "area")
// .attr("d", function(d) {
// //Calculate path based on only d.disaster array,
// //not all of d (which would include the nom name)
// return area(d.disaster);
// })
// .attr("stroke", "none")
// .attr("fill", function(d, i) {
// return color(i);
// });
//Append a title with the nom name (so we get easy tooltips)
// paths.append("title")
// .text(function(d) {
// return d.nom;
// });
// Add a group for each row of data
var groups = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")
.attr("class", "rect")
.style("fill", function(d, i) {
return color(i);
});
var rects = groups.selectAll("rect")
.data(function(d) { return d.disaster; })
.enter()
.append("rect")
//+ 5 permet de pousser les rectangles de 5 vers la droite
.attr("x", function(d) {
return xScale(dateFormat.parse(d.x)) + 5;
})
.attr("y", function(d) {
//return yScale(d.y); //Flip the math! xxx
return h - yScale(d.y0) - yScale(d.y) - padding[2]; //Flip the math!
})
.attr("width", 15)
.attr("height", function(d) {
return yScale(d.y);
})
// Height : 0 for the transition
// .attr("height", 0)
.append("title").html(function(d, i) {
return d.x + " : " + "<br>" + totals[i] + " natural disasters, " + "<br>" + d.y + " for " + d.name;
});
// TRYING TO ADD A TRANSITION BUT DOESN'T WORK !!!
// rects.transition()
// .delay(function(d, i) {
// return i * 500;
// })
// .duration(5000)
// .attr("height", function(d) {
// return yScale(d.y);
// })
// .on("mouseover", function() { tooltip.style("display", null); })
// .on("mouseout", function() { tooltip.style("display", "none"); })
// .on("mousemove", function(d) {
// //donne la position x de la boite de texte quand la souris passe sur la barre
// var xPosition = d3.mouse(this)[0] - 15;
// //donne la position y de la boite de texte quand la souris passe sur la barre
// var yPosition = d3.mouse(this)[1] - 25;
// // donne la position de la boite de texte, c'est ici qu'il faut donner les coordonnées pour avoir un texte fixe par exemple qui change en fonction de la valeur du graphique à l'endroit où se trouve la souris.
// tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");
// tooltip.select("text").html(d.name + '<p>' + d.y + '</p>');
// });
//Create axes
svg.append("g")
.attr("class", "x axis")
//pousser l'axe des abcsisses de 12 pixels vers la droite
.attr("transform", "translate(12," + (h - padding[2]) + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dy", ".-20em")
.attr("dx", "-8")
.attr("transform", function(d) {return "rotate(-70)"});
svg.append("text")
.attr ("x", 120)
.attr ("y", (padding[0] / 3))
.style("font-size", "14px")
.style("font-family", "Helvetica")
.style("font-weight", "none")
.style("font-style", "italic")
.text(" Type and number of natural disasters in top 15 cities of Alpes-Maritimes ");
svg.append("g")
.attr("class", "y axisbis")
.attr("transform", "translate(" + padding[3] + "," + padding[0] + ")")
.call(yAxisbis);
var legend = svg.selectAll(".legend")
.data(color.domain().slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 25 + ")"; });
legend.append("rect")
.attr("x", w - 150)
.attr("width", 15)
.attr("height", 15)
.style("fill", color);
legend.append("text")
.attr("x", w - 130)
.attr("y", 9)
.attr("dy", ".35em")
.text(function(d, i) {
switch (i) {
case 0: return "Earthquake";
case 1: return "Waves";
case 2: return "Drought";
case 3: return "Landslides";
case 4: return "Floodings";
}
});
// var tooltip = svg.append("g")
// .attr("class", "tooltip")
// .style("display", "none");
// tooltip.append("rect")
// .attr("width", 30)
// .attr("height", 20)
// .attr("fill", "white")
// .style("opacity", 0.5);
// tooltip.append("text")
// .attr("x", 15)
// .attr("dy", "1.9em")
// .style("text-anchor", "middle")
// .style ("white-space","rap")
// .attr("font-size", "12px")
// .attr("font-weight", "bold");
});
</script>
</body>
</html>
disaster 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
Floodings 0 3 0 0 0 15 0 3 3 4 16 15 24 1 15 2 10 14 45 0 4 8 5 18 3 0 1 14 30 16 10 3 22 8
Landslides 1 0 0 0 0 0 0 0 0 0 1 4 14 3 14 4 5 8 37 0 1 0 0 2 0 0 9 4 3 5 1 9 16 0
Drought 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 11 12 6 2 15 0 0 0 0 0 0 0 0
Waves 0 0 0 0 0 0 0 3 0 1 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0
Earthquake 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment