我可以根据 crossfilter/dc.js 中的交集(和)过滤数据吗?
Can I filter data based on an intersection (and) in crossfilter/dc.js?
我在 jsfiddle 上有以下示例:https://jsfiddle.net/woolycew65/bp5eavxy/ 并且很好奇我是否可以 select 饼图切片 TP-0 和 TP-2 来表示找到同时具有时间点 0 和时间点的患者2,不是 0 或 2。
数据集示例如下所示:
let data = [
{patientId: 101, site: "AAA", timepoints: [0, 2, 4, 6, 8, 12, 18, 24]},
{patientId: 102, site: "AAA", timepoints: [0, 2, 4, 6]},
{patientId: 103, site: "AAA", timepoints: [8, 12, 18, 24]},
{patientId: 104, site: "AAA", timepoints: [0, 4, 8, 18]},
{patientId: 105, site: "AAA", timepoints: [2, 6, 12, 24]},
{patientId: 501, site: "BBB", timepoints: [0]},
{patientId: 502, site: "BBB", timepoints: [2]},
{patientId: 503, site: "BBB", timepoints: [4]},
{patientId: 504, site: "BBB", timepoints: [6]},
{patientId: 505, site: "BBB", timepoints: [8]},
{patientId: 506, site: "BBB", timepoints: [12]},
{patientId: 507, site: "BBB", timepoints: [18]},
{patientId: 508, site: "BBB", timepoints: [24]}
];
我希望生成的交集过滤器生成 "AAA" (2),"BBB" (0),因为站点 AAA(101 和 102)有两名患者的时间点为 0 AND 2 且站点 BBB 处没有符合交叉过滤条件的患者。
显然结果数据集 returns "AAA" (4), "BBB" (2) 因为 AAA 站点有四名患者(101、102、104 和 105)有时间点 0 OR 2 和站点 BB 的两名患者满足联合过滤标准。
如果无法通过交叉过滤器实现,那么我假设我需要在 tpPie 图表上捕获一些事件并执行我自己的过滤,然后用新数据重新填充交叉过滤器。
如有任何帮助,我们将不胜感激。
感谢@EthanJewett 的评论和上述评论中的crossfilter 示例。我断断续续地搜索了好几个月,最后决定专心致志地寻找答案。 @Gordon 你可能也对这个例子感兴趣。
我已经能够将最终 dc.js 和交叉过滤器 example 放在一起,您可以在其中选择基于交叉点过滤时间点( 和 ) 或联合 ( 或 )。
如果你 select TP-6 和 TP-8 并在 或 / 和 之间切换你可以看到它的工作. and 选项表示我正在寻找时间点为 6 和 8 的患者。
我向数据集添加了一个附加字段 "Gender" 和另一个站点 "CCC"。
根据 Ethan 的指导,我缺少的关键部分是定义一个附加维度,当时间点饼图开始作为交集过滤时将被过滤( 和 )。
时间点饼图维度定义如下。 true 参数让维度知道数据是一个数组:
let tpDimension = ndx.dimension(function (d) {return d.timepoints;}, true);
额外的维度也和上面定义的一样,只是我们不打散数组,所以不传递真正的参数。该维度仅在时间点饼图filterHandler中使用。
let tp2Dimension = ndx.dimension(function (d) {return d.timepoints;});
如果用户select编辑了和选项,时间点饼图过滤器处理程序将被覆盖以使用额外维度。
tpPie.filterHandler(function (dimension, filters) {
if (filters.length === 0) {
// the empty case (no filtering)
dimension.filter(null);
} else if (filters.length === 1 && !filters[0].isFiltered) {
// single value and not a function-based filter
dimension.filterExact(filters[0]);
} else if (filters.length === 1 && filters[0].filterType === 'RangedFilter') {
// single range-based filter
dimension.filterRange(filters[0]);
} else {
// an array of values, or an array of filter objects
dimension.filterFunction(function (d) {
for (var i = 0; i < filters.length; i++) {
var filter = filters[i];
if (filter.isFiltered && filter.isFiltered(d)) {
return true;
} else if (filter <= d && filter >= d) {
return true;
}
}
return false;
});
};
// -------------------------------------------------------------
// Custom code to handle intersection filtering (AND)
if (glbTpFilterOption === 'tpAND') {
tpIntersectFilter(filters);
}
return filters;
});
执行时间点交叉过滤的实际代码是:
function tpIntersectFilter(filters) {
if (filters.length === 1) {
// Do not perform the intersect test when only 1 filter selected
tp2Dimension.filterAll();
} else if (filters.length === 0) {
// Since nothing is filtered we need to perform an intersect
// based on all keys.
tp2Dimension.filter(function (d) {
uniqueObjs = tpGroup.all();
for (var i = 0; i < uniqueObjs.length; i++) {
if (d.indexOf(uniqueObjs[i].key) == -1) return false;
}
return true;
});
} else {
tp2Dimension.filter(function (d) {
// Since we are looking for the intersection, test all
// filters, but once there isn't a match get out.
for (var i = 0; i < filters.length; i++) {
if (d.indexOf(filters[i]) == -1) return false;
}
return true;
});
}
}
最后,当用户在选项之间切换时,我需要处理:
$('#tpFilterOptions a').on('click', function () {
var sel = $(this).data('title');
var tog = $(this).data('toggle');
$('#' + tog).prop('value', sel);
$('a[data-toggle="' + tog + '"]').not('[data-title="' + sel + '"]').removeClass('active').addClass('notActive');
$('a[data-toggle="' + tog + '"][data-title="' + sel + '"]').removeClass('notActive').addClass('active');
glbTpFilterOption = sel;
if (sel === "tpOR") {
tp2Dimension.filterAll();
} else {
tpIntersectFilter(tpPie.filters());
}
dc.redrawAll();
})
我在 jsfiddle 上有以下示例:https://jsfiddle.net/woolycew65/bp5eavxy/ 并且很好奇我是否可以 select 饼图切片 TP-0 和 TP-2 来表示找到同时具有时间点 0 和时间点的患者2,不是 0 或 2。
数据集示例如下所示:
let data = [
{patientId: 101, site: "AAA", timepoints: [0, 2, 4, 6, 8, 12, 18, 24]},
{patientId: 102, site: "AAA", timepoints: [0, 2, 4, 6]},
{patientId: 103, site: "AAA", timepoints: [8, 12, 18, 24]},
{patientId: 104, site: "AAA", timepoints: [0, 4, 8, 18]},
{patientId: 105, site: "AAA", timepoints: [2, 6, 12, 24]},
{patientId: 501, site: "BBB", timepoints: [0]},
{patientId: 502, site: "BBB", timepoints: [2]},
{patientId: 503, site: "BBB", timepoints: [4]},
{patientId: 504, site: "BBB", timepoints: [6]},
{patientId: 505, site: "BBB", timepoints: [8]},
{patientId: 506, site: "BBB", timepoints: [12]},
{patientId: 507, site: "BBB", timepoints: [18]},
{patientId: 508, site: "BBB", timepoints: [24]}
];
我希望生成的交集过滤器生成 "AAA" (2),"BBB" (0),因为站点 AAA(101 和 102)有两名患者的时间点为 0 AND 2 且站点 BBB 处没有符合交叉过滤条件的患者。
显然结果数据集 returns "AAA" (4), "BBB" (2) 因为 AAA 站点有四名患者(101、102、104 和 105)有时间点 0 OR 2 和站点 BB 的两名患者满足联合过滤标准。
如果无法通过交叉过滤器实现,那么我假设我需要在 tpPie 图表上捕获一些事件并执行我自己的过滤,然后用新数据重新填充交叉过滤器。
如有任何帮助,我们将不胜感激。
感谢@EthanJewett 的评论和上述评论中的crossfilter 示例。我断断续续地搜索了好几个月,最后决定专心致志地寻找答案。 @Gordon 你可能也对这个例子感兴趣。
我已经能够将最终 dc.js 和交叉过滤器 example 放在一起,您可以在其中选择基于交叉点过滤时间点( 和 ) 或联合 ( 或 )。
如果你 select TP-6 和 TP-8 并在 或 / 和 之间切换你可以看到它的工作. and 选项表示我正在寻找时间点为 6 和 8 的患者。
我向数据集添加了一个附加字段 "Gender" 和另一个站点 "CCC"。
根据 Ethan 的指导,我缺少的关键部分是定义一个附加维度,当时间点饼图开始作为交集过滤时将被过滤( 和 )。
时间点饼图维度定义如下。 true 参数让维度知道数据是一个数组:
let tpDimension = ndx.dimension(function (d) {return d.timepoints;}, true);
额外的维度也和上面定义的一样,只是我们不打散数组,所以不传递真正的参数。该维度仅在时间点饼图filterHandler中使用。
let tp2Dimension = ndx.dimension(function (d) {return d.timepoints;});
如果用户select编辑了和选项,时间点饼图过滤器处理程序将被覆盖以使用额外维度。
tpPie.filterHandler(function (dimension, filters) {
if (filters.length === 0) {
// the empty case (no filtering)
dimension.filter(null);
} else if (filters.length === 1 && !filters[0].isFiltered) {
// single value and not a function-based filter
dimension.filterExact(filters[0]);
} else if (filters.length === 1 && filters[0].filterType === 'RangedFilter') {
// single range-based filter
dimension.filterRange(filters[0]);
} else {
// an array of values, or an array of filter objects
dimension.filterFunction(function (d) {
for (var i = 0; i < filters.length; i++) {
var filter = filters[i];
if (filter.isFiltered && filter.isFiltered(d)) {
return true;
} else if (filter <= d && filter >= d) {
return true;
}
}
return false;
});
};
// -------------------------------------------------------------
// Custom code to handle intersection filtering (AND)
if (glbTpFilterOption === 'tpAND') {
tpIntersectFilter(filters);
}
return filters;
});
执行时间点交叉过滤的实际代码是:
function tpIntersectFilter(filters) {
if (filters.length === 1) {
// Do not perform the intersect test when only 1 filter selected
tp2Dimension.filterAll();
} else if (filters.length === 0) {
// Since nothing is filtered we need to perform an intersect
// based on all keys.
tp2Dimension.filter(function (d) {
uniqueObjs = tpGroup.all();
for (var i = 0; i < uniqueObjs.length; i++) {
if (d.indexOf(uniqueObjs[i].key) == -1) return false;
}
return true;
});
} else {
tp2Dimension.filter(function (d) {
// Since we are looking for the intersection, test all
// filters, but once there isn't a match get out.
for (var i = 0; i < filters.length; i++) {
if (d.indexOf(filters[i]) == -1) return false;
}
return true;
});
}
}
最后,当用户在选项之间切换时,我需要处理:
$('#tpFilterOptions a').on('click', function () {
var sel = $(this).data('title');
var tog = $(this).data('toggle');
$('#' + tog).prop('value', sel);
$('a[data-toggle="' + tog + '"]').not('[data-title="' + sel + '"]').removeClass('active').addClass('notActive');
$('a[data-toggle="' + tog + '"][data-title="' + sel + '"]').removeClass('notActive').addClass('active');
glbTpFilterOption = sel;
if (sel === "tpOR") {
tp2Dimension.filterAll();
} else {
tpIntersectFilter(tpPie.filters());
}
dc.redrawAll();
})