Skip to content

Instantly share code, notes, and snippets.

@63anp3ca
Created August 29, 2018 14:12
Show Gist options
  • Save 63anp3ca/7610244a2921bc3b8156c45429f97c1d to your computer and use it in GitHub Desktop.
Save 63anp3ca/7610244a2921bc3b8156c45429f97c1d to your computer and use it in GitHub Desktop.
fresh block
license: mit
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>D3 DataBinding</title>
</head>
<body>
<h1>Scatterplot d3v5</h1>
<svg width=200
height=200
id="viz">
</svg>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
let myData = d3.json("https://raw.githubusercontent.com/john-guerra/consultaAnticorrupcion2018/master/consulta_anticorrupcion_municipios.json");
let svg = d3.select("#viz"),
margin = {top: 20, right: 20, bottom: 30, left: 50},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
const x = d3.scaleLinear()
.domain([0, d3.max(myData, d=> d.censo)])
.range([0, width])
const rects = svg.selectAll(".bar")
.data(myData.sort( (a,b) => d3.descending(a.censo, b.censo)) );
rects.enter()
.append("rect")
.attr("class", "bar")
.attr("x", 0)
.attr("y", (d,i, ds) => i*10)
.attr("width", d => x(d.censo))
.attr("height", 9)
.style("fill", "steelblue");
console.log("w", width, "h", height);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment