许多饼图的过滤列表并删除它们

Filter list for many pie charts and removing them

我有 2 个饼图,其中的数据如下:

data: [
    {diseaseType: 'Cancer', diseaseDetails: 'Lung cancer', quantity: 100},
    {diseaseType: 'Diebetes', diseaseDetails: 'Unspecific', quantity: 650},
    {diseaseType: 'Cancer', diseaseDetails: 'Breast cancer', quantity: 80}
  ]

我想获取过滤器列表并能够像用户一样删除它们(它只是受控测试代码):

this.diseasePieChart.filters().splice(0, 1)
dc.renderAll()

它正在更新第一个图表,但第二个(与第一个相关)没有,它保持删除过滤器之前的状态。

我正在渲染的第二张图表是这样的:

self.diseasePieChart.on('filtered.monitor', function (chart) {
// create dimensions etc and render second chart
}

我也尝试在删除过滤器后再次执行 crossfilter(data) 。当我打电话时 dc.filterAll 所有过滤器都被重置。

感谢您的帮助!

更改过滤器的正确入口点是 chart.filter() or chart.replaceFilter(),具体取决于您是尝试切换单个项目还是一次更改整个过滤器数组。

如您所知,在图表内操作过滤器数组可能会影响图表的绘制方式,但不会将更改传达给交叉过滤器维度和其他图表。

请注意,如上面 link 中所述,每个函数的参数接受类型有点令人惊讶:

The filter parameter can take one of these forms:

  • A single value: the value will be toggled (added if it is not present in the current filters, removed if it is present)
  • An array containing a single array of values ([[value,value,value]]): each value is toggled
  • When appropriate for the chart, a dc filter object such as
    • dc.filters.RangedFilter for the dc.coordinateGridMixin charts
    • dc.filters.TwoDimensionalFilter for the heat map
    • dc.filters.RangedTwoDimensionalFilter for the scatter plot
  • null: the filter will be reset using the resetFilterHandler

因此,如果您想获取数组,删除一个项目,然后将其重新设置,您可以:

var filters = chart.filters().slice(0); // copy the array of filters
filters.splice(0,1)
chart.replaceFilter([filters])
   .redrawGroup();

或(使用切换功能):

chart.filter(chart.filters()[0])
    .redrawGroup();

请注意,您通常希望在更改滤镜后重绘而不是渲染。这将允许显示动画过渡,并且速度会快一点。

此外,chart.redrawGroupdc.redrawAll()相同,但它更安全一些,以防您将来有更多图表组。