Skip to content

Commit b3af445

Browse files
committed
move auto-anchor logic to own module + add tests
1 parent a40fc83 commit b3af445

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

src/components/legend/anchor_utils.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
13+
/**
14+
* Determine the position anchor property of x/y xanchor/yanchor components.
15+
*
16+
* - values < 1/3 align the low side at that fraction,
17+
* - values [1/3, 2/3] align the center at that fraction,
18+
* - values > 2/3 align the right at that fraction.
19+
*/
20+
21+
exports.isRightAnchor = function isRightAnchor(opts) {
22+
return (
23+
opts.xanchor === 'right' ||
24+
(opts.xanchor === 'auto' && opts.x >= 2 / 3)
25+
);
26+
};
27+
28+
exports.isCenterAnchor = function isCenterAnchor(opts) {
29+
return (
30+
opts.xanchor === 'center' ||
31+
(opts.xanchor === 'auto' && opts.x > 1 / 3 && opts.x < 2 / 3)
32+
);
33+
};
34+
35+
exports.isBottomAnchor = function isTopAnchor(opts) {
36+
return (
37+
opts.yanchor === 'bottom' ||
38+
(opts.yanchor === 'auto' && opts.y <= 1 / 3)
39+
);
40+
};
41+
42+
exports.isMiddleAnchor = function isMiddleAnchor(opts) {
43+
return (
44+
opts.yanchor === 'middle' ||
45+
(opts.yanchor === 'auto' && opts.y > 1 / 3 && opts.y < 2 / 3)
46+
);
47+
};

test/jasmine/tests/legend_test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var Legend = require('@src/components/legend');
22
var Plots = require('@src/plots/plots');
33

44
var helpers = require('@src/components/legend/helpers');
5+
var anchorUtils = require('@src/components/legend/anchor_utils');
56
describe('Test legend:', function() {
67
'use strict';
78

@@ -361,4 +362,90 @@ describe('Test legend:', function() {
361362
});
362363
});
363364

365+
describe('isRightAnchor anchor util', function() {
366+
var isRightAnchor = anchorUtils.isRightAnchor;
367+
var threshold = 2/3;
368+
369+
it('should return true when \'xanchor\' is set to \'right\'', function() {
370+
expect(isRightAnchor({ xanchor: 'left' })).toBe(false);
371+
expect(isRightAnchor({ xanchor: 'center' })).toBe(false);
372+
expect(isRightAnchor({ xanchor: 'right' })).toBe(true);
373+
});
374+
375+
it('should return true when \'xanchor\' is set to \'auto\' and \'x\' >= 2/3', function() {
376+
var opts = { xanchor: 'auto' };
377+
378+
[0, 0.4, 0.7, 1].forEach(function(v) {
379+
opts.x = v;
380+
expect(isRightAnchor(opts))
381+
.toBe(v > threshold, 'case ' + v);
382+
});
383+
});
384+
});
385+
386+
describe('isCenterAnchor anchor util', function() {
387+
var isCenterAnchor = anchorUtils.isCenterAnchor;
388+
var threshold0 = 1/3;
389+
var threshold1 = 2/3;
390+
391+
it('should return true when \'xanchor\' is set to \'center\'', function() {
392+
expect(isCenterAnchor({ xanchor: 'left' })).toBe(false);
393+
expect(isCenterAnchor({ xanchor: 'center' })).toBe(true);
394+
expect(isCenterAnchor({ xanchor: 'right' })).toBe(false);
395+
});
396+
397+
it('should return true when \'xanchor\' is set to \'auto\' and 1/3 < \'x\' < 2/3', function() {
398+
var opts = { xanchor: 'auto' };
399+
400+
[0, 0.4, 0.7, 1].forEach(function(v) {
401+
opts.x = v;
402+
expect(isCenterAnchor(opts))
403+
.toBe(v > threshold0 && v < threshold1, 'case ' + v);
404+
});
405+
});
406+
});
407+
408+
describe('isBottomAnchor anchor util', function() {
409+
var isBottomAnchor = anchorUtils.isBottomAnchor;
410+
var threshold = 1/3;
411+
412+
it('should return true when \'yanchor\' is set to \'right\'', function() {
413+
expect(isBottomAnchor({ yanchor: 'top' })).toBe(false);
414+
expect(isBottomAnchor({ yanchor: 'middle' })).toBe(false);
415+
expect(isBottomAnchor({ yanchor: 'bottom' })).toBe(true);
416+
});
417+
418+
it('should return true when \'yanchor\' is set to \'auto\' and \'y\' <= 1/3', function() {
419+
var opts = { yanchor: 'auto' };
420+
421+
[0, 0.4, 0.7, 1].forEach(function(v) {
422+
opts.y = v;
423+
expect(isBottomAnchor(opts))
424+
.toBe(v < threshold, 'case ' + v);
425+
});
426+
});
427+
});
428+
429+
describe('isMiddleAnchor anchor util', function() {
430+
var isMiddleAnchor = anchorUtils.isMiddleAnchor;
431+
var threshold0 = 1/3;
432+
var threshold1 = 2/3;
433+
434+
it('should return true when \'yanchor\' is set to \'center\'', function() {
435+
expect(isMiddleAnchor({ yanchor: 'top' })).toBe(false);
436+
expect(isMiddleAnchor({ yanchor: 'middle' })).toBe(true);
437+
expect(isMiddleAnchor({ yanchor: 'bottom' })).toBe(false);
438+
});
439+
440+
it('should return true when \'yanchor\' is set to \'auto\' and 1/3 < \'y\' < 2/3', function() {
441+
var opts = { yanchor: 'auto' };
442+
443+
[0, 0.4, 0.7, 1].forEach(function(v) {
444+
opts.y = v;
445+
expect(isMiddleAnchor(opts))
446+
.toBe(v > threshold0 && v < threshold1, 'case ' + v);
447+
});
448+
});
449+
});
450+
364451
});

0 commit comments

Comments
 (0)