如何从JSON数据中找到最大值和最小值.我想要data1,data2 ... data(n)的最大checkins和Checkintimes.我的代码是:
var max = d3.max(data, function(d) { return d.Checkintimes;} );
var min = d3.min(data, function(d) { return d.Checkintimes;} );
Run Code Online (Sandbox Code Playgroud)
我也试过这个选项
d3.json("Values.json", function(data) {
var maxmbt = d3.max(d3.values(data) );
var maxcheckins = d3.max(d3.values(data));
console.log(maxmbt);
console.log(maxcheckins);
xScale.domain([0,maxcheckins]);
xScale.domain([0,maxmbt]);
});
Run Code Online (Sandbox Code Playgroud)
我有一个这种格式的json:
[
{
"name":"data1",
"checkins":[[1,0],[2,0],[3,0],[4,3],[5,0],[6,0],[7,0],[8,3],[9,0],[10,0],[11,0],[12,0]],
"teamsize":[[1,0],[2,0],[3,0],[4,3],[5,0],[6,0],[7,0],[8,1],[9,0],[10,0],[11,0],[12,0]],
"Checkintimes":[[1,0],[2,0],[3,0],[4,184],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,0],[12,0]]
},
{"name":"data2",
"checkins":[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,27],[12,12]],
"teamsize":[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,11],[12,11]],
"Checkintimes":[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,10],[12,12]]
}
]
Run Code Online (Sandbox Code Playgroud)
目前,当我使用返回值是一个数组.不是单一价值.对于例如签到,这里的最大值是27,并且在上述示例中是Checkintimes-184.
该d3.max函数需要具有获取单个值的访问器.您需要嵌套调用,因为您有嵌套数据:
var max = d3.max(data, function(d) {
return d3.max(d.Checkintimes, function(e) { return d3.max(e); });
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8453 次 |
| 最近记录: |