d3.js - 具有值数组的json数据的最大值和最小值 [英] d3.js - max and min value from json data which has array of values

查看:1850
本文介绍了d3.js - 具有值数组的json数据的最大值和最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从JSON数据中查找最大值和最小值。我想要的最大的checkins和checkinimes的data1,data2 ... data(n)。我的代码是:

How do i find the max and min values from JSON data. I want the max of checkins and Checkintimes of data1,data2 ...data(n). My code is :

 var max = d3.max(data, function(d) { return d.Checkintimes;} );
 var min = d3.min(data, function(d) { return d.Checkintimes;} );

我也尝试过这个选项。

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]);

});

我有一个这种格式的json:

I have a json in this format:

[
{
   "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]]
}
]

目前当我使用返回值是数组。不是一个单一的价值。对于例如检查最大值这里是27和检查时间 - 在上述示例中的184。

currently when i use the returning value is an array. not an unitary value. For e.g checkins max value here is 27 and Checkintimes - 184 in the aforesaid example.

推荐答案

d3 .max 函数需要具有获取单个值的访问器。您需要嵌套调用,因为您有嵌套数据:

The d3.max function needs to have an accessor that gets individual values. You would need nested calls as you have nested data:

var max = d3.max(data, function(d) {
  return d3.max(d.Checkintimes, function(e) { return d3.max(e); });
});

这篇关于d3.js - 具有值数组的json数据的最大值和最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆