xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Module 4 Exercise</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: Times, Arial, Tahoma;
}
h1 {
font-size: 30px;
margin: 0px 0px 20px 0px;
}
p1 {
font-size: 18px;
margin: 0px 20px 40px 0;
}
p2 {
font-size: 12px;
margin: 0px 0px 20px 10px;
}
p3 {
font-size: 14px;
font-family: Arial;
stroke:blue;
fill:blue;
margin: 0px 0px 20px 160px;
}
svg {
background-color: #6E6E6E;
}
rect:hover {
fill: #CEECF5;
}
.axis path,
.axis line {
fill: none;
stroke: white;
shape-rendering: crispEdges;
}
.axis text {
font-family: Times;
font-size: 15px;
stroke:white;
fill:white;
}
.y.axis path,
.y.axis line {
opacity: 0;
}
</style>
</head>
<body>
<h1>Corruption Perception</h1>
<p1>Corruption Perception Index “Corruption Perception ” scores by region. Source: <a href="https://www.transparency.org/cpi2014/results">CPI</a>, 2014</p>
<p2> Regions:
EU= European Union and Western Europe
AP= Asia Pacific
AM= Americas
ME= Middle East and North Africa
AF= Sub Saharan Africa
EE= Eastern Europe and Central Asia</p>
<p3> Worst Performing Countries< ------------- >BestPerforming Countries</p>
<script type="text/javascript">
var w = 800;
var h = 600;
var padding = [ 20, 20, 30, 120 ]; //Top, right, bottom, left
var widthScale = d3.scale.linear()
.range([ 0, w - padding[1] - padding[3] ]);
var heightScale = d3.scale.ordinal()
.rangeRoundBands([ padding[0], h - padding[2] ], 0.6);
var xAxis = d3.svg.axis()
.scale(widthScale)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(heightScale)
.orient("left");
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
d3.csv("CPI1314.csv", function(data) {
data.sort(function(a, b) {
return d3.descending(+a.cpi2014Score, +b.cpi2014Score);
});
widthScale.domain([ 0, d3.max(data, function(d) {
return +d.cpi2014Score;
}) ]);
heightScale.domain(data.map(function(d) { return d.Region; } ));
var rects = svg.selectAll("rect")
.data(data)
.enter()
.append("rect");
rects.attr("x", padding[3])
.attr("y", function(d) {
return heightScale(d.Region);
})
.attr("width", function(d) {
return widthScale(d.cpi2014Score);
})
.attr("height", heightScale.rangeBand())
.attr("fill", function(d) {
if (d.Region=="AP") {return "#F8E0E6"}
if (d.Region=="EE") {return "#FA5882"}
if (d.Region=="ME") {return "#F5A9BC"}
if (d.Region=="AF") {return "#F7819F"}
if (d.Region=="EU") {return "#FBEFF2"}
if (d.Region=="AM") {return "#F6CED8"}
})
.append("title")
.text(function(d) {
return d.countryorTerritory + "'s CPI 2014 score is " + d.cpi2014Score;
});
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);
});
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js