Skip to content

Commit 180b6b9

Browse files
authored
Merge pull request #2829 from plotly/legend-horiz-edit-position-fixups
Position-editable horizontal legend toggle fixups
2 parents dcaebc2 + 5449f30 commit 180b6b9

File tree

2 files changed

+92
-9
lines changed

2 files changed

+92
-9
lines changed

src/components/legend/draw.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,13 @@ module.exports = function draw(gd) {
341341
}
342342
},
343343
clickFn: function(numClicks, e) {
344-
var clickedTrace =
345-
fullLayout._infolayer.selectAll('g.traces').filter(function() {
346-
var bbox = this.getBoundingClientRect();
347-
return (e.clientX >= bbox.left && e.clientX <= bbox.right &&
348-
e.clientY >= bbox.top && e.clientY <= bbox.bottom);
349-
});
344+
var clickedTrace = fullLayout._infolayer.selectAll('g.traces').filter(function() {
345+
var bbox = this.getBoundingClientRect();
346+
return (
347+
e.clientX >= bbox.left && e.clientX <= bbox.right &&
348+
e.clientY >= bbox.top && e.clientY <= bbox.bottom
349+
);
350+
});
350351
if(clickedTrace.size() > 0) {
351352
clickOrDoubleClick(gd, legend, clickedTrace, numClicks, e);
352353
}
@@ -672,14 +673,19 @@ function computeLegendDimensions(gd, groups, traces) {
672673
opts._width = Math.ceil(opts._width);
673674
opts._height = Math.ceil(opts._height);
674675

676+
var isEditable = (
677+
gd._context.edits.legendText ||
678+
gd._context.edits.legendPosition
679+
);
680+
675681
traces.each(function(d) {
676-
var legendItem = d[0],
677-
bg = d3.select(this).select('.legendtoggle');
682+
var legendItem = d[0];
683+
var bg = d3.select(this).select('.legendtoggle');
678684

679685
Drawing.setRect(bg,
680686
0,
681687
-legendItem.height / 2,
682-
(gd._context.edits.legendText ? 0 : opts._width) + extraWidth,
688+
(isEditable ? 0 : opts._width) + extraWidth,
683689
legendItem.height
684690
);
685691
});

test/jasmine/tests/legend_test.js

+77
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var anchorUtils = require('@src/components/legend/anchor_utils');
1010

1111
var d3 = require('d3');
1212
var failTest = require('../assets/fail_test');
13+
var mouseEvent = require('../assets/mouse_event');
1314
var delay = require('../assets/delay');
1415
var createGraphDiv = require('../assets/create_graph_div');
1516
var destroyGraphDiv = require('../assets/destroy_graph_div');
@@ -929,6 +930,7 @@ describe('legend interaction', function() {
929930

930931
describe('editable mode interactions', function() {
931932
var gd;
933+
932934
var mock = {
933935
data: [{
934936
x: [1, 2, 3],
@@ -1027,6 +1029,81 @@ describe('legend interaction', function() {
10271029
});
10281030
});
10291031

1032+
describe('visible toggle', function() {
1033+
var gd;
1034+
1035+
beforeEach(function() {
1036+
gd = createGraphDiv();
1037+
});
1038+
1039+
afterEach(destroyGraphDiv);
1040+
1041+
var data = [
1042+
{y: [1, 2, 1]},
1043+
{y: [2, 1, 2]},
1044+
{y: [2, 3, 4]}
1045+
];
1046+
1047+
// we need to click on the drag cover to truly test this,
1048+
function clickAt(p) {
1049+
return function() {
1050+
return new Promise(function(resolve) {
1051+
var el = d3.select('g.legend').node();
1052+
var opts = {element: el};
1053+
mouseEvent('mousedown', p[0], p[1], opts);
1054+
mouseEvent('mouseup', p[0], p[1], opts);
1055+
setTimeout(resolve, DBLCLICKDELAY + 20);
1056+
});
1057+
};
1058+
}
1059+
1060+
function assertVisible(expectation) {
1061+
return function() {
1062+
var actual = gd._fullData.map(function(t) { return t.visible; });
1063+
expect(actual).toEqual(expectation);
1064+
};
1065+
}
1066+
1067+
var specs = [{
1068+
orientation: 'h',
1069+
edits: {legendPosition: true},
1070+
clickPos: [[118, 469], [212, 469], [295, 469]]
1071+
}, {
1072+
orientation: 'h',
1073+
edits: {legendPosition: true, legendText: true},
1074+
clickPos: [[118, 469], [212, 469], [295, 469]]
1075+
}, {
1076+
orientation: 'v',
1077+
edits: {legendPosition: true},
1078+
clickPos: [[430, 114], [430, 131], [430, 153]]
1079+
}, {
1080+
orientation: 'v',
1081+
edits: {legendPosition: true, legendText: true},
1082+
clickPos: [[430, 114], [430, 131], [430, 153]]
1083+
}];
1084+
1085+
specs.forEach(function(s) {
1086+
var msg = s.orientation + ' - ' + JSON.stringify(s.edits);
1087+
1088+
it('should find correct bounding box (case ' + msg + ')', function(done) {
1089+
Plotly.plot(gd,
1090+
Lib.extendDeep([], data),
1091+
{legend: {orientation: s.orientation}, width: 500, height: 500},
1092+
{edits: s.edits}
1093+
)
1094+
.then(assertVisible([true, true, true]))
1095+
.then(clickAt(s.clickPos[0]))
1096+
.then(assertVisible(['legendonly', true, true]))
1097+
.then(clickAt(s.clickPos[1]))
1098+
.then(assertVisible(['legendonly', 'legendonly', true]))
1099+
.then(clickAt(s.clickPos[2]))
1100+
.then(assertVisible(['legendonly', 'legendonly', 'legendonly']))
1101+
.catch(failTest)
1102+
.then(done);
1103+
});
1104+
});
1105+
});
1106+
10301107
describe('legend visibility interactions', function() {
10311108
var gd;
10321109

0 commit comments

Comments
 (0)