Built with blockbuilder.org
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="jquery.tipsy.js"></script>
<link href="tipsy.css" rel="stylesheet" type="text/css" />
<style>
.bar:hover {
fill: brown;
}
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
position: relative;
width: 960px;
}
.axis text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
fill-opacity: .5;
}
.x.axis path {
display: none;
}
label {
position: absolute;
top: 10px;
right: 10px;
}
</style>
<h1>Visualization 4</h1>
<h2>Nychol Bazurto Gómez and Carlos Torres</h2>
<h4>Country: Germany</h4>
<h4>EPI Score: 80.47</h4>
<h4>10-Year Percent Change: 1.89%</h4>
<label><input type="checkbox"> Sort values</label>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var margin = {margin:60, top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var EPI_Score = 80.47;
var Ten_Year_Percent_Change = 1.89;
var x = d3.scaleBand()
.range([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
var xAxis = d3.axisBottom(x)
.scale(x);
var yAxis = d3.axisLeft(y)
.scale(y);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var tooltip = d3.select("body")
.append("label")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden");
d3.tsv("data.tsv", function(error, data) {
data.forEach(function(d) {
d.value = +d.value;
});
x.domain(data.map(function(d) { return d.index; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.attr("y", 0)
.attr("x", 70)
.attr("dy", "-0.2296em")
.attr("transform", "rotate(-90)")
.style("font-weight","bold");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Value");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.index); })
.attr('width', x.bandwidth())
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); })
.style("opacity", function(d){
if(d.value > 0 && d.value <= 40){
return 0.3;
}else if(d.value > 40 && d.value <= 80){
return 0.5;
}else if(d.value > 80 && d.value <= 90){
return 0.7;
}else if(d.value > 90 && d.value <= 100){
return 0.9;
}
});
d3.select("input").on("change", change);
var sortTimeout = setTimeout(function() {
d3.select("input").property("checked", true).each(change);
}, 2000);
function change() {
clearTimeout(sortTimeout);
// Copy-on-write since tweens are evaluated after a delay.
var x0 = x.domain(data.sort(this.checked
? function(a, b) { return b.value - a.value; }
: function(a, b) { return d3.ascending(a.index, b.index); })
.map(function(d) { return d.index; }))
.copy();
svg.selectAll(".bar")
.sort(function(a, b) { return x0(a.index) - x0(b.index); });
var transition = svg.transition().duration(750),
delay = function(d, i) { return i * 50; };
transition.selectAll(".bar")
.delay(delay)
.attr("x", function(d) { return x0(d.index); });
transition.select(".x.axis")
.call(xAxis)
.selectAll("g")
.delay(delay);
}
$('rect').tipsy({
gravity: 'w',
html: true,
title: function() {
var d = this.__data__;
return +d.value +" "+d.index;
}
});
});
</script>
Modified http://code.jquery.com/jquery-1.6.2.min.js to a secure url
https://d3js.org/d3.v4.min.js
https://code.jquery.com/jquery-1.6.2.min.js