Skip to content

Instantly share code, notes, and snippets.

@sampathweb
Last active October 17, 2016 23: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 sampathweb/2d1eb60f870d07a4a7ac1d7c742817a1 to your computer and use it in GitHub Desktop.
Save sampathweb/2d1eb60f870d07a4a7ac1d7c742817a1 to your computer and use it in GitHub Desktop.
merging selections
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
var dataFirst = [
{ldap: "enjalot", x: 0},
{ldap: "siumei", x: 1},
{ldap: "ramesh", x: 2}
]
var dataLater = [
{ldap: "sami", x: 3},
{ldap: "logan", x: 4},
{ldap: "phillip", x: 5},
{ldap: "siumei", x: 6}
];
svg.selectAll("circle")
.data(dataFirst, function(d) {
return d.ldap;
})
.enter()
.append("circle")
.attr("r", 30)
.attr("cx", function(d, i) {
return 50 + i * 80
})
.attr("cy",100)
.attr("fill", "#eda39a")
.on("mouseover", function(d) {
console.log(d);
})
setTimeout(function() {
var circles = svg.selectAll("circle")
.data(dataLater, function(d){
return d.ldap;
});
var enteredCircles = circles
.enter()
.append("circle")
.attr("r", 30)
.attr("cx", function(d, i) {
return 50 + d.x * 80
})
.attr("cy",100)
.attr("fill", "#862417");
enteredCircles.merge(circles)
.attr("cx", function(d, i){
return 50 + d.x * 80
})
.style("stroke", "#111")
}, 1000)
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment