Built with blockbuilder.org
Last active
October 13, 2017 20:09
-
-
Save anqi-lu/0b716989e10e636f4e0aee1a9f69adba to your computer and use it in GitHub Desktop.
General Update Pattern Notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> | |
<!--svg width="960" height="500"></svg!--> | |
<script> | |
// Feel free to change or delete any of the code you see in this editor! | |
function render(data){ | |
// Managing one thing | |
var svg = d3.select("body") | |
.selectAll("svg") | |
.data([null]); | |
svg = svg | |
.enter() | |
.append("svg") | |
.attr("width", 960) | |
.attr("height", 500) | |
.merge(svg); // merge enter, update selections, reasign svg into the merged selection | |
var rects = svg//d3.select("svg") | |
.selectAll("rect") | |
.data(data); | |
// .attr("cx", function (d) { return d; }) | |
// .attr("fill", "orange"); | |
rects | |
.exit() | |
.remove(); | |
rects | |
.enter() | |
.append("rect") | |
.attr("x", function(d) { return d; }) | |
.attr("y", 100) | |
.attr("width", 70) | |
.attr("height", 300) | |
.attr("fill", "green") | |
.merge(rects) // so the cx applies to all rects (both new and old) | |
.attr("cx", function(d){ return d; }); | |
} | |
// test cases (enter, update, exit) | |
render([100]); | |
render([300, 500, 700, 800, 880]); | |
//render([300, 500]); | |
render([]); | |
// render([200, 400, 600, 800]); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment