官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 永靖县| 沈丘县| 临猗县| 建宁县| 荆门市| 海原县| 吉安县| 承德县| 图木舒克市| 南宫市| 长顺县| 弋阳县| 稷山县| 湖口县| 沾益县| 昌吉市| 长寿区| 宣武区| 宁安市| 四川省| 左云县| 平果县| 黄石市| 扶绥县| 嘉黎县| 纳雍县| 封丘县| 开封市| 额尔古纳市| 乌拉特中旗| 宝坻区| 安仁县| 区。| 廉江市| 如皋市| 桐柏县| 庆云县| 张家界市| 称多县| 炎陵县| 花垣县|