0

I need some help with d3 in scala js please. Not sure how to go about using d3.json function.

I have this:

val rectXFunVpc = (d: Vpcs,
                   i: Int) => {
  println(s"rectXFunVpc i:$i")
  i * 30
}

d3.json(
      "json-file.json", callback = (e: Any, json: Any) => {

        val jsonAsString: String = JSON.stringify(json.asInstanceOf[js.Any])
        println(s"jsonAsString: $jsonAsString")
        val pickledJson = read[domain.DescribeVpcsObject](jsonAsString)
        println(s"pickledJson:$pickledJson")

        val dataArray: js.Array[Vpcs] = pickledJson.Vpcs.asInstanceOf[js.Array[Vpcs]]
        println(s"dataArray:$dataArray")

        val sel: Update[Vpcs] = svg.selectAll("rect").data(dataArray)
        sel.enter()
          .append("rect")
          .attr("x", rectXFunVpc)
          .attr("y", 20)
          .attr("width", 20)
          .attr("height", 10)
          .style("fill", rectColorFun)

        print()

      }
    )

couple of issues:

  1. The rects are not drawn so it looks like the dataArray is not right but when I console.log it I think I am getting a proper js.Array[Vpcs]
  2. The rectXFuncVpc is never called (my printlns in there are not printed in browser console)
  3. the return type of Unit is forcing me to put the print() as the last statement of the function

Is there a chance someone could provide me an example please?

Armin
  • 1,367
  • 1
  • 12
  • 17

1 Answers1

1

Hard to figure out without the Vpcs json format. Here is your json example with a simple json:

https://github.com/spaced/scala-js-d3-example-app/tree/json_example

To enforce a Unit , you can use ()