Skip to content

Commit a3aebfe

Browse files
authored
Merge pull request plotly#762 from plotly/non-scaling-points
Non-scaling SVG scatter points
2 parents 3393334 + f786b4f commit a3aebfe

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

src/lib/index.js

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

487+
lib.setPointGroupScale = function(selection, x, y) {
488+
var t, scale, re;
489+
490+
x = x || 1;
491+
y = y || 1;
492+
493+
if(x === 1 && y === 1) {
494+
scale = '';
495+
} else {
496+
// The same scale transform for every point:
497+
scale = ' scale(' + x + ',' + y + ')';
498+
}
499+
500+
// A regex to strip any existing scale:
501+
re = /\s*sc.*/;
502+
503+
selection.each(function() {
504+
// Get the transform:
505+
t = (this.getAttribute('transform') || '').replace(re, '');
506+
t += scale;
507+
t = t.trim();
508+
509+
// Append the scale transform
510+
this.setAttribute('transform', t);
511+
});
512+
513+
return scale;
514+
};
515+
487516
lib.isIE = function() {
488517
return typeof window.navigator.msSaveBlob !== 'undefined';
489518
};

src/plots/cartesian/dragbox.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,13 @@ module.exports = function dragBox(gd, plotinfo, x, y, w, h, ns, ew) {
633633

634634
subplot.plot
635635
.call(Lib.setTranslate, plotDx, plotDy)
636-
.call(Lib.setScale, xScaleFactor, yScaleFactor);
636+
.call(Lib.setScale, xScaleFactor, yScaleFactor)
637+
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);
637643
}
638644
}
639645

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)