Skip to content

Instantly share code, notes, and snippets.

@riccardoscalco
Created May 22, 2015 09:58
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 riccardoscalco/110e30b431f6e6f112c0 to your computer and use it in GitHub Desktop.
Save riccardoscalco/110e30b431f6e6f112c0 to your computer and use it in GitHub Desktop.
d3 data join

Selection

p = d3.selectAll("p")
p // [Array[0]]
p[0] // []
p.data() // []

Data-join

p = p.data([1,2,3])
p // [Array[3]]
p[0] // [undefined, undefined, undefined]
p.data() // [undefined, undefined, undefined]
p.enter() // [Array[3]]
p.enter()[0] // [Object, Object, Object]

Append

p.enter()append("p").text(function(d) { return d }) // 3 new paragraphs on the DOM
p // [Array[3]]
p[0] // [<p>1</p>, <p>2</p>, <p>3</p>]
p.data() // [1, 2, 3]
p.enter() // [Array[3]]
p.enter()[0] // [Object, Object, Object]

Update

p.data([4,5,6])
p.data() // [4, 5, 6]
p.transition().text(function(d){ return d }) // the 3 paragraphs are updated

Written with StackEdit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment