仅从 dc.js 图表(维度/组)中获取未过滤的数据

Get only non-filtered data from dc.js chart (dimension / group)

所以这是一个关于一个相当具体的问题的问题。据我所知,dc.js 的主要贡献者 Gordon 不支持对数标度的 elasticY(true) 函数。

因此,在知道这一点之后,我尝试通过在 dc.js 的 renderlet 事件中构建一个变通方法来实现我自己的解决方案。此事件始终由用户单击条形图触发。我想做的是:

let groupSize = this.getGroupSize(fakeGroup, this.yValue);
let maximum = group.top(1)[0].value;
let minimum = group.top(groupSize)[groupSize-1].value;

console.log(minimum, maximum);

chart.y(d3.scale.log().domain([minimum, maximum])
  .range(this.height, 0)
  .nice()
  .clamp(true));

我认为,此时 "fakeGroup"(即 group.top(50))仅包含用户单击某处后未过滤掉的数据点。但是,该组始终包含前 50 个中的所有数据点,并且不会因筛选事件而改变。

我真正想要的是获取所有未过滤掉的数据点,以获取 yScale 的新最大值和最小值,并通过再次调用 chart.y(...) 相应地重新缩放 yAxis。

有没有办法只获取仍在图表中且未过滤掉的数据行。我也尝试使用 remove_empty_bins(group) 但没有任何运气。某处总是缺少 all() 或 top(),即使在给了 remove_empty_bins 这两个函数之后。

我是这样解决的:

我创建了一个名为 rescale() 的函数,它看起来像这样:

rescale(chart, group, fakeGroup) {
let groupSize = this.getGroupSize(fakeGroup, this.yValue);

let minTop = group.top(groupSize)[groupSize-1].value;
let minimum = minTop > 0 ? minTop : 0.0001;
let maximum = group.top(1)[0].value;

chart.y(d3.scale.log().domain([minimum, maximum])
  .range(this.height, 0)
  .nice()
  .clamp(true));}

我觉得参数很漂亮 self-explanatory,我刚得到我的图表,整个组由 dimension.group.reduceSum 和我创建的假组组成,其中包含前 50 个元素,以减少我的图表的条数。

在事件监听器中调用rescale()方法

chart.on('preRedraw', (chart) => {
  this.rescale(chart, group, fakeGroup);
}

所以我所做的是 re-defining(re-setting 关于过滤数据的最小值和最大值)图表 yAxis 每次重绘图表时,这恰好也是每次我的一个图表是过滤。所以现在,在过滤另一个图表后,比例总是适合图表包含的过滤数据。