我使用和d3.js v4以ES6风格React尝试制作具有上下文刷新的维度图表。本质上,我拿了this example并将其转换为ES6。d3 v4 + react + es6 + crossfilter:Selection.exit()。remove()不起作用
我遇到的问题是selection.exit().remove()
不能正常工作,因此在每次重绘时,越来越多的圆被添加到svg g元素中。创建画笔时会触发重绘。我通过运行检查
selection.exit()
.attr('class', d => {
console.log('anything');
return 'anything';
})
.remove();
但没有输出,所以我想我的数据选择无效。
这里是上下文:
componentDidMount() {
const el = ReactDOM.findDOMNode(this); // the mounted svg element
const svg = d3.select(el)
.attr('width', 300)
.attr('height', 500);
const g = svg.append('g')
.attr('transform', 'translate(32,32)');
let circles = g.append('g')
.selectAll('circle');
const brushGroup = g.append('g')
.attr('class', 'brush')
.call(brush);
function redraw() {
const all = group.all(); // crossfilter group returns an array
xAxisGroup.call(xAxis);
yAxisGroup.call(yAxis);
circles = circles.data(all, d => d); // keyFn for data constancy
circles.enter().append('circle')
.attr('r', radius)
.attr('cx', plotX) // radius + plotX/Y are simple curry functions
.attr('cy', plotY);
circles.exit().remove(); // not working!!
}
redraw();
}
我也知道在V4 d3-selection这种变化的,但我不是超级自信这行是我update
的,哪些是我的update + enter
。
任何帮助将不胜感激。谢谢!
的'exit'选择,在你将数据绑定到一个选择,所有这些都留下没有数据指他们的元素。现在仔细看看你的“let circles”:它们不受任何数据约束。您必须正确定义您的输入,更新和退出选择。现在他们没有正确定义。 –
当我设置'circles = circles.data(all,d => d)''时,不会创建正确的连接选择,因此'circles.enter()'和'circles.exit()'会怎么样? –
你有你的圈子数据的独特id这样的'circles = circles.data(all,d => d.id)' – Cyril