【问题标题】:Is d3.json() function can get json object?d3.json() 函数是否可以获取 json 对象?
【发布时间】:2012-12-10 10:57:11
【问题描述】:

我是 JQuery / d3js 世界的新手。

我想从 json 格式的数据中绘制图表。 我看到了一些示例,d3.json(json,f) 函数只能获取包含 json 格式数据的文件。 我的问题:是否可以用json格式的对象或字符串来调用它,例如:

val jsonStr = { "foo" : "bar"}
d3.json(jsonStr ,f)

如果不是,我如何用动态数据(json格式)绘制图表

【问题讨论】:

    标签: jquery d3.js


    【解决方案1】:

    如果您将数据作为 Javascript 变量,则不需要 d3.json 函数。只需使用变量的名称,您将使用第二个参数(回调函数)的参数到d3.json

    var data = [1,2,3];   
    svg.selectAll("circle")
    .data(data)
    .enter()
    .append("circle")
    .‌​attr("cx", function(d) { return d; })
    .attr("cy", function(d) { return d; })
    

    【讨论】:

    • 你能写一个简单的例子来说明它的外观吗?
    • var data = [...]; svg.selectAll("foo").data(data)...
    • 你能举个例子吗?
    • var data = [1,2,3]; svg.selectAll("circle").data(data).enter().append("circle").attr("cx", function(d) { return d; }).attr("cy", function(d) { return d; })
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 2020-03-01
    • 2018-11-19
    相关资源
    最近更新 更多