xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Cleaning Up</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: Helvetica, Arial, sans-serif;
}
h1 {
font-size: 40px;
margin: 0;
}
p {
font-size: 13px;
margin: 20px 20 0 0;
}
svg {
background-color: white;
}
circle:hover {
fill: steelblue;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 13px;
}
.y.axis path,
.y.axis line {
opacity: 20;
}
</style>
</head>
<body>
<h1> Top 20 countries by forest </h1>
<p> % of the area cover by woodland and total area of forest in km<SUP>2</SUP> </p>
<script type="text/javascript">
// set width and height of viz, plus padding
var w = 700;
var h = 600;
var padding = [ 10, 50, 20, 50 ]; //Top, right, bottom, left
// make width scale, set as linear (to hold numbers). Subtract padding
var xScale = d3.scale.linear()
.range([ padding[3], w - padding[1] ]);
// make height scale, set as ordinal (eg categories). Subtract padding.
var yScale = d3.scale.linear()
.range([ padding[0], h - padding[2] ]);
// create horizontal axis and place it on the bottom
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(4)
.tickFormat(function(d) {
return d + " km2";
});
// create veritcal axis and place it on the left
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.tickFormat(function(d) {
return d + " %";
});
// create a holder for the SVG in the body, give it the width and height set earlier
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
// call the data
d3.csv("forestdata.csv", function(data) {
// sort the data, descending
data.sort(function(a, b) {
return d3.descending(+a.forest2012Km2, +b.forest2012Km2);
});
// make axis width scalable to biggest value
xScale.domain([ 0, 8000000 ]);
// make axis height scalable to biggest value
yScale.domain([ 70, 0 ]);
// create a circleangle for each line of data
var circles = svg.selectAll("circle")
.data(data)
.enter()
.append("circle");
// add padding between bars. set fill color. assemble tooltip
circles.attr("cx", function(d) {
return xScale(d.forest2012Km2);
})
.attr("cy", function(d) {
return yScale(d.forest2012);
})
.attr("r", 0)
.attr("fill", "green")
.append("title")
.text(function(d) {
return d.country + "'s forest area is " + d.forest2012Km2 + " km2 and cover " + d.forest2012 + "% of the country" ;
});
circles.sort(function(a, b) {
return d3.ascending(+a.forest2012Km2, +b.forest2012Km2);
})
.transition()
.delay(function(d, i) {
return i * 150;
})
.duration(2000)
.attr("r", 7);
// draw horizontal axis, allow for padding
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2]) + ")")
.call(xAxis);
// draw vertical axis, allow for padding
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + padding[3] + ",0)")
.call(yAxis);
});
</script>
<p>Source : Banque mondiale<p>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js