Skip to content

Commit a9759a2

Browse files
committed
RE: etienne review
- stash _titleWidth and _titleHeight once in _fullLayout.legend - bring back test for prune unsupported global-level test and use parcoords instead of choropleth - reuse getGradientDirection function to handle reversescale case - reserve new lines for Lib.coerce argumnets not ternary operator - add a jasmine test for legend.title.side relayout
1 parent 264044f commit a9759a2

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed

src/components/legend/draw.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ module.exports = function draw(gd) {
6464
.style('stroke-width', opts.borderwidth + 'px');
6565

6666

67-
var title = fullLayout.legend.title;
68-
gd._fullLayout._legendTitleWidth = 0;
69-
gd._fullLayout._legendTitleHeight = 0;
67+
var title = opts.title;
68+
opts._titleWidth = 0;
69+
opts._titleHeight = 0;
7070
if(title.text) {
7171
var titleEl = Lib.ensureSingle(legend, 'text', 'legendtitletext');
7272
titleEl.attr('text-anchor', 'start')
@@ -383,7 +383,7 @@ function drawTexts(g, gd) {
383383

384384
textEl.attr('text-anchor', 'start')
385385
.classed('user-select-none', true)
386-
.call(Drawing.font, fullLayout.legend.font)
386+
.call(Drawing.font, opts.font)
387387
.text(isEditable ? ensureLength(name, maxNameLength) : name);
388388

389389
svgTextUtils.positionText(textEl, constants.textGap, 0);
@@ -483,10 +483,8 @@ function computeTextDimensions(g, gd) {
483483
var mathjaxGroup = g.select('g[class*=math-group]');
484484
var mathjaxNode = mathjaxGroup.node();
485485
var bw = gd._fullLayout.legend.borderwidth;
486-
var opts = legendItem ?
487-
gd._fullLayout.legend :
488-
gd._fullLayout.legend.title;
489-
var lineHeight = opts.font.size * LINE_SPACING;
486+
var opts = gd._fullLayout.legend;
487+
var lineHeight = (legendItem ? opts : opts.title).font.size * LINE_SPACING;
490488
var height, width;
491489

492490
if(mathjaxNode) {
@@ -525,11 +523,11 @@ function computeTextDimensions(g, gd) {
525523
legendItem.height = Math.max(height, 16) + 3;
526524
legendItem.width = width;
527525
} else { // case of title
528-
if(opts.side.indexOf('left') !== -1) {
529-
gd._fullLayout._legendTitleWidth = width;
526+
if(opts.title.side.indexOf('left') !== -1) {
527+
opts._titleWidth = width;
530528
}
531-
if(opts.side.indexOf('top') !== -1) {
532-
gd._fullLayout._legendTitleHeight = height;
529+
if(opts.title.side.indexOf('top') !== -1) {
530+
opts._titleHeight = height;
533531
}
534532
}
535533
}
@@ -548,8 +546,6 @@ function computeLegendDimensions(gd, groups, traces) {
548546
var fullLayout = gd._fullLayout;
549547
var opts = fullLayout.legend;
550548
var gs = fullLayout._size;
551-
opts._titleWidth = fullLayout._legendTitleWidth;
552-
opts._titleHeight = fullLayout._legendTitleHeight;
553549

554550
var isVertical = helpers.isVertical(opts);
555551
var isGrouped = helpers.isGrouped(opts);

src/components/legend/style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ module.exports = function style(s, gd) {
573573
if(s.size()) {
574574
var gradientID = 'legendfill-' + trace.uid;
575575
Drawing.gradient(s, gd, gradientID,
576-
reversescale ? 'horizontal' : 'horizontalreversed',
576+
getGradientDirection(reversescale),
577577
colorscale, 'fill');
578578
}
579579
};

src/plots/plots.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,9 +1292,7 @@ plots.supplyTraceDefaults = function(traceIn, traceOut, colorIndex, layout, trac
12921292

12931293
if(Registry.traceIs(traceOut, 'showLegend')) {
12941294
Lib.coerce(traceIn, traceOut,
1295-
_module.attributes.showlegend ?
1296-
_module.attributes :
1297-
plots.attributes,
1295+
_module.attributes.showlegend ? _module.attributes : plots.attributes,
12981296
'showlegend'
12991297
);
13001298

test/jasmine/bundle_tests/plotschema_test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ describe('plot schema', function() {
372372
var traces = Plotly.PlotSchema.get().traces;
373373

374374
expect(traces.sankey.attributes.hoverinfo.flags.length).toBe(0);
375+
expect(traces.parcoords.attributes.showlegend).toBe(undefined, 'no legend attrs for parcoords (for now)');
375376
expect(traces.table.attributes.opacity).toBe(undefined, 'no opacity attr for table');
376377
expect(traces.parcoords.attributes.hoverinfo).toBe(undefined, 'no hover attrs for parcoords');
377378
expect(traces.scatter3d.attributes.selectedpoints).toBe(undefined, 'no selectedpoints for gl3d traces');

test/jasmine/tests/legend_test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,42 @@ describe('legend relayout update', function() {
781781
.catch(failTest)
782782
.then(done);
783783
});
784+
785+
it('should fit in graph viewport when changing legend.title.side', function(done) {
786+
var fig = Lib.extendDeep({}, require('@mocks/0.json'));
787+
fig.layout.legend = {
788+
title: {
789+
text: 'legend title'
790+
}
791+
};
792+
793+
function _assert(msg, xy, wh) {
794+
return function() {
795+
var fullLayout = gd._fullLayout;
796+
var legend3 = d3.select('g.legend');
797+
var bg3 = legend3.select('rect.bg');
798+
var translate = Drawing.getTranslate(legend3);
799+
var x = translate.x;
800+
var y = translate.y;
801+
var w = +bg3.attr('width');
802+
var h = +bg3.attr('height');
803+
804+
expect([x, y]).toBeWithinArray(xy, 25, msg + '| legend x,y');
805+
expect([w, h]).toBeWithinArray(wh, 25, msg + '| legend w,h');
806+
expect(x + w <= fullLayout.width).toBe(true, msg + '| fits in x');
807+
expect(y + h <= fullLayout.height).toBe(true, msg + '| fits in y');
808+
};
809+
}
810+
811+
Plotly.plot(gd, fig)
812+
.then(_assert('base', [667.72, 60], [120, 83]))
813+
.then(function() { return Plotly.relayout(gd, 'legend.title.side', 'left'); })
814+
.then(_assert('after relayout to *left*', [607.54, 60], [180, 67]))
815+
.then(function() { return Plotly.relayout(gd, 'legend.title.side', 'top'); })
816+
.then(_assert('after relayout to *top*', [667.72, 60], [120, 83]))
817+
.catch(failTest)
818+
.then(done);
819+
});
784820
});
785821

786822
describe('legend orientation change:', function() {

0 commit comments

Comments
 (0)