DC.js,交叉过滤器 - 隐藏 0 值箱/组时的动态图表高度



我想动态设置包含隐藏箱的 DC 图表的高度。

可以使用remove_empty_bins((函数在 DC 图表中隐藏零值箱 - 请参阅以下帖子: 如果值等于零,如何隐藏dc.js行图值

我创建了一个组"SubCategoryGroup_nonEmpty",它只包含非空箱。 但是现在我的图表高度无法适应非空箱的数量。

我尝试使用以下代码设置.height((:

SubcategoryRowChart
.dimension(SubCategoryDim)
.group(SubCategoryGroup_nonEmpty)
.elasticX(true)
.valueAccessor(function(p) { return p.value.count > 0 ? p.value.total / p.value.count : 0; })
...
.height(function(anchor) {
// calculate the height depending on the count of not-hidden bins
return SubCategoryDim.group().size() * 40; // 40 = size of single element including padding
});

但是使用SubCategoryDim.group((.size((我只得到总箱,而不是过滤后的箱:

如何获取过滤箱的计数?

戈登 - 你是我的救星!多谢!:)

这是我的最终解决方案:

  • 使用nonEmptyGroup.all().length获取筛选的条柱计数
  • 创建函数CalcRowChartHeight_bySingleRowHeight()以根据条柱数、行高和边距计算行高
function CalcRowChartHeight_bySingleRowHeight(chart, group, SingleRowHeight){
var ChartHeight = group.all().length * SingleRowHeight;
var ChartMargins = chart.margins().top + chart.margins().bottom;
var Height = ChartHeight + ChartMargins
return Height;
}
  • 覆盖设置heightredraw()函数。_redraw()函数是redraw()的副本。我使用它是因为在这种情况下我没有弄清楚如何使用preRedraw事件。知道吗?
SubCategoryRowChart
.dimension(SubCategoryDim)
.group(SubCategoryGroup_nonEmpty)
.elasticX(true)
...
.redraw = function (){
// calculate the height based on the count of non-hidden bins
SubCategoryRowChart.height(CalcRowChartHeight_bySingleRowHeight(SubCategoryRowChart, SubCategoryGroup_nonEmpty, 40))
return SubCategoryRowChart._redraw();
}
;

最新更新