Skip to content

Commit

Permalink
Move legend's anchor_utils to Lib and use them in title alignment [882]
Browse files Browse the repository at this point in the history
  • Loading branch information
rmoestl committed Nov 26, 2018
1 parent c2b4c10 commit 86a88e2
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 80 deletions.
21 changes: 10 additions & 11 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ var FROM_BR = alignmentConstants.FROM_BR;
var getLegendData = require('./get_legend_data');
var style = require('./style');
var helpers = require('./helpers');
var anchorUtils = require('./anchor_utils');

var DBLCLICKDELAY = interactConstants.DBLCLICKDELAY;

Expand Down Expand Up @@ -154,17 +153,17 @@ module.exports = function draw(gd) {
lx = gs.l + gs.w * opts.x,
ly = gs.t + gs.h * (1 - opts.y);

if(anchorUtils.isRightAnchor(opts)) {
if(Lib.isRightAnchor(opts)) {
lx -= opts._width;
}
else if(anchorUtils.isCenterAnchor(opts)) {
else if(Lib.isCenterAnchor(opts)) {
lx -= opts._width / 2;
}

if(anchorUtils.isBottomAnchor(opts)) {
if(Lib.isBottomAnchor(opts)) {
ly -= opts._height;
}
else if(anchorUtils.isMiddleAnchor(opts)) {
else if(Lib.isMiddleAnchor(opts)) {
ly -= opts._height / 2;
}

Expand Down Expand Up @@ -699,18 +698,18 @@ function expandMargin(gd) {
opts = fullLayout.legend;

var xanchor = 'left';
if(anchorUtils.isRightAnchor(opts)) {
if(Lib.isRightAnchor(opts)) {
xanchor = 'right';
}
else if(anchorUtils.isCenterAnchor(opts)) {
else if(Lib.isCenterAnchor(opts)) {
xanchor = 'center';
}

var yanchor = 'top';
if(anchorUtils.isBottomAnchor(opts)) {
if(Lib.isBottomAnchor(opts)) {
yanchor = 'bottom';
}
else if(anchorUtils.isMiddleAnchor(opts)) {
else if(Lib.isMiddleAnchor(opts)) {
yanchor = 'middle';
}

Expand All @@ -730,10 +729,10 @@ function expandHorizontalMargin(gd) {
opts = fullLayout.legend;

var xanchor = 'left';
if(anchorUtils.isRightAnchor(opts)) {
if(Lib.isRightAnchor(opts)) {
xanchor = 'right';
}
else if(anchorUtils.isCenterAnchor(opts)) {
else if(Lib.isCenterAnchor(opts)) {
xanchor = 'center';
}

Expand Down
9 changes: 4 additions & 5 deletions src/components/rangeselector/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var Drawing = require('../drawing');
var Lib = require('../../lib');
var svgTextUtils = require('../../lib/svg_text_utils');
var axisIds = require('../../plots/cartesian/axis_ids');
var anchorUtils = require('../legend/anchor_utils');

var alignmentConstants = require('../../constants/alignment');
var LINE_SPACING = alignmentConstants.LINE_SPACING;
Expand Down Expand Up @@ -218,21 +217,21 @@ function reposition(gd, buttons, opts, axName, selector) {
var ly = graphSize.t + graphSize.h * (1 - opts.y);

var xanchor = 'left';
if(anchorUtils.isRightAnchor(opts)) {
if(Lib.isRightAnchor(opts)) {
lx -= width;
xanchor = 'right';
}
if(anchorUtils.isCenterAnchor(opts)) {
if(Lib.isCenterAnchor(opts)) {
lx -= width / 2;
xanchor = 'center';
}

var yanchor = 'top';
if(anchorUtils.isBottomAnchor(opts)) {
if(Lib.isBottomAnchor(opts)) {
ly -= height;
yanchor = 'bottom';
}
if(anchorUtils.isMiddleAnchor(opts)) {
if(Lib.isMiddleAnchor(opts)) {
ly -= height / 2;
yanchor = 'middle';
}
Expand Down
9 changes: 4 additions & 5 deletions src/components/sliders/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var Color = require('../color');
var Drawing = require('../drawing');
var Lib = require('../../lib');
var svgTextUtils = require('../../lib/svg_text_utils');
var anchorUtils = require('../legend/anchor_utils');
var arrayEditor = require('../../plot_api/plot_template').arrayEditor;

var constants = require('./constants');
Expand Down Expand Up @@ -207,21 +206,21 @@ function findDimensions(gd, sliderOpts) {
dims.height = dims.currentValueTotalHeight + constants.tickOffset + sliderOpts.ticklen + constants.labelOffset + dims.labelHeight + sliderOpts.pad.t + sliderOpts.pad.b;

var xanchor = 'left';
if(anchorUtils.isRightAnchor(sliderOpts)) {
if(Lib.isRightAnchor(sliderOpts)) {
dims.lx -= dims.outerLength;
xanchor = 'right';
}
if(anchorUtils.isCenterAnchor(sliderOpts)) {
if(Lib.isCenterAnchor(sliderOpts)) {
dims.lx -= dims.outerLength / 2;
xanchor = 'center';
}

var yanchor = 'top';
if(anchorUtils.isBottomAnchor(sliderOpts)) {
if(Lib.isBottomAnchor(sliderOpts)) {
dims.ly -= dims.height;
yanchor = 'bottom';
}
if(anchorUtils.isMiddleAnchor(sliderOpts)) {
if(Lib.isMiddleAnchor(sliderOpts)) {
dims.ly -= dims.height / 2;
yanchor = 'middle';
}
Expand Down
9 changes: 4 additions & 5 deletions src/components/updatemenus/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var Color = require('../color');
var Drawing = require('../drawing');
var Lib = require('../../lib');
var svgTextUtils = require('../../lib/svg_text_utils');
var anchorUtils = require('../legend/anchor_utils');
var arrayEditor = require('../../plot_api/plot_template').arrayEditor;

var LINE_SPACING = require('../../constants/alignment').LINE_SPACING;
Expand Down Expand Up @@ -566,21 +565,21 @@ function findDimensions(gd, menuOpts) {
dims.ly = graphSize.t + graphSize.h * (1 - menuOpts.y);

var xanchor = 'left';
if(anchorUtils.isRightAnchor(menuOpts)) {
if(Lib.isRightAnchor(menuOpts)) {
dims.lx -= paddedWidth;
xanchor = 'right';
}
if(anchorUtils.isCenterAnchor(menuOpts)) {
if(Lib.isCenterAnchor(menuOpts)) {
dims.lx -= paddedWidth / 2;
xanchor = 'center';
}

var yanchor = 'top';
if(anchorUtils.isBottomAnchor(menuOpts)) {
if(Lib.isBottomAnchor(menuOpts)) {
dims.ly -= paddedHeight;
yanchor = 'bottom';
}
if(anchorUtils.isMiddleAnchor(menuOpts)) {
if(Lib.isMiddleAnchor(menuOpts)) {
dims.ly -= paddedHeight / 2;
yanchor = 'middle';
}
Expand Down
62 changes: 62 additions & 0 deletions src/lib/anchor_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright 2012-2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';


/**
* Determine the position anchor property of x/y xanchor/yanchor components.
*
* - values < 1/3 align the low side at that fraction,
* - values [1/3, 2/3] align the center at that fraction,
* - values > 2/3 align the right at that fraction.
*/


exports.isLeftAnchor = function isLeftAnchor(opts) {
return (
opts.xanchor === 'left' ||
(opts.xanchor === 'auto' && opts.x <= 1 / 3)
);
};

exports.isCenterAnchor = function isCenterAnchor(opts) {
return (
opts.xanchor === 'center' ||
(opts.xanchor === 'auto' && opts.x > 1 / 3 && opts.x < 2 / 3)
);
};

exports.isRightAnchor = function isRightAnchor(opts) {
return (
opts.xanchor === 'right' ||
(opts.xanchor === 'auto' && opts.x >= 2 / 3)
);
};

exports.isTopAnchor = function isTopAnchor(opts) {
return (
opts.yanchor === 'top' ||
(opts.yanchor === 'auto' && opts.y >= 2 / 3)
);
};

exports.isMiddleAnchor = function isMiddleAnchor(opts) {
return (
opts.yanchor === 'middle' ||
(opts.yanchor === 'auto' && opts.y > 1 / 3 && opts.y < 2 / 3)
);
};

exports.isBottomAnchor = function isBottomAnchor(opts) {
return (
opts.yanchor === 'bottom' ||
(opts.yanchor === 'auto' && opts.y <= 1 / 3)
);
};
8 changes: 8 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ lib.pathArc = anglesModule.pathArc;
lib.pathSector = anglesModule.pathSector;
lib.pathAnnulus = anglesModule.pathAnnulus;

var anchorUtils = require('./anchor_utils');
lib.isLeftAnchor = anchorUtils.isLeftAnchor;
lib.isCenterAnchor = anchorUtils.isCenterAnchor;
lib.isRightAnchor = anchorUtils.isRightAnchor;
lib.isTopAnchor = anchorUtils.isTopAnchor;
lib.isMiddleAnchor = anchorUtils.isMiddleAnchor;
lib.isBottomAnchor = anchorUtils.isBottomAnchor;

var geom2dModule = require('./geometry2d');
lib.segmentsIntersect = geom2dModule.segmentsIntersect;
lib.segmentDistance = geom2dModule.segmentDistance;
Expand Down
65 changes: 16 additions & 49 deletions src/plot_api/subroutines.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,62 +515,29 @@ function getMainTitleY(fullLayout, dy) {
}

function getMainTitleTextAnchor(fullLayout) {
var xanchor = fullLayout.title.xanchor;

switch(xanchor) {
case 'auto':
return calcTextAnchor(fullLayout.title.x);
case 'left':
return SVG_TEXT_ANCHOR_START;
case 'right':
return SVG_TEXT_ANCHOR_END;
case 'center':
default:
return SVG_TEXT_ANCHOR_MIDDLE;
}

function calcTextAnchor(x) {
if(x < 1 / 3) {
return SVG_TEXT_ANCHOR_START;
} else if(x > 2 / 3) {
return SVG_TEXT_ANCHOR_END;
}
var title = fullLayout.title;

return SVG_TEXT_ANCHOR_MIDDLE;
var textAnchor = SVG_TEXT_ANCHOR_MIDDLE;
if(Lib.isRightAnchor(title)) {
textAnchor = SVG_TEXT_ANCHOR_END;
} else if(Lib.isLeftAnchor(title)) {
textAnchor = SVG_TEXT_ANCHOR_START;
}

return textAnchor;
}

function getMainTitleDy(fullLayout) {
var yanchor = fullLayout.title.yanchor;
switch(yanchor) {
case 'auto':
return calcDy(fullLayout.title.y);
case 'middle':
return alignmentConstants.MID_SHIFT + 'em';
case 'top':
return alignmentConstants.CAP_SHIFT + 'em';
case 'bottom':
default:
return '0em';
}

function calcDy(y) {

// Imitate behavior before "chart title alignment" was introduced
if(y === 'auto') {
return '0em';
} else if(typeof y === 'number') {
if(y < 1 / 3) {
return '0em';
} else if(y > 2 / 3) {
return alignmentConstants.CAP_SHIFT + 'em';
}

return alignmentConstants.MID_SHIFT + 'em';
}
var title = fullLayout.title;

return 0;
var dy = '0em';
if(Lib.isTopAnchor(title)) {
dy = alignmentConstants.CAP_SHIFT + 'em';
} else if(Lib.isMiddleAnchor(title)) {
dy = alignmentConstants.MID_SHIFT + 'em';
}

return dy;
}

exports.doTraceStyle = function(gd) {
Expand Down
9 changes: 4 additions & 5 deletions test/jasmine/tests/legend_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var DBLCLICKDELAY = require('@src/constants/interactions').DBLCLICKDELAY;
var Legend = require('@src/components/legend');
var getLegendData = require('@src/components/legend/get_legend_data');
var helpers = require('@src/components/legend/helpers');
var anchorUtils = require('@src/components/legend/anchor_utils');

var d3 = require('d3');
var failTest = require('../assets/fail_test');
Expand Down Expand Up @@ -501,7 +500,7 @@ describe('legend anchor utils:', function() {
'use strict';

describe('isRightAnchor', function() {
var isRightAnchor = anchorUtils.isRightAnchor;
var isRightAnchor = Lib.isRightAnchor;
var threshold = 2 / 3;

it('should return true when \'xanchor\' is set to \'right\'', function() {
Expand All @@ -522,7 +521,7 @@ describe('legend anchor utils:', function() {
});

describe('isCenterAnchor', function() {
var isCenterAnchor = anchorUtils.isCenterAnchor;
var isCenterAnchor = Lib.isCenterAnchor;
var threshold0 = 1 / 3;
var threshold1 = 2 / 3;

Expand All @@ -544,7 +543,7 @@ describe('legend anchor utils:', function() {
});

describe('isBottomAnchor', function() {
var isBottomAnchor = anchorUtils.isBottomAnchor;
var isBottomAnchor = Lib.isBottomAnchor;
var threshold = 1 / 3;

it('should return true when \'yanchor\' is set to \'right\'', function() {
Expand All @@ -565,7 +564,7 @@ describe('legend anchor utils:', function() {
});

describe('isMiddleAnchor', function() {
var isMiddleAnchor = anchorUtils.isMiddleAnchor;
var isMiddleAnchor = Lib.isMiddleAnchor;
var threshold0 = 1 / 3;
var threshold1 = 2 / 3;

Expand Down

0 comments on commit 86a88e2

Please sign in to comment.