xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>exercise3</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: #ddddff;
}
svg {
background-color: white;
}
</style>
</head>
<body>
<script type="text/javascript">
var svg = d3.select("body")
.append("svg")
.attr("width", 900)
.attr("height", 900);
d3.csv("CPI1314.csv", function(data) {
data.sort(function(a, b) {
return d3.descending(+a.cpi2014Score, +b.cpi2014Score);
//If your numeric values aren't sorting properly,
//try commenting out the line above, and instead using:
//
//return d3.descending(+a.lifeSatisfaction, +b.lifeSatisfaction);
//
//Data coming in from the CSV is saved as strings (text),
//so the + signs here force JavaScript to treat those
//strings instead as numeric values, thereby fixing the
//sort order (hopefully!).
});
var rects = svg.selectAll("rect")
.data(data)
.enter()
.append("rect");
rects.attr("x", 0)
.attr("y", function(d, i) {
return i * 3;
})
.attr("width", function(d) {
return d.countryRank2014 * 5;
})
.attr("height", 2)
.attr("fill", function(d) {
if (d.Region=="AP") {return "grey"}
if (d.Region=="EE") {return "grey"}
if (d.Region=="ME") {return "yellow"}
if (d.Region=="AF") {return "grey"}
if (d.Region=="EU") {return "grey"}
if (d.Region=="AM") {return "grey"}
})
.append("title")
.text(function(d) {
return d.countryorTerritory + "'s CPI score in 2014 is " + d.cpi2014Score;
});
});
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js