简体   繁体   中英

Complication in accessing Object's value in D3 V4

I hope my question isn't too hard to understand. After I using the nest() function to my data and assigned to var groupByAgeAndtime , I have difficulties in accessing the values of the object. I don't know how to access the object's value directly and pass the value to d3.forceSimulation.

I've tried using Object.keys(groupByAgeAndtime, function(d){ return groupByAgeAndTime[d];}) to access object's value but it didn't work.

Here's the issue:

var groupByAgeAndtime = d3.nest()
              .key(function(d) { return d.age; })
              //.key(function(d) { return d.time_in_hospital; })
              .rollup(function(v) {
                return{
                    mean_time_in_hospital : d3.mean(v, function(d){ return d.time_in_hospital;})

              } })
              .object(datapoints);

After nesting and rollup the data, I don't know how to access and pass the var groupByAgeAndtime to another variable's function:

var simulation = d3.forceSimulation()
    .force("x",d3.forceX(width/2).strength(0.05))
    .force("y",d3.forceY(height/2).strength(0.05))
    .force("collide", d3.forceCollide(function(d){
        return radiusScale(d.groupByAgeAndtime) + 2;
    }))

How should I pass the nested data to return radiusScale(d.groupByAgeAndtime) + 2; then?

Here is my code in fiddle: code in fiddle

I would really appreciate if some expert are able to answer my problem.

transform the d3.nest data back into an array of key-value pairs

d3.map(datapoints).entries();

You have to do the sorting yourself and be aware that the key is a string with a possible $ .

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