Summary

Hereโ€™s the deal. Instead of telling D3 how to do something, tell D3 what you want. You want the circle elements to correspond to data. You want one circle per datum. Instead of instructing D3 to create circles, then, tell D3 that the selection โ€œcircleโ€ should correspond to data. This concept is called the data join:

Thinking with Joins

  • d3.Selection ๊ฐ์ฒด๋Š” join()์„ ํ˜ธ์ถœํ•˜๊ธฐ์— ์•ž์„œ selection.data(data, key)๋กœ data๋ฅผ ๋จผ์ € ๋ถˆ๋Ÿฌ์˜จ ์ƒํƒœ์—ฌ์•ผ ํ•œ๋‹ค. ํ•ด๋‹น d3.Selection
  • ์ด๋•Œ data๋Š” ์ˆœํšŒ ๊ฐ€๋Šฅ(iterable)ํ•œ ๋ชจ๋“  ๊ฐ์ฒด์ด๋‹ค.
  • d3.Selection ๊ฐ์ฒด์˜ ๊ตฌ์กฐ๋ฅผ ๋จผ์ € ์•Œ์•„์•ผ ํ•œ๋‹ค.

Steps

join()์„ ํ˜ธ์ถœํ•œ d3.Selection ๊ฐ์ฒด์—๋Š” DOM ์š”์†Œ์˜ ๋ชฉ๋ก์„ ๊ฐ–๊ณ  ์žˆ๋‹ค. d3.Selection ๊ฐ์ฒด๋Š” DOM ์š”์†Œ ํ•˜๋‚˜ํ•˜๋‚˜๋งˆ๋‹ค __data__๋ฅผ ์ถ”๊ฐ€ํ•˜ ํ•ด๋‹น DOM๊ณผ ์ง์ง€์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ๋™์‹œ์— ์ €์žฅํ•œ๋‹ค.