Skip to content

Instantly share code, notes, and snippets.

@aaizemberg
Last active October 20, 2017 17:04
Show Gist options
  • Save aaizemberg/715d9d0ab9a28c25476ded7e65e053da to your computer and use it in GitHub Desktop.
Save aaizemberg/715d9d0ab9a28c25476ded7e65e053da to your computer and use it in GitHub Desktop.
Seguidores en Twitter de los 4 candidatos a Senador
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Seguidores en Twitter de los 4 candidatos a Senador</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js"></script>
</head>
<body>
<div id="vis"></div>
<script>
var v = [
{'candidato':'cristina', 'seguidores':5163060 },
{'candidato':'sergio', 'seguidores': 1061798 },
{'candidato':'esteban', 'seguidores': 550250 },
{'candidato':'florencio', 'seguidores': 542601 }
];
var w = 300, h = 150, p = 70;
var dy = h / v.length;
var svg = d3.select("div#vis").append("svg")
.attr("width",w)
.attr("height",h)
svg.selectAll("text.candidatos").data(v).enter()
.append("text")
.attr("class","candidatos")
.attr("x", 10)
.attr("y", function(d,i) {return (i*dy)+25;})
.text(function(d,i) {return d.candidato;} )
var maximo = d3.max(v, function(d) {return d.seguidores; });
var escala = d3.scaleLinear().domain([0,maximo]).range([0,w-p]);
svg.selectAll("rect").data(v).enter().append("rect")
.attr("x", p)
.attr("y", function(d,i) {return (i*dy);})
.attr("height", dy-1)
.attr("width", 0 )
.transition()
.attr("width", function(d,i) {return escala(d.seguidores); } );
svg.selectAll("text.seguidores").data(v).enter()
.append("text")
.style("fill", function(d,i) {
return i === 0 ? "white" : "black";
} )
.attr("class","seguidores")
.attr("x", function(d,i) {
return (i > 0 ? p+5 : 0) + escala(d.seguidores);
} )
.attr("y", function(d,i) {return (i*dy)+25;})
.text(function(d,i) {return d.seguidores.toLocaleString();} );
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment