我在用着dc.js http://dc-js.github.io渲染数据集的漂亮气泡图。底层的 dc.js 是交叉过滤器 https://github.com/square/crossfilter.
我想使用来自服务器的新数据顺利刷新我的图表。这个问题在Github上 https://github.com/dc-js/dc.js/issues/177明确表示可以通过以下方式做到这一点:
- 从交叉过滤器中删除所有数据
- 添加新数据
- calling
dc.redrawAll()
.
我已经完成了这个工作,但是为了删除所有数据,您首先必须清除所有过滤器(因为crossfilter.remove https://github.com/square/crossfilter/wiki/API-Reference#crossfilter_remove仅删除与当前过滤器匹配的记录)。
我想“记住”之前如何过滤我的数据,以便在替换所有数据后可以再次重建过滤器。我愿意深入人心crossfilter
代码,但任何指针都会有帮助。
另外:如果有人知道一种基于唯一密钥更新交叉过滤器数据的方法,那将是金粉!
这就是我最终一起破解的内容。它工作得很好,尽管我确信它的效率低得可笑,因为所有维度都必须从头开始创建:
var _xfilter = crossfilter({x:1, y:2},{x:3, y:4}),
_dimensions = [];
_dimensions.push(_xfilter.dimension(function(d) { return d.x; });
// Unfilters all the given dimensions, removes all data
// from xf and adds newData to xf.
var _xfilter_reset = function(xf, dimensions, newData) {
var i;
for (i = 0; i < dimensions.length; i++) {
// Clear all filters from this dimension.
// Necessary because xf.remove only removes records
// matching the current filter.
dimensions[i].filter(null);
}
xf.remove(); // Remove all data from the crossfilter
xf.add(newData);
return xf;
}
// Resets the global crossfilter object and reapplies all
// current dc.js chart filters.
var _refresh_data = function(data) {
var i, j,
chart, oldFilters,
allCharts = dc.chartRegistry.list();
_xfilter = _xfilter_reset(_xfilter, _dimensions, data);
// Reset all filters using dc.js
for (i = 0; i < allCharts.length; i++) {
chart = allCharts[i];
oldFilters = chart.filters(); // Get current filters
chart.filter(null); // Reset all filters on current chart
for (j = 0; j < oldFilters.length; j++) {
// Set all the oldFilters back onto the chart
chart.filter(oldFilters[j]);
}
}
dc.redrawAll();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)