Skip to content

Instantly share code, notes, and snippets.

@arnolddevos
Created March 18, 2016 07:07
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 arnolddevos/12972edcfee584333181 to your computer and use it in GitHub Desktop.
Save arnolddevos/12972edcfee584333181 to your computer and use it in GitHub Desktop.
d3 key function demo
iteration 1
key( 11,12,13 | 11 | 0) was 'this' an array? true
key( 11,12,13 | 12 | 1) was 'this' an array? true
key( 11,12,13 | 13 | 2) was 'this' an array? true
iteration 2
key( [object HTMLDivElement] | 11 | 0) was 'this' an array? false
key( [object HTMLDivElement] | 12 | 1) was 'this' an array? false
key( [object HTMLDivElement] | 13 | 2) was 'this' an array? false
key( 11,12,13 | 11 | 0) was 'this' an array? true
key( 11,12,13 | 12 | 1) was 'this' an array? true
key( 11,12,13 | 13 | 2) was 'this' an array? true
iteration 3
key( [object HTMLDivElement] | 11 | 0) was 'this' an array? false
key( [object HTMLDivElement] | 12 | 1) was 'this' an array? false
key( [object HTMLDivElement] | 13 | 2) was 'this' an array? false
key( 11,12,13 | 11 | 0) was 'this' an array? true
key( 11,12,13 | 12 | 1) was 'this' an array? true
key( 11,12,13 | 13 | 2) was 'this' an array? true
package test
import scala.scalajs.js
import org.singlespaced.d3js.d3
import org.singlespaced.d3js.selection.Update
object KeyFunctionDemo extends js.JSApp {
def main() = {
for(i <- 1 to 3) {
println(s"iteration $i")
exercise
}
}
def exercise = {
val s: Update[Int] = d3.select("body").selectAll("div").data(numbers, key_func)
s.enter.append("div")
s.text((d: Int, i: Int, j: js.UndefOr[Int]) => d.toString)
}
def key_func: js.ThisFunction2[Any, js.UndefOr[Int], Int, String] = {
(t: Any, d: js.UndefOr[Int], i: Int) =>
println(s"key( $t | $d | $i) was 'this' an array? ${js.Array.isArray(t.asInstanceOf[js.Any])}")
i.toString
}
def numbers = js.Array(11, 12, 13)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment