Skip to content

Commit e58dcb0

Browse files
authored
Merge pull request #1672 from plotly/scatter-text-join-fixup
Scatter <g textpoint> join fixup
2 parents ad65c23 + 3f7601c commit e58dcb0

File tree

3 files changed

+82
-8
lines changed

3 files changed

+82
-8
lines changed

src/components/drawing/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ drawing.setRect = function(s, x, y, w, h) {
4747
s.call(drawing.setPosition, x, y).call(drawing.setSize, w, h);
4848
};
4949

50+
/** Translate / remove node
51+
*
52+
* @param {object} d : calcdata point item
53+
* @param {sel} sel : d3 selction of node to translate
54+
* @param {object} xa : corresponding full xaxis object
55+
* @param {object} ya : corresponding full yaxis object
56+
*
57+
* @return {boolean} :
58+
* true if selection got translated
59+
* false if selection got removed
60+
*/
5061
drawing.translatePoint = function(d, sel, xa, ya) {
5162
// put xp and yp into d if pixel scaling is already done
5263
var x = d.xp || xa.c2p(d.x),
@@ -59,8 +70,12 @@ drawing.translatePoint = function(d, sel, xa, ya) {
5970
} else {
6071
sel.attr('transform', 'translate(' + x + ',' + y + ')');
6172
}
73+
} else {
74+
sel.remove();
75+
return false;
6276
}
63-
else sel.remove();
77+
78+
return true;
6479
};
6580

6681
drawing.translatePoints = function(s, xa, ya, trace) {

src/traces/scatter/plot.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ function plotOne(gd, idx, plotinfo, cdscatter, cdscatterAll, element, transition
391391
}
392392

393393
function makePoints(d) {
394-
var join, selection;
394+
var join, selection, hasNode;
395395

396396
var trace = d[0].trace,
397397
s = d3.select(this),
@@ -433,11 +433,14 @@ function plotOne(gd, idx, plotinfo, cdscatter, cdscatterAll, element, transition
433433
join.each(function(d) {
434434
var el = d3.select(this);
435435
var sel = transition(el);
436-
Drawing.translatePoint(d, sel, xa, ya);
437-
Drawing.singlePointStyle(d, sel, trace, markerScale, lineScale, gd);
436+
hasNode = Drawing.translatePoint(d, sel, xa, ya);
438437

439-
if(trace.customdata) {
440-
el.classed('plotly-customdata', d.data !== null && d.data !== undefined);
438+
if(hasNode) {
439+
Drawing.singlePointStyle(d, sel, trace, markerScale, lineScale, gd);
440+
441+
if(trace.customdata) {
442+
el.classed('plotly-customdata', d.data !== null && d.data !== undefined);
443+
}
441444
}
442445
});
443446

@@ -458,8 +461,10 @@ function plotOne(gd, idx, plotinfo, cdscatter, cdscatterAll, element, transition
458461
join.enter().append('g').classed('textpoint', true).append('text');
459462

460463
join.each(function(d) {
461-
var sel = transition(d3.select(this).select('text'));
462-
Drawing.translatePoint(d, sel, xa, ya);
464+
var g = d3.select(this);
465+
var sel = transition(g.select('text'));
466+
hasNode = Drawing.translatePoint(d, sel, xa, ya);
467+
if(!hasNode) g.remove();
463468
});
464469

465470
join.selectAll('text')

test/jasmine/tests/scatter_test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,60 @@ describe('end-to-end scatter tests', function() {
383383
expect(Plotly.d3.selectAll('.textpoint').size()).toBe(3);
384384
}).catch(fail).then(done);
385385
});
386+
387+
it('should remove all point and text nodes on blank data', function(done) {
388+
function assertNodeCnt(ptCnt, txCnt) {
389+
expect(d3.selectAll('.point').size()).toEqual(ptCnt);
390+
expect(d3.selectAll('.textpoint').size()).toEqual(txCnt);
391+
}
392+
393+
function assertText(content) {
394+
d3.selectAll('.textpoint').each(function(_, i) {
395+
var tx = d3.select(this).select('text');
396+
expect(tx.text()).toEqual(content[i]);
397+
});
398+
}
399+
400+
Plotly.plot(gd, [{
401+
x: [150, 350, 650],
402+
y: [100, 300, 600],
403+
text: ['A', 'B', 'C'],
404+
mode: 'markers+text',
405+
marker: {
406+
size: [100, 200, 300],
407+
line: { width: [10, 20, 30] },
408+
color: 'yellow',
409+
sizeref: 3,
410+
gradient: {
411+
type: 'radial',
412+
color: 'white'
413+
}
414+
}
415+
}])
416+
.then(function() {
417+
assertNodeCnt(3, 3);
418+
assertText(['A', 'B', 'C']);
419+
420+
return Plotly.restyle(gd, {
421+
x: [[null, undefined, NaN]],
422+
y: [[NaN, null, undefined]]
423+
});
424+
})
425+
.then(function() {
426+
assertNodeCnt(0, 0);
427+
428+
return Plotly.restyle(gd, {
429+
x: [[150, 350, 650]],
430+
y: [[100, 300, 600]]
431+
});
432+
})
433+
.then(function() {
434+
assertNodeCnt(3, 3);
435+
assertText(['A', 'B', 'C']);
436+
})
437+
.catch(fail)
438+
.then(done);
439+
});
386440
});
387441

388442
describe('scatter hoverPoints', function() {

0 commit comments

Comments
 (0)