Skip to content

Instantly share code, notes, and snippets.

@josecorella
Last active April 17, 2021 06:45
A10: Proportional Symbols
<!DOCTYPE html>
<html lang="">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<style>
.states {
fill: rgb(204, 204, 204);
stroke: #fff;
}
.symbol {
fill: steelblue;
fill-opacity: .8;
stroke: #fff;
}
</style>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script src="https://d3js.org/queue.v1.min.js"></script>
<script src="proportional-symbols.js"></script>
</body>
</html>
var width = 960,
height = 500;
var radius = d3.scale.sqrt().domain([0, 1e6]).range([0, 10]);
var path = d3.geo.path();
var svg = d3
.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
svg
.append("text")
.attr("font-family", "Arial, Helvetica, sans-serif")
.attr("transform", "translate(450,30)")
.style("text-anchor", "middle")
.attr("fill", "black")
.text("Population per State vs Expensive States");
queue()
.defer(d3.json, "us-all.json")
.defer(d3.json, "us-state-centroids.json")
.await(ready);
function ready(error, us, centroid) {
if (error) throw error;
svg
.append("path")
.attr("class", "states")
.datum(topojson.feature(us, us.objects.states))
.attr("d", path);
svg
.selectAll(".symbol")
.data(
centroid.features.sort(function (a, b) {
return b.properties.population - a.properties.population;
})
)
.enter()
.append("path")
.attr("class", "symbol")
.attr(
"d",
path.pointRadius(function (d) {
return radius(d.properties.population);
})
)
.style("fill", function (d) {
if (d.properties.hasOwnProperty("cost")) {
return "rgb(0, 224, 37)";
}
})
.append("title")
.text(function (d) {
nfObject = new Intl.NumberFormat("en-US");
return (
d.properties.name +
"\n" +
"Population: " +
nfObject.format(d.properties.population)
);
});
}
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment