Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dyorama/d2570764d0b94150d2a559799f5c150b to your computer and use it in GitHub Desktop.
Save dyorama/d2570764d0b94150d2a559799f5c150b to your computer and use it in GitHub Desktop.
Natural disasters: where are the most vulnerable cities in France?
//60 % des arrêtés pour inondations dans les 15 communes
var w = 650;
var h = 350;
var padding = [ 35, 30, 30, 160 ]; //Top, right, bottom, left
var widthScale = d3.scale.linear()
.range([ 0, w - padding[1] - padding[3] ]);
//le 0.15 définit la taille la hauteur des barres, le changer pour avoir des barres + ou - larges
var heightScale = d3.scale.ordinal()
.rangeRoundBands([ padding[0], h - padding[2] ], 0.29);
var xAxis = d3.svg.axis()
.scale(widthScale)
.orient("bottom")
.tickFormat(function(d) { return d; })
.ticks(6)
.outerTickSize(0);
var yAxis = d3.svg.axis()
.scale(heightScale)
.orient("left");
//Now SVG goes into #container instead of body
var svg = d3.select(".wpd3-71-1")
.append("svg")
.attr("width", w)
.attr("height", h);
//Easy colors accessible via a 10-step ordinal scale
var color = d3.scale.ordinal()
.range(["rgba(0, 154, 205, 0.7)", "#2ADF82", "#FFC115", "#006FCD", "#cc310b"]);
d3.csv("http://localhost/datavizexplorer/datavizexplorer/wordpress/wp-content/uploads/2015/12/15perils.csv", function(data) {
data.sort(function(a, b) {
return d3.descending(+a.catastrophes, +b.catastrophes);
});
widthScale.domain([ 0, d3.max(data, function(d) {
return +d.catastrophes;
}) ]);
heightScale.domain(data.map(function(d) { return d.perils; } ));
//Bind data to groups (not bars directly)
var groups = svg.selectAll("g")
.data(data)
.enter()
.append("g")
.attr("class", "bar");
var div = d3.select("body").append("div")
.attr("class", "tooltip");
//Add a rect to each group
var rects = groups.append("rect")
.attr("x", padding[3])
.attr("y", function(d) {
return heightScale(d.perils);
})
.attr("width", 0)
.attr("height", heightScale.rangeBand())
.style("fill", function(d, i) {
return color(i);
})
//Add a text element to each group
// groups.append("title")
// .text(function(d) {
// return d.catastrophes + " arrêtés depuis 1982" ;
// });
.on("mousemove", function(d) {
var xPosition = parseFloat(d3.select(this).attr("cx"));
var yPosition = parseFloat(d3.select(this).attr("cy"));
var currentState = this;
d3.select(this).style('fill-opacity', 1);
//Update the tooltip position and value
d3.select(".tooltip")
.style("left", (d3.event.pageX+20) + "px")
.style("top", (d3.event.pageY ) + "px")
.style("font-family", "sans-serif")
.style("font-size", "14px")
.html( d.catastrophes + " arrêtés pour " + d.nom + "<br/>" + " depuis 1982" + " soit " + d.percent + " % ");
// //Show the tooltip
d3.select(".tooltip").classed("hidden", false);
})
.on("mouseout", function() {
d3.selectAll("rect")
.style("fill-opacity",10);
//Hide the tooltip
d3.select("div.tooltip").classed("hidden", true);
});
rects.transition()
.delay(function(d, i) {
return i * 50;
})
.duration(2000)
.attr("width", function(d) {
return widthScale(d.catastrophes);
})
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + padding[3] + ",0)")
.call(yAxis);
svg.append("text")
.attr ("x", 30)
.attr ("y", (padding[0] / 2))
.style("font-size", "14px")
.style("font-family", "Helvetica")
.style("font-weight", "none")
.style("font-style", "italic")
.text("Total des arrêtés dans les 15 communes par type de péril depuis 1982");
});
perils catastrophes nom percent
Inondations 312 inondations 60
Mouvements de terrain 141 mouvements de terrain 27
Sécheresse 48 sécheresse 9
Dégâts dus aux vagues 18 dégâts dus aux vagues 3
Séisme 2 séisme 0.4
// Des dégâts plus fréquents depuis les années 1990
//Set up stack method
var stack = d3.layout.stack()
.values(function(d) {
return d.disaster;
});
//.order("reverse");
//Width, height, padding
var w = 750;
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(["rgba(0, 154, 205, 0.7)", "#2ADF82", "#FFC115", "#006FCD", "#cc310b"]);
//Create the empty SVG image
var svg = d3.select(".wpd3-71-3")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load data
d3.csv("http://localhost/datavizexplorer/datavizexplorer/wordpress/wp-content/uploads/2015/11/stackperils.csv", function(data) {
//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) ]);
// 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);
});
// Define the div for the tooltip
var div = d3.select("body").append("div")
.attr("class", "tooltip");
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)
.on("mousemove", function(d,i) {
var xPosition = parseFloat(d3.select(this).attr("cx"));
var yPosition = parseFloat(d3.select(this).attr("cy"));
var currentState = this;
d3.select(this).style('fill-opacity', 1);
//Update the tooltip position and value
d3.select("div.tooltip")
.style("left", (d3.event.pageX+20) + "px")
.style("top", (d3.event.pageY ) + "px")
.html("<b>" + d.x + "</b>" + "<br/>" + totals[i] + " arrêtés" + "<br/>" + d.y + " pour " + d.name);
// //Show the tooltip
d3.select("div.tooltip").classed("hidden", false);
})
.on("mouseout", function() {
d3.selectAll("rect")
.style("fill-opacity",10);
//Hide the tooltip
d3.select("div.tooltip").classed("hidden", true);
});
//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("Cumul des arrêtés par année dans les 15 communes depuis 1982");
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("class", "mois")
.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 "Séisme";
case 1: return "Dégâts dus aux vagues";
case 2: return "Sécheresse";
case 3: return "Mouvements de terrain";
case 4: return "Inondations";
}
});
// 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");
});
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
inondations 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
mouvements de terrain 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
sécheresse 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
dégâts dus aux vagues 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
séisme 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
// L'automne, saison des inondations //
//Set up stack method
var stack = d3.layout.stack()
.values(function(d) {
return d.disaster;
});
//.order("reverse");
//Width, height, padding
var w = 750;
var h = 450;
var padding = [ 40, 50, 60, 70 ]; //Top, right, bottom, left
//Set up date format function (année)
var dateFormat = d3.time.format("%m");
//Set up scales
//le - 40 permet de garder le graphique à l'échelle sinon le mois de décembre disparaissait !
var xScale = d3.time.scale()
.rangeRound([ padding[3], w - padding[1] - 40 ]);
//
var yScale = d3.scale.linear()
.range([ 0, h - padding[0] - padding[2] ]);
var yScalebis = d3.scale.linear()
.range([ h - padding[0] - padding[2], 0 ]);
// var nomdesmois = [
// {"mois": "Janvier"},
// {"mois": "Février"},
// {"mois": "Mars"},
// {"mois": "Avril"},
// {"mois": "Mai"},
// {"mois": "Juin"},
// {"mois": "Juin"},
// {"mois": "Août"},
// {"mois": "Septembre"},
// {"mois": "Octobre"},
// {"mois": "Novembre"},
// {"mois": "Décembre"},
// ];
// console.log(nomdesmois);
// // var nomdesmois = ["mois":"Janvier","Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"];
// svg.select("nomdesmois")
// .data("nomdesmois")
// .enter()
// .append("text")
// .text(function(d) { return d; });
//Configure axis generators
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(12)
.tickFormat(function(d) { return d.mois });
// svg.append("text")
// .attr ("x", 120)
// .attr ("y", (padding[0] / 3))
// .style("font-size", "12px")
// .style("font-family", "Helvetica")
// .style("font-weight", "none")
// .style("font-style", "italic")
// .text("Nombre d'arrêtés dans les 15 communes selon l'aléa naturel ");
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(12);
var yAxisbis = d3.svg.axis()
.scale(yScalebis)
.orient("left")
.ticks(6)
.outerTickSize(0);
//Easy colors accessible via a 10-step ordinal scale
var color = d3.scale.ordinal()
.range(["rgba(0, 154, 205, 0.7)", "#2ADF82", "#FFC115", "#006FCD", "#cc310b"]);
//Create the empty SVG image
var svg = d3.select(".wpd3-71-2")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load data
d3.csv("http://localhost/datavizexplorer/datavizexplorer/wordpress/wp-content/uploads/2015/11/stackmois.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 = ["01","02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];
// var nomdesmois = ["Janvier","Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"];
//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) + 30 ]);
yScalebis.domain([ 0, d3.max(totals) + 30 ]);
//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);
});
// Define the div for the tooltip
var div = d3.select("body").append("div")
.attr("class", "tooltip");
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)) + 36;
})
.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", 30)
.attr("height", function(d) {
return yScale(d.y);
})
// Height : 0 for the transition
// .attr("height", 0)
// .append("title").html(function(d, i) {
// return totals[i] + " arrêtés, " + "<br>" + d.y + " pour " + d.name;
// });
// Height : 0 for the transition
// .attr("height", 0)
.on("mousemove", function(d,i) {
var xPosition = parseFloat(d3.select(this).attr("cx"));
var yPosition = parseFloat(d3.select(this).attr("cy"));
var currentState = this;
d3.select(this).style('fill-opacity', 1);
//Update the tooltip position and value
d3.select("div.tooltip")
.style("left", (d3.event.pageX+20) + "px")
.style("top", (d3.event.pageY ) + "px")
.html( totals[i] + " arrêtés" + "<br/>" + d.y + " pour " + d.name)
// //Show the tooltip
d3.select("div.tooltip").classed("hidden", false);
})
.on("mouseout", function() {
d3.selectAll("rect")
.style("fill-opacity",10);
//Hide the tooltip
d3.select("div.tooltip").classed("hidden", true);
});
// 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(50," + (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", 70)
.attr ("y", (padding[0] / 3))
.style("font-size", "14px")
.style("font-family", "Helvetica")
.style("font-weight", "none")
.style("font-style", "italic")
.text("Cumul des arrêtés par mois dans les 15 communes depuis 1982 ");
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")
//le 20 donne l'espacement entre entre les textes
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", w - 670)
.attr("width", 10)
.attr("y", 40)
.attr("height", 10)
.style("fill", color);
legend.append("text")
.attr("class", "legend")
.attr("x", w - 655)
.attr("y", 45)
.attr("dy", ".35em")
.text(function(d, i) {
switch (i) {
case 0: return "Séisme";
case 1: return "Dégâts dus aux vagues";
case 2: return "Sécheresse";
case 3: return "Mouvements de terrain";
case 4: return "Inondations";
}
});
// 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");
svg.append("text")
.attr("class", "mois")
.attr ("x", 745)
.attr ("y", 407)
// .attr("transform", "translate(140,407) rotate(-70)")
.text("Décembre");
svg.append("text")
.attr("class", "mois")
.attr ("x", 685)
.attr ("y", 407)
// .attr("transform", "translate(140,407) rotate(-70)")
.text("Novembre");
svg.append("text")
.attr("class", "mois")
.attr ("x", 625)
.attr ("y", 407)
// .attr("transform", "translate(140,407) rotate(-70)")
.text("Octobre");
svg.append("text")
.attr("class", "mois")
.attr ("x", 577)
.attr ("y", 407)
// .attr("transform", "translate(140,407) rotate(-70)")
.text("Septembre");
svg.append("text")
.attr("class", "mois")
.attr ("x", 505)
.attr ("y", 407)
// .attr("transform", "translate(140,407) rotate(-70)")
.text("Août");
svg.append("text")
.attr("class", "mois")
.attr ("x", 455)
.attr ("y", 407)
// .attr("transform", "translate(140,407) rotate(-70)")
.text("Juillet");
svg.append("text")
.attr("class", "mois")
.attr ("x", 398)
.attr ("y", 407)
// .attr("transform", "translate(140,407) rotate(-70)")
.text("Juin");
svg.append("text")
.attr("class", "mois")
.attr ("x", 343)
.attr ("y", 407)
// .attr("transform", "translate(140,407) rotate(-70)")
.text("Mai");
svg.append("text")
.attr("class", "mois")
.attr ("x", 291)
.attr ("y", 407)
// .attr("transform", "translate(140,407) rotate(-70)")
.text("Avril");
svg.append("text")
.attr("class", "mois")
.attr ("x", 240)
.attr ("y", 407)
// .attr("transform", "translate(140,407) rotate(-70)")
.text("Mars");
svg.append("text")
.attr("class", "mois")
.attr ("x", 195)
.attr ("y", 407)
// .attr("transform", "translate(140,407) rotate(-70)")
.text("Février");
svg.append("text")
.attr("class", "mois")
.attr ("x", 140)
.attr ("y", 407)
// .attr("transform", "translate(140,407) rotate(-70)")
.text("Janvier");
// .attr("transform","translate("+width/2+","+height/2+")rotate("+rotate+")");
});
disaster 01 02 03 04 05 06 07 08 09 10 11 12
inondations 31 10 0 1 9 11 0 11 44 86 80 29
mouvements de terrain 30 2 5 1 2 2 0 0 6 30 33 30
sécheresse 19 0 1 0 1 0 27 0 0 0 0 0
dégâts dus aux vagues 0 3 0 0 0 0 0 0 1 0 4 10
séisme 0 0 0 2 0 0 0 0 0 0 0 0
<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis.net/kml/2.2'>
<Document>
<name>Top 20 des villes catastrophes naturelles</name>
<description> | Created with http://batchgeo.com</description>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>1 : 63 arrêtés</value>
</Data>
</ExtendedData>
<address>Nice</address>
<Point>
<coordinates>7.2619532,43.7101728,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>2 : 48 arrêtés</value>
</Data>
</ExtendedData>
<address>Cagnes-sur-Mer</address>
<Point>
<coordinates>7.14882,43.663739,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>3 : 46 arrêtés</value>
</Data>
</ExtendedData>
<address>Antibes</address>
<Point>
<coordinates>7.125102,43.580418,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>4 : 40 arrêtés</value>
</Data>
</ExtendedData>
<address>Cannes</address>
<Point>
<coordinates>7.017369,43.552847,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>5 : 39 arrêtés</value>
</Data>
</ExtendedData>
<address>Marseille</address>
<Point>
<coordinates>5.36978,43.296482,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>6 : 38 arrêtés</value>
</Data>
</ExtendedData>
<address>Èze</address>
<Point>
<coordinates>7.361792,43.7278189,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>7 : 38 arrêtés</value>
</Data>
</ExtendedData>
<address>Villeneuve-Loubet</address>
<Point>
<coordinates>7.1086040898438,43.749228123257,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>8 : 36 arrêtés</value>
</Data>
</ExtendedData>
<address>Contes</address>
<Point>
<coordinates>7.3177499999999,43.815268,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>9 : 36 arrêtés</value>
</Data>
</ExtendedData>
<address>Falicon</address>
<Point>
<coordinates>7.279002,43.749318,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>10 : 35 arrêtés</value>
</Data>
</ExtendedData>
<address>Toulouse</address>
<Point>
<coordinates>1.444209,43.604652,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>11 : 33 arrêtés</value>
</Data>
</ExtendedData>
<address>Bordeaux</address>
<Point>
<coordinates>-0.57917999999995,44.837789,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>12 : 32 arrêtés</value>
</Data>
</ExtendedData>
<address>Saint-Laurent-du-Var</address>
<Point>
<coordinates>7.189811,43.673417,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>13 : 31 arrêtés</value>
</Data>
</ExtendedData>
<address>Grasse</address>
<Point>
<coordinates>6.9264920000001,43.660153,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>14 : 28 arrêtés</value>
</Data>
</ExtendedData>
<address>Menton</address>
<Point>
<coordinates>7.4975400000001,43.774481,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>15 : 28 arrêtés</value>
</Data>
</ExtendedData>
<address>Mandelieu-la-Napoule</address>
<Point>
<coordinates>6.938309,43.546232,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>16 : 28 arrêtés</value>
</Data>
</ExtendedData>
<address>Mougins</address>
<Point>
<coordinates>7.006491,43.6023319,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>17 : 27 arrêtés</value>
</Data>
</ExtendedData>
<address>Castres</address>
<Point>
<coordinates>2.241295,43.606214,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>18 : 27 arrêtés</value>
</Data>
</ExtendedData>
<address>Vallauris</address>
<Point>
<coordinates>7.055041,43.577244,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>19 : 26 arrêtés</value>
</Data>
</ExtendedData>
<address>Castelsarrasin</address>
<Point>
<coordinates>1.107132,44.039889,0</coordinates>
</Point>
</Placemark>
<Placemark>
<styleUrl>#0</styleUrl>
<ExtendedData>
<Data name='Catastrophes naturelles'>
<value>20 : 25 arrêtés</value>
</Data>
</ExtendedData>
<address>Peillon</address>
<Point>
<coordinates>7.3821820000001,43.777924,0</coordinates>
</Point>
</Placemark>
<Style id="0"><IconStyle><scale>1</scale><Icon><href>http://batchgeo.com/images/icons/blue_shadow_Marker.png</href></Icon><hotSpot x=".3" y=".8" xunits="fraction" yunits="fraction" /></IconStyle><LabelStyle><scale>0</scale></LabelStyle></Style> </Document>
</kml>
// Nice, capitale des catastrophes naturelles
var w = 650;
var h = 500;
var padding = [ 35, 30, 30, 170 ]; //Top, right, bottom, left
var widthScale = d3.scale.linear()
.range([ 0, w ]);
var widthScale = d3.scale.linear()
.range([ 0, w - padding[1] - padding[3] ]);
var heightScale = d3.scale.ordinal()
.rangeRoundBands([ padding[0], h - padding[2] ], 0.1);
var xAxis = d3.svg.axis()
.scale(widthScale)
.orient("bottom")
.tickFormat(function(d) { return d; })
.ticks(5)
.outerTickSize(0);
var yAxis = d3.svg.axis()
.scale(heightScale)
.orient("left");
var svg = d3.select(".wpd3-71-0")
.append("svg")
.attr("width", w)
.attr("height", h);
d3.csv("http://localhost/datavizexplorer/datavizexplorer/wordpress/wp-content/uploads/2015/11/20villes.csv", function(data) {
data.sort(function(a, b) {
return d3.descending(+a.catastrophes, +b.catastrophes);
});
widthScale.domain([ 0, d3.max(data, function(d) {
return +d.catastrophes;
}) ]);
heightScale.domain(data.map(function(d) { return d.département; } ));
//Bind data to groups (not bars directly)
var groups = svg.selectAll("g")
.data(data)
.enter()
.append("g")
.attr("class", "bar");
//Add a rect to each group
var rects = groups.append("rect")
.attr("x", padding[3])
.attr("y", function(d) {
return heightScale(d.département);
})
.attr("width", 0)
.attr("height", heightScale.rangeBand())
.attr("fill", "#0B74C7");
//Add a text element to each group
groups.append("text")
.attr("x", 180)
.attr("y", function(d) {
return heightScale(d.département) + 15;
})
.text(function(d) {
return d.catastrophes + " arrêtés depuis 1982" ;
});
rects.transition()
.delay(function(d, i) {
return i * 0;
})
.duration(7000)
.attr("width", function(d) {
return widthScale(d.catastrophes);
})
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + padding[3] + ",0)")
.call(yAxis);
svg.append("text")
.attr ("x", 30)
.attr ("y", (padding[0] / 2))
.style("font-size", "14px")
.style("font-family", "Helvetica")
.style("font-weight", "none")
.style("font-style", "italic")
.text("20 premières villes françaises selon le nombre d'arrêtés de catastrophe naturelle depuis 1982");
});
villes catastrophes département
Nice 63 Nice (06)
Cagnes-sur-Mer 48 Cagnes-sur-Mer (06)
Antibes 46 Antibes (06)
Cannes 40 Cannes (06)
Marseille 39 Marseille (13)
Èze 38 Èze (06)
Villeneuve-Loubet 38 Villeneuve-Loubet (06)
Contes 36 Contes (06)
Falicon 36 Falicon (06)
Toulouse 35 Toulouse (31)
Bordeaux 33 Bordeaux (33)
Saint-Laurent-du-Var 32 Saint-Laurent-du-Var (06)
Grasse 31 Grasse (06)
Mandelieu-la-Napoule 28 Mandelieu-la-Napoule (06)
Menton 28 Menton (06)
Mougins 28 Mougins (06)
Castres 27 Castres (81)
Vallauris 27 Vallauris (06)
Castelsarrasin 26 Castelsarrasin (82)
Peillon 25 Peillon (06)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment