D3 json解析斯卡拉js
问题描述:
我需要一些在斯卡拉js的d3的帮助。不知道如何去使用d3.json函数。D3 json解析斯卡拉js
我有这样的:
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()
}
)
几个问题:
- 的rects未按所以它看起来像dataArray的是不正确的,但是当我CONSOLE.LOG它我想我进行正确的js.Array [VPCS]
- 的rectXFuncVpc不会被调用(我printlns在那里不 在浏览器控制台打印)
- 返回典型单元的e强迫我把print()作为函数的最后一个语句
请问有人有机会给我一个例子吗?
答
很难找出没有vpcs json格式。 下面是一个简单的JSON您的JSON例子:
https://github.com/spaced/scala-js-d3-example-app/tree/json_example
要实施Unit
,你可以使用()