简体   繁体   中英

Handling nested data arrays

I'm stuck... I want to use data from a field in a nested array. I'm newbie so I missing some parts :-( If a write in debug d[0].children[1].birthdate I get a correct value. When I try to plot my date I only get the first child from my "parent" data.

   //Part of my code:

    circle.append("circle") 
    .style("fill", "purple")
    .attr("r", 5)
    //This get me each parent first child but I need all the children e.g Adam, Eve, Julia....
    .attr("cx", function(d,i,j) { return x(new Date(d.parent[j].birthdate));}) 

My Data:

"parent":[
     "name": "Mum",
       "children": [
            {
                "name": "Adam",
                "birthdate": "2013-01-05"
            },
            {
                "name": "Eve",
                "birthdate": "2013-02-05"
            },
            {
                "name": "Julia",
                "birthdate": "2013-03-05"
            }
        ]
    },        
    {
        "name": "Dad",
        "children": [
            {
                "name": "Romeo",
                "birthdate": "2013-04-05"
            },
            {
                "name": "Maria",
                "birthdate": "2013-03-15"
            }
        ]
    },
    "name": "StepMom",
       "children": [
            {
                "name": "A",
                "birthdate": "2013-01-05"
            },
            {
                "name": "B",
                "birthdate": "2013-02-05"
            },
            {
                "name": "C",
                "birthdate": "2013-03-05"
            }
        ]
    }
    ]           

It's not clear what you're trying to do, but in your code you don't have .children . That is, instead of

.attr("cx", function(d,i,j) { return x(new Date(d.parent[j].birthdate));})

you probably want

.attr("cx", function(d,i,j) { return x(new Date(d.children[j].birthdate));})

On a different note, you should parse your dates properly instead of relying on the browser to do so:

.attr("cx", function(d,i,j) { return x(d3.format("%Y-%m-%d").parse(d.children[j].birthdate));})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM