Skip to content

Instantly share code, notes, and snippets.

@63anp3ca
Last active August 29, 2018 16:43
Show Gist options
  • Save 63anp3ca/ae0d704a18f68a1b2baddc7b40a92ddd to your computer and use it in GitHub Desktop.
Save 63anp3ca/ae0d704a18f68a1b2baddc7b40a92ddd to your computer and use it in GitHub Desktop.
prueba33
license: mit
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>D3 DataBinding</title>
<style>
.land {
fill: #fff;
stroke: #ccc;
}
.state {
fill: #ccc;
stroke: #666;
}
</style>
</head>
<body>
<h1>Simple Barchart d3v5</h1>
<svg width=200
height=200
id="viz">
</svg>
<svg width=200
height=200
id="viz2">
</svg>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
// Ratio of Obese (BMI >= 30) in U.S. Adults, CDC 2008
var valueById = [
NaN, .187, .198, NaN, .133, .175, .151, NaN, .100, .125,
.171, NaN, .172, .133, NaN, .108, .142, .167, .201, .175,
.159, .169, .177, .141, .163, .117, .182, .153, .195, .189,
.134, .163, .133, .151, .145, .130, .139, .169, .164, .175,
.135, .152, .169, NaN, .132, .167, .139, .184, .159, .140,
.146, .157, NaN, .139, .183, .160, .143
];
var path = d3.geo.path();
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500);
d3.json("/mbostock/raw/4090846/us.json", function(error, us) {
if (error) throw error;
svg.append("path")
.datum(topojson.feature(us, us.objects.land))
.attr("class", "land")
.attr("d", path);
svg.selectAll(".state")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("path")
.attr("class", "state")
.attr("d", path)
.attr("transform", function(d) {
var centroid = path.centroid(d),
x = centroid[0],
y = centroid[1];
return "translate(" + x + "," + y + ")"
+ "scale(" + Math.sqrt(valueById[d.id] * 5 || 0) + ")"
+ "translate(" + -x + "," + -y + ")";
})
.style("stroke-width", function(d) {
return 1 / Math.sqrt(valueById[d.id] * 5 || 1);
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment