【问题标题】:How to user JSON arrays with d3如何使用 d3 使用 JSON 数组
【发布时间】:2013-02-19 11:01:32
【问题描述】:

我是 d3 和 json 的新手。我正在尝试构建水平甘特图。早些时候,我使用存储在 var 数据集中的内联数组也达到了同样的效果。但是现在我已经用 json 对象数组替换了数组。

[   {
        "process" :"process-name 1",
        "stage" : "stage name 1",
        "activities": [
            {
            "activity_name": "waiting",
            "start": new Date('2012-12-10T06:45+05:30'),
            "end": new Date('2012-10-10T08:45+05:30'),
            },
            {
            "activity_name": "processing",
            "start": new Date('2012-10-11T05:45+05:30'),
            "end": new Date('2012-10-11T06:45+05:30'),
            },
            {
            "activity_name": "pending",
            "start": new Date('2012-10-12T11:45+05:30'),
            "end": new Date('2012-10-13T12:45+05:30'),
            }
            ]

    },
    {
        "process" :"process-name 2",
        "stage" : "stage name 2",
        "activities": [
            {
            "activity_name": "waiting",
            "start": new Date('2012-10-22T06:45+05:30'),
            "end": new Date('2012-10-23T08:45+05:30'),
            },
            {
            "activity_name": "processing",
            "start": new Date('2012-10-25T05:45+05:30'),
            "end": new Date('2012-10-25T06:45+05:30'),
            },
            {
            "activity_name": "pending",
            "start": new Date('2012-10-27T11:45+05:30'),
            "end": new Date('2012-10-27T12:45+05:30'),
            }
            ]
    }       

    ];

绘制矩形的代码的 d3 部分是这样的:

console.log(dataset);

var height = 600;
var width = 500;
var x = d3.time.scale().domain([new Date(2011, 0, 1,23,33,00), new Date(2013, 0, 1, 23, 59)]).range([0, width]);



var svg = d3.selectAll("body")
.append("svg")
.attr("width",width)
.attr("height",height)
.attr("shape-rendering","crispEdges");


temp = svg.selectAll("body")
.data(dataset) // LINE 1 
.enter()


temp.append("rect")
.attr("width", 4)
.attr("height",12)
.attr("x", function(d) {return x(d.activities[0].start)}) // LINE 2
.attr("y",function(d,i){return i*10;})
.attr("fill","steelblue");

我有以下问题:

  1. 我想在 activities 中访问 START 日期,但使用代码行(标记为 LINE 1 和 LINE 2)我有写它只为每个活动的第一次开始提供 2 个矩形。为什么?

  2. LINE 1 中,我可以使用 dataset.actitvies 而不是仅使用数据集吗?我尝试使用它,给了我这个错误:

未捕获的类型错误:无法读取未定义的属性“0”

【问题讨论】:

    标签: json d3.js data-visualization


    【解决方案1】:
    1. 因为数据集的长度是 2,所以当你将它与你的矩形连接时,你只会得到 2 个矩形。

    2. 您可以使用 dataset.activities,但您需要将 LINE 2 更改为

      .attr("x", function(d) {return x(d.start)})

    因为您更改了联接,因此 d 现在指的是活动。然而,如果你这样做,你只会得到三个矩形——你的一个项目的活动。

    如果您要使用嵌套数据,您要么需要使用嵌套选择,要么需要首先通过将所有活动添加到一个简单数组来展平数据。

    var data = [];
    
    for (var i = 0, len = dataset.length; i < len; i++) {
        data = data.concat(dataset[i].activities);
    }
    

    嵌套选择如下所示:

    projects = svg.selectAll("g")
        .data(dataset)
      .enter().append("g")
        .attr("transform", function (d, i) { return "translate(0," + (i * 10) + ")"; }),
    
    rects = projects.selectAll("rect")
        .data(function (d) { return d.activities; })
      .enter().append("rect")
        .attr("height", 12)
        .attr("width", 4)
        .attr("x", function (d) { return x(d.start); })
    

    编辑:这是jsfiddle

    【讨论】:

    • 只有一个问题。你写这个的地方 .data(function (d) { return d.activities; }) 我写了 .data(d.activities) 但它归还不明物体,为什么?
    • 很高兴听到!请接受答案,以便它显示为已回答。 bost.ocks.org/mike/nest 将为您提供有关嵌套选择的概述。基本上,您将组加入数据集,然后为矩形定义访问器函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    相关资源
    最近更新 更多