简体   繁体   English

获得复杂减少的前 n 个

[英]Getting Top n Of Complex Reduce

I think I'm missing something obvious.我想我错过了一些明显的东西。 Using DC.JS's datatable implementation:使用 DC.JS 的数据表实现:

  var salesExpenseByCompany = dc.dataTable("#salesExpenseByCompany");

  var salesExpenseDim = facts.dimension(function (d) {
    return d.client;
  });

  function reduceAdd(i, d) {
    i.sales = i.sales + d.sales;
    i.expenses = i.expenses + d.expenses;
    return i;
  }
  function reduceRemove(i, d) {
    i.sales = i.sales - d.sales;
    i.expenses = i.expenses - d.expenses;
    return i;
  }

  function reduceInitial(i, d) {
    return {
      sales: 0,
      expenses: 0,
    };
  }

This, of course, returns a new object, summing up each property in the data.当然,这会返回一个新的 object,汇总数据中的每个属性。

 [
    {'key':'B','value':{'sales':1544,'expenses':478}},
    {'key':'C','value':{'sales':2781,'expenses':1354}},
    {'key':'D','value':{'sales':1196,'expenses':987}},
    {'key':'E','value':{'sales':1156,'expenses':622}},
    {'key':'F','value':{'sales':1778,'expenses':1208}},
    {'key':'G','value':{'sales':666,'expenses':55}},
    {'key':'A','value':{'sales':2318,'expenses':801}}
]

When I load it into the Datatable, i want the top 10 or 25 or 50 sales(set via buttons), but I'm not sure how to get that.当我将它加载到数据表中时,我想要前 10 或 25 或 50 个销售额(通过按钮设置),但我不知道如何获得它。 I want those to be sorted descending.我希望这些按降序排序。 Here's what I have so far:这是我到目前为止所拥有的:

    .dimension(salesExpenseGr)
.size(4)
    .order(d3.descending)
    .sortBy(function (d) {
      return d.value.sales;
    })
    .columns([
      {
        label: "Company Name",
        format: function (d) {
          return d.key;
        },
      },
      {
        label: "Sales",
        className: "text-right",
        format: function (d) {
          return numFormat(d.value.sales);
        },
      },
      {
        label: "Expenses",
        className: "text-right",
        format: function (d) {
          return numFormat(d.value.expenses);
        },
      },
    ]);

The .size() option seems to first take the first four of the dimension and then sorts those 4 by the proper column. .size()选项似乎首先采用维度的前四个,然后按正确的列对这四个进行排序。

With .size(4)使用.size(4)

在此处输入图像描述

Sans .size().size()

在此处输入图像描述

Company F should be in the table when the size is limited to 4.当规模限制在 4 个时,F 公司应在表中。

So what am I missing?那么我错过了什么? I suspect this has to do with the .top() option, but I'm not sure where to put it.我怀疑这与.top()选项有关,但我不确定该放在哪里。

https://codepen.io/jlbmagic/pen/WNxGWXZ https://codepen.io/jlbmagic/pen/WNxGWXZ

See the attached Codepen.请参阅随附的 Codepen。

THANKS!谢谢!

The group ordering should usually agree with the sorting of the data table:组排序通常应该与数据表的排序一致:

  var salesExpenseGr = salesExpenseDim
    // ...
    .order(({sales}) => sales);

I think if you used a simple reduction, the group would be sorted as you expect.我认为如果您使用简单的归约,该组将按您的预期排序。 But crossfilter doesn't know how to sort objects, so sorting doesn't happen and the bins are left in alphabetic order by key.但是 crossfilter 不知道如何对对象进行排序,因此不会发生排序,并且这些 bin 按字母顺序按键。

If the sorts agree, F is included instead of D:如果排序一致,则包含 F 而不是 D:

带 F 的表

(The value of F differs between your pen and your screenshots.) (您的笔和屏幕截图之间的 F 值不同。)

Fork of your codepen . 你的 codepen 的分支

historic note历史记录

It would be reasonable to ask why dc.js has footguns like this one.有理由问为什么 dc.js 有这样的footguns。 It has to do with the evolution of the library.这与图书馆的发展有关。 Early on, dc.js tried to use functionality from crossfilter whenever possible, so .size() uses dimension.top() (which is group.top() here).早期,dc.js 尽可能使用 crossfilter 的功能,因此.size()使用dimension.top() (这里是group.top() )。 But later on, people found they wanted to sort data in ways not possible using just crossfilter objects, so .sortBy() was added to the data table (and .ordering() for other charts).但后来,人们发现他们想要以仅使用交叉过滤器对象无法实现的方式对数据进行排序,因此.sortBy()添加到数据表中(以及.ordering()用于其他图表)。

Capped charts such as the row and pie charts have stopped using .top() for this reason, but the data table has never been cleaned up.由于这个原因,行图和饼图等封顶图已停止使用.top() ,但数据表从未被清理过。

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

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