javascript - d3 v4 + react + es6 + crossfilter: Selection.exit().remove() not working -
i'm using crossfilter.js , d3.js v4 es6 style react in attempt make dimensional charts context brushing. essentially, took this example , converted es6.
the problem have selection.exit().remove()
not working such on each redraw, more , more circles appended svg g element. redraws triggered when brush created. checked running
selection.exit() .attr('class', d => { console.log('anything'); return 'anything'; }) .remove();
but nothing outputted i'm thinking data selection not valid.
here context:
componentdidmount() { const el = reactdom.finddomnode(this); // 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 = group.all(); // crossfilter group returns array xaxisgroup.call(xaxis); yaxisgroup.call(yaxis); circles = circles.data(all, d => d); // keyfn data constancy circles.enter().append('circle') .attr('r', radius) .attr('cx', plotx) // radius + plotx/y simple curry functions .attr('cy', ploty); circles.exit().remove(); // not working!! } redraw(); }
i aware of change in d3-selection in v4 i'm not super confident lines update
, ones update + enter
.
any appreciated. thank you!
i suspect problem 1 of 2 things. #1 below. it's bit hard tell out working example test things out in, here go:
i believe
selectall
,data
join should happen inredraw
function. because never redoselectall
in redraw function, selection never contain elements. if checkenter
,exit
selections inredraw
function,enter
selection contain data points because underlying selection empty.that data key function returns object. since object result of crossfilter's
group.all
, should comparable reference, safercircles.data(all, d => d.key)
.
the solution should like:
componentdidmount() { const el = reactdom.finddomnode(this); // 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 circlesgroup = g.append('g'); // hang on group const brushgroup = g.append('g') .attr('class', 'brush') .call(brush); function redraw() { const = group.all(); // crossfilter group returns array xaxisgroup.call(xaxis); yaxisgroup.call(yaxis); let circles = circlesgroup // selection , data join here .selectall('circle') .data(all, d => d.key); // have key function return key, not object circles.enter().append('circle') .attr('r', radius) .attr('cx', plotx) // radius + plotx/y simple curry functions .attr('cy', ploty); circles.exit().remove(); } redraw(); }
Comments
Post a Comment