Built with blockbuilder.org
What, why, and how analysis
What?
Why?
How?
Decisions made:
forked from carlosfelipetorres's block: Tarea4
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>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
.bar:hover {
fill: brown;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
</style>
<h1>Visualization</h1>
<h2>Nychol Bazurto Gómez and Carlos Torres</h2>
<div id="visualization"></div>
<label><input type="checkbox"> Sort values</label>
</head>
<body>
<script>
// load the external data
// set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 1260 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// set the ranges
var x = d3.scaleBand()
.range([0, width])
.padding(0.1);
var y = d3.scaleLinear()
.range([height, 0]);
var tooltip = d3.select("body")
.append("div").append("div").append("div")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden");
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 color = d3.scaleOrdinal(d3.schemeCategory20c);
x.domain(data.map(function(d) { return d.index; }));
y.domain([0, d3.max(data, function(d) { return d.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("fill", function(d){ return color(d.value); });
// add the x Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// add the y Axis
svg.append("g")
.attr("class", "y axis")
.call(d3.axisLeft(y));
$('rect').tipsy({
gravity: 's',
html: true,
title: function() {
var d = this.__data__;
return +d.value;
}
});
d3.select("input").on("change", sortArray);
var sortTimeout = setTimeout(function() {
d3.select("input").property("checked", true).each(sortArray);
}, 2000);
function sortArray (){
clearTimeout(sortTimeout);
// Copy-on-write since tweens are evaluated after a delay.
var x0 = x.domain(data = data.sort(function (a, b) {
if (a.value < b.value) return -1;
if (a.value > b.value) return 1;
return 0;})
.map(function(d) {console.log(d.value); 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(x)
.selectAll("g")
.delay(delay);
}
</script>
</body>
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