Reputation: 201
{
"data" : {
"years" : {
"YR1998" : {
"week" : {
"W1" : { "mean": { 7.8 } },
"W2" : { "mean" : { 6.1 } },
}
},
"YR1999" : {
"week" : {
"W1" : { "mean" : { 5.1 } },
"W2" : { "mean" : { 8.1 } },
}
}
}
}
}
I have the following json file above and I wish to find the max "mean" using d3.max function but cannot seem to get a result on the y axis. The code I am using is the following,
var dataset;
d3.json("ili.json", function(error, data) {
dataset = data;
};
var y = d3.scale.linear().domain([0, d3.max(dataset, function(d) {
return d3.max(d.years, function(e) {
return d3.max(e.week, function(f) {
return d3.max(f.week, function(g) {
return g.mean;
}
}
}
}]).range([height, 0]);
Upvotes: 0
Views: 1388
Reputation: 27534
The first parameter to d3.max()
has to be an array; it doesn't work with key-value objects. Because the objects you're passing in don't have valid length
properties, d3.max
is treating them as if they are empty arrays, and returning an undefined value.
You can create an array out of a key-value object using the d3.values
. For your nested data structure, you'll have to apply it to each step of the nested max functions:
var y=d3.scale.linear().domain([0,
d3.max(d3.values(dataset), function(d) {
return d3.max(d3.values(d.years), function(e) {
return d3.max(d3.values(e.week), function(f) {
return d3.max(d3.values(f.week), function(g) {
return g.mean;
}
}
}
}]).range([height, 0]);
Upvotes: 1