【问题标题】:D3 json parsing in Scala jsScala js中的D3 json解析
【发布时间】:2016-06-30 20:42:45
【问题描述】:

我需要一些关于 scala js 中 d3 的帮助。不知道如何使用 d3.json 函数。

我有这个:

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()

      }
    )

几个问题:

  1. 没有绘制矩形,所以看起来 dataArray 不正确,但是当我 console.log 时,我想我得到了一个正确的 js.Array[Vpcs]
  2. 从不调用 rectXFuncVpc(我的 printlns 没有 打印在浏览器控制台中)
  3. Unit 的返回类型迫使我将 print() 作为函数的最后一条语句

有没有人可以给我一个例子吗?

【问题讨论】:

    标签: d3.js scala.js


    【解决方案1】:

    如果没有 Vpcs json 格式,很难弄清楚。 这是带有简单 json 的 json 示例:

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

    要强制执行 Unit ,您可以使用 ()

    【讨论】:

      猜你喜欢
      • 2015-07-04
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多