Reputation: 121
I now have data in an array as
A=[{“id”=3, “s1”=0.3, “s2”=0.5},{“id”=6, “s1”=0.3, “s2”=0.5},{“id”=23, “s1”=0.3, “s2”=0.5},….] etc
and I add it by:
.data(A, function(d) {return d.id})
Which works as intended.
Now I’d like to have a different data structure more like:
B={“3”:{“s1":0.3,"s2":0.5},"6":{"s1":0.3,"s2”:0.5},”23”:{“s1":0.3,"s2":0.5}}
But I am not sure how to make the .data() call for this structure, I.e. how to write the function providing the indexes?
Upvotes: 2
Views: 42
Reputation: 102198
D3 data()
method accepts only 3 things:
Therefore, you cannot use that object (B
) you have. You need to convert it in an array.
For instance, here is a solution (out of many) for converting it into an array similar to your first one (A
):
const B = {
"3": {
"s1": 0.3,
"s2": 0.5
},
"6": {
"s1": 0.3,
"s2": 0.5
},
"23": {
"s1": 0.3,
"s2": 0.5
}
};
const data = Object.keys(B).map(function(d) {
return Object.assign({}, B[d], {
id: d
})
});
console.log(data)
The difference here is that id
is a string, not a number. If you want a number instead, just coerce it.
Upvotes: 2