简体   繁体   English

按组查找最新日期和输出值

[英]Find the most recent date and output value by group

I want to display the most recent value only for each group. 我只想显示每个组的最新值。

Example from CSV below: The total amount of Bagels, of the Cinnamon Raisin variety, were collected on three separate sampling periods: May 2017, March 2017, and November 2016 with their amounts being: 300, 100, and 20 respectively. 下面的CSV示例:肉桂葡萄干品种百吉饼的总量在三个单独的采样周期内收集:2017年5月,2017年3月和2016年11月,分别为300、100和20。

I've taken the year and month, combined them, and converted them into a number format so I can do a d3.max to locate the most recent (highest) value. 我已经将年和月进行了合并,并将它们转换为数字格式,所以我可以执行d3.max来定位最新(最高)值。 The highest would be 42856 (May 2017), but I want to display the amount (300) instead of the d3.max amount. 最高为42856(2017年5月),但我想显示金额(300)而不是d3.max金额。

breakfastItem,gluten,breakfastItemType,month,year,dateCode,value
Bagel,Yes,Cinnamon Raisin,May,2017,42856,300
Bagel,Yes,Cinnamon Raisin,March,2017,42795,100
Bagel,Yes,Cinnamon Raisin,November,2016,42675,20
Bagel,Yes,Blueberry,February,2017,42767,50
Bagel,Yes,Blueberry,November,2016,42675,30
Bagel,Yes,Blueberry,March,2016,42430,100
Bagel,Yes,Plain,February,2018,43132,200
Bagel,Yes,Plain,December,2017,43070,202
Bagel,Yes,Plain,February,2016,42401,201

Here was my Javascript: 这是我的Javascript:

d3.csv("Breakfast.csv",function(data) {
        data.forEach(function(d){
            d.value = +d.value;
            d.year = +d.year;
            d.dateCode = +d.dateCode;
        });

        var breakfastCombinations = d3.nest()
        .key(function(d) {return d.breakfastItem; })
        .key(function(d) {return d.breakfastItemType; })
        .rollup(function(oldestDate) { 
            return d3.max(oldestDate, function(d) {
                return d.dateCode; });
            })
        .entries(data);
        document.getElementById("breakfastjson").innerHTML = JSON.stringify(breakfastCombinations,false,2); 

    });

Which pops out my JSON as 弹出我的JSON为

 {
    "key": "Bagel",
    "values": [
      {
        "key": "Cinnamon Raisin",
        "value": 42856
      },
      {
        "key": "Blueberry",
        "value": 42767
      },
      {
        "key": "Plain",
        "value": 43132
      }
    ]
  }

But I want the "value" to be the total amount of Bagels of that type, instead of the dateCode. 但是我希望“值”是该类型的百吉饼的总数,而不是dateCode。 Like this: 像这样:

  {
    "key": "Bagel",
    "values": [
      {
        "key": "Cinnamon Raisin",
        "value": 300
      },
      {
        "key": "Blueberry",
        "value": 50
      },
      {
        "key": "Plain",
        "value": 200
      }
    ]
  }

I want to use this dateCode to identify the information to display for a lot of different pieces of information (like Month, Year, etc.). 我想使用此dateCode标识要显示的信息,以显示许多不同的信息(例如Month,Year等)。 I've tried combinations like: 我尝试过类似的组合:

        .rollup(function(oldestDate) { 
        return d3.max(oldestDate, function(d) {
            return d.dateCode; }).value;
        })

And

        .rollup(function(oldestDate) { 
        return d3.max(oldestDate, function(d) {
            return d.dateCode.value; });
        })

But I can't seem to find the right syntax to display it. 但是我似乎找不到正确的语法来显示它。 Help! 救命!

You just need to sort the oldestDate array of objects and get the value property of the first one, which is the highest: 您只需要对对象的oldestDate数组进行排序,并获取第一个对象的value属性,这是最高的:

.rollup(function(oldestDate) {
    return oldestDate.sort(function(a, b) {
        return b.dateCode - a.dateCode
    })[0].value
});

Here is the demo: 这是演示:

 var csv = `breakfastItem,gluten,breakfastItemType,month,year,dateCode,value Bagel,Yes,Cinnamon Raisin,May,2017,42856,300 Bagel,Yes,Cinnamon Raisin,March,2017,42795,100 Bagel,Yes,Cinnamon Raisin,November,2016,42675,20 Bagel,Yes,Blueberry,February,2017,42767,50 Bagel,Yes,Blueberry,November,2016,42675,30 Bagel,Yes,Blueberry,March,2016,42430,100 Bagel,Yes,Plain,February,2018,43132,200 Bagel,Yes,Plain,December,2017,43070,202 Bagel,Yes,Plain,February,2016,42401,201`; var data = d3.csvParse(csv, function(d) { d.value = +d.value; d.year = +d.year; d.dateCode = +d.dateCode; return d; }); var breakfastCombinations = d3.nest() .key(function(d) { return d.breakfastItem; }) .key(function(d) { return d.breakfastItemType; }) .rollup(function(oldestDate) { return oldestDate.sort(function(a, b) { return b.dateCode - a.dateCode })[0].value }) .entries(data); console.log(breakfastCombinations) 
 <script src="https://d3js.org/d3.v4.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM