- Expert Data Visualization
- Jos Dirksen
- 179字
- 2021-07-09 18:22:42
Removing elements which aren't needed anymore
The final step we need to take is to remove rectangles that aren't needed anymore. If in the first call to update we add five rectangles, and in the next call only three are needed, we're stuck with two leftover ones. D3 also has an elegant mechanism to deal with that:
rectangles.exit().remove();
The call to exit() will select the elements for which no data is available. We can then do anything we want with those rectangles. In this case, we just remove them by calling remove(), but we could also change their opacity to make them look transparent, or animate them to slowly disappear.
For instance, if we replace the previous line of code with this:
rectangles.exit().attr("class", "remove");
Then set the CSS for the remove class to this:
.remove {
fill: red;
opacity: 0.2;
}
In that case, we'd see the following:

In the preceding screenshot, we've reused two existing rectangles, and instead of removing the five we don't need, we change their style to the remove class, which renders them semi-transparent red.
- 演進式架構(原書第2版)
- Java程序設計(慕課版)
- Python量化投資指南:基礎、數據與實戰
- TypeScript Blueprints
- JavaScript:Functional Programming for JavaScript Developers
- Game Programming Using Qt Beginner's Guide
- 自己動手寫Java虛擬機
- Vue.js前端開發基礎與項目實戰
- 編寫高質量代碼:改善Python程序的91個建議
- Mastering PHP Design Patterns
- Instant 960 Grid System
- Python進階編程:編寫更高效、優雅的Python代碼
- SEO實戰密碼
- Java EE 7 Development with NetBeans 8
- 用Flutter極速構建原生應用