Skip to content

Commit f786b4f

Browse files
committed
Add setPointGroupScale tests
1 parent 4bd2987 commit f786b4f

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

src/lib/index.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -484,22 +484,30 @@ lib.setScale = function(element, x, y) {
484484
return transform;
485485
};
486486

487-
lib.setPointScale = function(selection, x, y) {
487+
lib.setPointGroupScale = function(selection, x, y) {
488+
var t, scale, re;
489+
488490
x = x || 1;
489491
y = y || 1;
490492

491-
// The same scale transform for every point:
492-
var scale = ' scale(' + x + ',' + y + ')';
493+
if(x === 1 && y === 1) {
494+
scale = '';
495+
} else {
496+
// The same scale transform for every point:
497+
scale = ' scale(' + x + ',' + y + ')';
498+
}
493499

494500
// A regex to strip any existing scale:
495-
var re = /sc.*/;
501+
re = /\s*sc.*/;
496502

497503
selection.each(function() {
498504
// Get the transform:
499-
var t = this.getAttribute('transform').replace(re, '');
505+
t = (this.getAttribute('transform') || '').replace(re, '');
506+
t += scale;
507+
t = t.trim();
500508

501509
// Append the scale transform
502-
this.setAttribute('transform', t + scale);
510+
this.setAttribute('transform', t);
503511
});
504512

505513
return scale;

src/plots/cartesian/dragbox.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -634,9 +634,12 @@ module.exports = function dragBox(gd, plotinfo, x, y, w, h, ns, ew) {
634634
subplot.plot
635635
.call(Lib.setTranslate, plotDx, plotDy)
636636
.call(Lib.setScale, xScaleFactor, yScaleFactor)
637-
.selectAll('.points').selectAll('.point')
638-
.call(Lib.setPointScale, 1 / xScaleFactor, 1 / yScaleFactor);
639637

638+
// This is specifically directed at scatter traces, applying an inverse
639+
// scale to individual points to counteract the scale of the trace
640+
// as a whole:
641+
.selectAll('.points').selectAll('.point')
642+
.call(Lib.setPointGroupScale, 1 / xScaleFactor, 1 / yScaleFactor);
640643
}
641644
}
642645

test/jasmine/tests/lib_test.js

+38
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,44 @@ describe('Test lib.js:', function() {
12021202
});
12031203
});
12041204

1205+
describe('setPointGroupScale', function() {
1206+
var el, sel;
1207+
1208+
beforeEach(function() {
1209+
el = document.createElement('div');
1210+
sel = d3.select(el);
1211+
});
1212+
1213+
it('sets the scale of a point', function() {
1214+
Lib.setPointGroupScale(sel, 2, 2);
1215+
expect(el.getAttribute('transform')).toBe('scale(2,2)');
1216+
});
1217+
1218+
it('appends the scale of a point', function() {
1219+
el.setAttribute('transform', 'translate(1,2)');
1220+
Lib.setPointGroupScale(sel, 2, 2);
1221+
expect(el.getAttribute('transform')).toBe('translate(1,2) scale(2,2)');
1222+
});
1223+
1224+
it('modifies the scale of a point', function() {
1225+
el.setAttribute('transform', 'translate(1,2) scale(3,4)');
1226+
Lib.setPointGroupScale(sel, 2, 2);
1227+
expect(el.getAttribute('transform')).toBe('translate(1,2) scale(2,2)');
1228+
});
1229+
1230+
it('does not apply the scale of a point if scale (1, 1)', function() {
1231+
el.setAttribute('transform', 'translate(1,2)');
1232+
Lib.setPointGroupScale(sel, 1, 1);
1233+
expect(el.getAttribute('transform')).toBe('translate(1,2)');
1234+
});
1235+
1236+
it('removes the scale of a point if scale (1, 1)', function() {
1237+
el.setAttribute('transform', 'translate(1,2) scale(3,4)');
1238+
Lib.setPointGroupScale(sel, 1, 1);
1239+
expect(el.getAttribute('transform')).toBe('translate(1,2)');
1240+
});
1241+
});
1242+
12051243
describe('pushUnique', function() {
12061244

12071245
beforeEach(function() {

0 commit comments

Comments
 (0)