如何在d3.json命令中使用d3.min和d3.max [英] How to use d3.min and d3.max within a d3.json command

查看:167
本文介绍了如何在d3.json命令中使用d3.min和d3.max的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触d3和Javascript。我已经玩了choropleth示例,但现在想做一些更详细的更改。其中之一是找到我用作输入的JSON的最大值和最小值。



我的问题很简单,因为对象下面的代码片段正确地输出到console.log(data)命令的控制台,但是整个对象也输出对于min和max语句,而不仅仅是最小和最大值。

  d3.json(data / json,function(data){
console.log(data);
console.log(d3.min([data]));
console.log(d3.max data]));
});

以下是JSON对象的片段:

  {01:32710,02:20280,03:18002} 

所有我想要的一些帮助是理解为什么d3.min([data])和d3.max([data])导致整个对象被输出到控制台

解决方案



d3.min() d3.max()函数对数组进行操作,对象。你似乎试图通过在数组中包装数据对象来解决这个问题(例如 d3.max([data])),但所有这些真正做的是说给我一个数组的最大值一个值(数据对象)。因为数组中只有一个值,所以返回该值。



如果只需要对象中的max / min值,可以使用 d3.values()函数以数组形式提取数据中的所有值,然后取该数组中的最大值:

  var max = d3.max(d3.values(data)); 
// 32710

如果您还想知道键,使用 d3.entries()创建一个 {key,value} 对象的数组, top:

  //创建一个键值对象数组
var max = d3.entries b $ b //按值递减排序
.sort(function(a,b){return d3.descending(a.value,b.value);})
//取第一个选项
[0];
// {key:01,value:32710}


I'm new to d3 and Javascript. I have played around with the choropleth example but would like to now make some more detailed changes. One of which is to find the max and min of the JSON that I am using as input.

My question is simple, for the code snippet below the object is output correctly to the console for the console.log(data) command, however the entire object is also output for the min and max statements as well instead of just the min and the max.

d3.json("data/unemployment_by_state.json", function(data) {   
  console.log(data);
  console.log(d3.min([data]));
  console.log(d3.max([data]));
});

Here is a snippet of the JSON object:

{"01":32710,"02":20280,"03":18002}

All I would like some help with is understanding why the d3.min([data]) and d3.max([data]) are causing the entire object to be output to the console instead of the min and max number in the data object.

Thanks in advance.

解决方案

The d3.min() and d3.max() functions operate on arrays, not on objects. You seem to be trying to address this by wrapping the data object in an array (e.g. d3.max([data])) but all this is really doing is saying "Give me the max value of an array with one value (the data object)." As there's only one value in the array, that value is returned.

If you just want the max/min value in the object, you can use the d3.values() function to extract all the values in the data as an array, then take the max value in that array:

var max = d3.max(d3.values(data)); 
// 32710

If you want to know the key as well, you might need to use d3.entries() to create an array of { key, value } objects, then sort them and take the top:

// create an array of key, value objects
var max = d3.entries(data)
    // sort by value descending
    .sort(function(a, b) { return d3.descending(a.value, b.value); })
    // take the first option
    [0];
// { key: "01", value: 32710 }

这篇关于如何在d3.json命令中使用d3.min和d3.max的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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