Skip to content

Instantly share code, notes, and snippets.

@d3indepth
Last active August 2, 2017 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d3indepth/053a606c7c488cdc0f0eabeaf85ec1dc to your computer and use it in GitHub Desktop.
Save d3indepth/053a606c7c488cdc0f0eabeaf85ec1dc to your computer and use it in GitHub Desktop.
Data join with array of objects
license: gpl-3.0
height: 170
border: no
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Data join with array of objects</title>
</head>
<style>
circle {
fill: #ddd;
}
</style>
<body>
<svg width="760" height="140">
<g transform="translate(70, 70)">
<circle />
<circle />
<circle />
<circle />
<circle />
</g>
</svg>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
var cities = [
{ name: 'London', population: 8674000},
{ name: 'New York', population: 8406000},
{ name: 'Sydney', population: 4293000},
{ name: 'Paris', population: 2244000},
{ name: 'Beijing', population: 11510000}
];
var s = d3.selectAll('circle');
s.data(cities);
s.attr('r', function(d) {
var scaleFactor = 0.000005;
return d.population * scaleFactor;
})
.attr('cx', function(d, i) {
return i * 120;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment