复杂的减少样本不清楚如何减少工作

问题描述 投票:0回答:1

complex reduce sample开始,我已将其修剪为单个图表,我试图了解减少的工作原理

我在代码中做了一些评论,这些评论不在示例中,表示我认为根据我如何阅读文档而发生的事情。

function groupArrayAdd(keyfn) {
      var bisect = d3.bisector(keyfn);  //set the bisector value function
     //elements is the group that we are reducing,item is the current item
       //this is a the reduce function being supplied to the reduce call on the group runAvgGroup for add below
      return function(elements, item) {
          //get the position of the key value  for this element in the sorted array and put it there
          var pos = bisect.right(elements, keyfn(item));
          elements.splice(pos, 0, item);
          return elements;
      };
  }
  function groupArrayRemove(keyfn) {
      var bisect = d3.bisector(keyfn);//set the bisector value function
    //elements is the group that we are reducing,item is the current item
    //this is a the reduce function being supplied to the reduce call on the group runAvgGroup for remove below
      return function(elements, item) {
        //get the position of the key value  for this element in the sorted array and splice it out
          var pos = bisect.left(elements, keyfn(item));
          if(keyfn(elements[pos])===keyfn(item))
              elements.splice(pos, 1);
          return elements;
      };
  }
  function groupArrayInit() {
                  //for each key found by the key function return this array?
      return [];  //the result array for where the data is being inserted in sorted order?
  }

我不太确定我对这是如何工作的看法是完全正确的。一些魔法没有表现出来。我是否纠正了元素是正在调用reduce函数的组?还有groupArrayInit()中的数组是如何间接填充的?

我的一部分感觉提供给reduce调用的函数实际上是array.map函数而不是array.reduce函数,但我不能完全理解为什么。阅读了文档,我在这里没有联系。

任何帮助,将不胜感激。还有我错过了为所有这些例子创建的Pens / Fiddles吗?像这一个http://dc-js.github.io/dc.js/examples/complex-reduce.html这是我从这开始,但不得不下载csv并手动转换为Json。

--------------更新我添加了一些print语句,试图澄清add函数是如何工作的

  function groupArrayAdd(keyfn) {
      var bisect = d3.bisector(keyfn);  //set the bisector value function
     //elements is the group that we are reducing,item is the current item
       //this is a the reduce function being supplied to the reduce call on the group runAvgGroup for add below
      return function(elements, item) {
        console.log("---Start Elements and Item and keyfn(item)----")
        console.log(elements)  //elements grouped by run?
        console.log(item)   //not seeing the pattern on what this is on each run
        console.log(keyfn(item))
        console.log("---End----")
          //get the position of the key value  for this element in the sorted array and put it there
          var pos = bisect.right(elements, keyfn(item));
          elements.splice(pos, 0, item);
          return elements;
      };
  }

并打印出小组的内容

console.log("RunAvgGroup")
console.log(runAvgGroup.top(Infinity))

这导致final group not sorted by Key

哪个看起来不正确b / c值没有按键排序(运行编号)?查看打印语句的结果似乎也没有帮助。

crossfilter
1个回答
1
投票

这对我来说基本上是正确的。问题只是概念性的。

Crossfilter的group.reduce与Array.reduce或Array.map完全不同。 Group.reduce定义了处理向组添加新记录或从组中删除记录的方法。因此它在概念上类似于支持反转操作的增量Array.reduce。这允许应用和删除过滤器。

Group.top返回您的组列表。这些组的value属性应该是reduce函数返回的元素值。组的密钥是组访问者(在创建组的dimension.group调用中定义)返回的值,如果未定义组访问者,则为维度访问者。减少功能仅适用于组值,并且不能直接访问组密钥。

因此,请检查group.top输出中的这些值,希望您能看到所期望的元素列表。

© www.soinside.com 2019 - 2024. All rights reserved.