Created
March 18, 2016 07:07
-
-
Save arnolddevos/12972edcfee584333181 to your computer and use it in GitHub Desktop.
d3 key function demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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