Skip to content

Non-scaling SVG scatter points #762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 21, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 29 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,35 @@ lib.setScale = function(element, x, y) {
return transform;
};

lib.setPointGroupScale = function(selection, x, y) {
var t, scale, re;

x = x || 1;
y = y || 1;

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:
re = /\s*sc.*/;

selection.each(function() {
// Get the transform:
t = (this.getAttribute('transform') || '').replace(re, '');
t += scale;
t = t.trim();

// Append the scale transform
this.setAttribute('transform', t);
});

return scale;
};

lib.isIE = function() {
return typeof window.navigator.msSaveBlob !== 'undefined';
};
Expand Down
8 changes: 7 additions & 1 deletion src/plots/cartesian/dragbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,13 @@ 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)

// 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);
}
}

Expand Down
38 changes: 38 additions & 0 deletions test/jasmine/tests/lib_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down