Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/components/drawing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ drawing.setRect = function(s, x, y, w, h) {
s.call(drawing.setPosition, x, y).call(drawing.setSize, w, h);
};

/** Translate / remove node
*
* @param {object} d : calcdata point item
* @param {sel} sel : d3 selction of node to translate
* @param {object} xa : corresponding full xaxis object
* @param {object} ya : corresponding full yaxis object
*
* @return {boolean} :
* true if selection got translated
* false if selection got removed
*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🎎 !

drawing.translatePoint = function(d, sel, xa, ya) {
// put xp and yp into d if pixel scaling is already done
var x = d.xp || xa.c2p(d.x),
Expand All @@ -59,8 +70,12 @@ drawing.translatePoint = function(d, sel, xa, ya) {
} else {
sel.attr('transform', 'translate(' + x + ',' + y + ')');
}
} else {
sel.remove();
return false;
}
else sel.remove();

return true;
};

drawing.translatePoints = function(s, xa, ya, trace) {
Expand Down
6 changes: 4 additions & 2 deletions src/traces/scatter/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,10 @@ function plotOne(gd, idx, plotinfo, cdscatter, cdscatterAll, element, transition
join.enter().append('g').classed('textpoint', true).append('text');

join.each(function(d) {
var sel = transition(d3.select(this).select('text'));
Drawing.translatePoint(d, sel, xa, ya);
var g = d3.select(this);
var sel = transition(g.select('text'));
var hasTextPt = Drawing.translatePoint(d, sel, xa, ya);
if(!hasTextPt) g.remove();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each <text> node is wrapped in a <g class="textpoint"> group. It is on this group that the data-join happens so we must make sure that group is removed whenever <text> node are removed to obtain the correct enter and merge selections on updates.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drawing.translatePoint gets used in line 436 above too, and could short-circuit the lines after it if it removed the node.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good eyes 👁️ thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 3f7601c

});

join.selectAll('text')
Expand Down
54 changes: 54 additions & 0 deletions test/jasmine/tests/scatter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,60 @@ describe('end-to-end scatter tests', function() {
expect(Plotly.d3.selectAll('.textpoint').size()).toBe(3);
}).catch(fail).then(done);
});

it('should remove all point and text nodes on blank data', function(done) {
function assertNodeCnt(ptCnt, txCnt) {
expect(d3.selectAll('.point').size()).toEqual(ptCnt);
expect(d3.selectAll('.textpoint').size()).toEqual(txCnt);
}

function assertText(content) {
d3.selectAll('.textpoint').each(function(_, i) {
var tx = d3.select(this).select('text');
expect(tx.text()).toEqual(content[i]);
});
}

Plotly.plot(gd, [{
x: [150, 350, 650],
y: [100, 300, 600],
text: ['A', 'B', 'C'],
mode: 'markers+text',
marker: {
size: [100, 200, 300],
line: { width: [10, 20, 30] },
color: 'yellow',
sizeref: 3,
gradient: {
type: 'radial',
color: 'white'
}
}
}])
.then(function() {
assertNodeCnt(3, 3);
assertText(['A', 'B', 'C']);

return Plotly.restyle(gd, {
x: [[null, undefined, NaN]],
y: [[NaN, null, undefined]]
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On master, this restyle call results in

d3.selectAll('.point').size() // => 0 (as desired)
d3.selectAll('.textpoint').size() // => 3 (which messes up subsequent updates)

})
.then(function() {
assertNodeCnt(0, 0);

return Plotly.restyle(gd, {
x: [[150, 350, 650]],
y: [[100, 300, 600]]
});
})
.then(function() {
assertNodeCnt(3, 3);
assertText(['A', 'B', 'C']);
})
.catch(fail)
.then(done);
});
});

describe('scatter hoverPoints', function() {
Expand Down