From 429c6fd4e035b60b34d2386cd94d24d78753d38d Mon Sep 17 00:00:00 2001 From: Ricky Reusser Date: Wed, 20 Jul 2016 22:24:20 -0400 Subject: [PATCH 1/3] Simple attempt at non-scaling points --- src/lib/index.js | 21 +++++++++++++++++++++ src/plots/cartesian/dragbox.js | 5 ++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/lib/index.js b/src/lib/index.js index 0a3d8bf7854..6a537bf3e3b 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -484,6 +484,27 @@ lib.setScale = function(element, x, y) { return transform; }; +lib.setPointScale = function(selection, x, y) { + x = x || 1; + y = y || 1; + + // The same scale transform for every point: + var scale = ' scale(' + x + ',' + y + ')'; + + // A regex to strip any existing scale: + var re = /sc.*/; + + selection.each(function(p) { + // Get the transform: + var t = this.getAttribute('transform').replace(re,''); + + // Append the scale transform + this.setAttribute('transform', t + scale); + }); + + return scale; +}; + lib.isIE = function() { return typeof window.navigator.msSaveBlob !== 'undefined'; }; diff --git a/src/plots/cartesian/dragbox.js b/src/plots/cartesian/dragbox.js index ff6f7bc52bf..2a585519c80 100644 --- a/src/plots/cartesian/dragbox.js +++ b/src/plots/cartesian/dragbox.js @@ -633,7 +633,10 @@ module.exports = function dragBox(gd, plotinfo, x, y, w, h, ns, ew) { subplot.plot .call(Lib.setTranslate, plotDx, plotDy) - .call(Lib.setScale, xScaleFactor, yScaleFactor); + .call(Lib.setScale, xScaleFactor, yScaleFactor) + .selectAll('.points').selectAll('.point') + .call(Lib.setPointScale, 1 / xScaleFactor, 1 / yScaleFactor); + } } From 4bd2987f8e5c1f71fd5ae3d6449a372d7f68ba66 Mon Sep 17 00:00:00 2001 From: Ricky Reusser Date: Wed, 20 Jul 2016 22:35:55 -0400 Subject: [PATCH 2/3] Fix lint error --- src/lib/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/index.js b/src/lib/index.js index 6a537bf3e3b..1c226b5ab05 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -494,9 +494,9 @@ lib.setPointScale = function(selection, x, y) { // A regex to strip any existing scale: var re = /sc.*/; - selection.each(function(p) { + selection.each(function() { // Get the transform: - var t = this.getAttribute('transform').replace(re,''); + var t = this.getAttribute('transform').replace(re, ''); // Append the scale transform this.setAttribute('transform', t + scale); From f786b4f91ed9f2e1dc27637d63c583d4e98b55c1 Mon Sep 17 00:00:00 2001 From: Ricky Reusser Date: Thu, 21 Jul 2016 12:52:26 -0400 Subject: [PATCH 3/3] Add setPointGroupScale tests --- src/lib/index.js | 20 ++++++++++++------ src/plots/cartesian/dragbox.js | 7 +++++-- test/jasmine/tests/lib_test.js | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/lib/index.js b/src/lib/index.js index 1c226b5ab05..9f3686462e4 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -484,22 +484,30 @@ lib.setScale = function(element, x, y) { return transform; }; -lib.setPointScale = function(selection, x, y) { +lib.setPointGroupScale = function(selection, x, y) { + var t, scale, re; + x = x || 1; y = y || 1; - // The same scale transform for every point: - var scale = ' scale(' + x + ',' + y + ')'; + if(x === 1 && y === 1) { + scale = ''; + } else { + // The same scale transform for every point: + scale = ' scale(' + x + ',' + y + ')'; + } // A regex to strip any existing scale: - var re = /sc.*/; + re = /\s*sc.*/; selection.each(function() { // Get the transform: - var t = this.getAttribute('transform').replace(re, ''); + t = (this.getAttribute('transform') || '').replace(re, ''); + t += scale; + t = t.trim(); // Append the scale transform - this.setAttribute('transform', t + scale); + this.setAttribute('transform', t); }); return scale; diff --git a/src/plots/cartesian/dragbox.js b/src/plots/cartesian/dragbox.js index 2a585519c80..5b18574482f 100644 --- a/src/plots/cartesian/dragbox.js +++ b/src/plots/cartesian/dragbox.js @@ -634,9 +634,12 @@ module.exports = function dragBox(gd, plotinfo, x, y, w, h, ns, ew) { subplot.plot .call(Lib.setTranslate, plotDx, plotDy) .call(Lib.setScale, xScaleFactor, yScaleFactor) - .selectAll('.points').selectAll('.point') - .call(Lib.setPointScale, 1 / xScaleFactor, 1 / yScaleFactor); + // This is specifically directed at scatter traces, applying an inverse + // scale to individual points to counteract the scale of the trace + // as a whole: + .selectAll('.points').selectAll('.point') + .call(Lib.setPointGroupScale, 1 / xScaleFactor, 1 / yScaleFactor); } } diff --git a/test/jasmine/tests/lib_test.js b/test/jasmine/tests/lib_test.js index 02c0595b094..b25821f9fd0 100644 --- a/test/jasmine/tests/lib_test.js +++ b/test/jasmine/tests/lib_test.js @@ -1202,6 +1202,44 @@ describe('Test lib.js:', function() { }); }); + describe('setPointGroupScale', function() { + var el, sel; + + beforeEach(function() { + el = document.createElement('div'); + sel = d3.select(el); + }); + + it('sets the scale of a point', function() { + Lib.setPointGroupScale(sel, 2, 2); + expect(el.getAttribute('transform')).toBe('scale(2,2)'); + }); + + it('appends the scale of a point', function() { + el.setAttribute('transform', 'translate(1,2)'); + Lib.setPointGroupScale(sel, 2, 2); + expect(el.getAttribute('transform')).toBe('translate(1,2) scale(2,2)'); + }); + + it('modifies the scale of a point', function() { + el.setAttribute('transform', 'translate(1,2) scale(3,4)'); + Lib.setPointGroupScale(sel, 2, 2); + expect(el.getAttribute('transform')).toBe('translate(1,2) scale(2,2)'); + }); + + it('does not apply the scale of a point if scale (1, 1)', function() { + el.setAttribute('transform', 'translate(1,2)'); + Lib.setPointGroupScale(sel, 1, 1); + expect(el.getAttribute('transform')).toBe('translate(1,2)'); + }); + + it('removes the scale of a point if scale (1, 1)', function() { + el.setAttribute('transform', 'translate(1,2) scale(3,4)'); + Lib.setPointGroupScale(sel, 1, 1); + expect(el.getAttribute('transform')).toBe('translate(1,2)'); + }); + }); + describe('pushUnique', function() { beforeEach(function() {