Skip to content

Commit 82b9769

Browse files
committed
calc max name/label length in getLegendData
... during calcdata loop, instead of in Legend.draw this allows us to 🔪 one loop over the data that plus some linting.
1 parent 1cf347a commit 82b9769

File tree

2 files changed

+27
-43
lines changed

2 files changed

+27
-43
lines changed

src/components/legend/draw.js

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,7 @@ module.exports = function draw(gd) {
4545
if(!fullLayout.showlegend || !legendData.length) {
4646
fullLayout._infolayer.selectAll('.legend').remove();
4747
fullLayout._topdefs.select('#' + clipId).remove();
48-
49-
Plots.autoMargin(gd, 'legend');
50-
return;
51-
}
52-
53-
var maxLength = 0;
54-
for(var i = 0; i < legendData.length; i++) {
55-
for(var j = 0; j < legendData[i].length; j++) {
56-
var item = legendData[i][j][0];
57-
var trace = item.trace;
58-
var isPieLike = Registry.traceIs(trace, 'pie-like');
59-
var name = isPieLike ? item.label : trace.name;
60-
maxLength = Math.max(maxLength, name && name.length || 0);
61-
}
48+
return Plots.autoMargin(gd, 'legend');
6249
}
6350

6451
var legend = Lib.ensureSingle(fullLayout._infolayer, 'g', 'legend', function(s) {
@@ -72,7 +59,6 @@ module.exports = function draw(gd) {
7259
var bg = Lib.ensureSingle(legend, 'rect', 'bg', function(s) {
7360
s.attr('shape-rendering', 'crispEdges');
7461
});
75-
7662
bg.call(Color.stroke, opts.bordercolor)
7763
.call(Color.fill, opts.bgcolor)
7864
.style('stroke-width', opts.borderwidth + 'px');
@@ -89,17 +75,11 @@ module.exports = function draw(gd) {
8975
.call(Color.fill, '#808BA4');
9076
});
9177

92-
var groups = scrollBox.selectAll('g.groups')
93-
.data(legendData);
94-
95-
groups.enter().append('g')
96-
.attr('class', 'groups');
97-
78+
var groups = scrollBox.selectAll('g.groups').data(legendData);
79+
groups.enter().append('g').attr('class', 'groups');
9880
groups.exit().remove();
9981

100-
var traces = groups.selectAll('g.traces')
101-
.data(Lib.identity);
102-
82+
var traces = groups.selectAll('g.traces').data(Lib.identity);
10383
traces.enter().append('g').attr('class', 'traces');
10484
traces.exit().remove();
10585

@@ -111,15 +91,9 @@ module.exports = function draw(gd) {
11191
return trace.visible === 'legendonly' ? 0.5 : 1;
11292
}
11393
})
114-
.each(function() {
115-
d3.select(this)
116-
.call(drawTexts, gd, maxLength);
117-
})
94+
.each(function() { d3.select(this).call(drawTexts, gd); })
11895
.call(style, gd)
119-
.each(function() {
120-
d3.select(this)
121-
.call(setupTraceToggle, gd);
122-
});
96+
.each(function() { d3.select(this).call(setupTraceToggle, gd); });
12397

12498
Lib.syncOrAsync([Plots.previousPromises,
12599
function() {
@@ -296,9 +270,7 @@ module.exports = function draw(gd) {
296270
constants.scrollBarWidth,
297271
scrollBarHeight
298272
);
299-
clipPath.select('rect').attr({
300-
y: opts.borderwidth + scrollBoxY
301-
});
273+
clipPath.select('rect').attr('y', bw + scrollBoxY);
302274
}
303275

304276
if(gd._context.edits.legendPosition) {
@@ -384,13 +356,15 @@ function clickOrDoubleClick(gd, legend, legendItem, numClicks, evt) {
384356
}
385357
}
386358

387-
function drawTexts(g, gd, maxLength) {
359+
function drawTexts(g, gd) {
388360
var legendItem = g.data()[0][0];
389361
var fullLayout = gd._fullLayout;
362+
var opts = fullLayout.legend;
390363
var trace = legendItem.trace;
391364
var isPieLike = Registry.traceIs(trace, 'pie-like');
392365
var traceIndex = trace.index;
393366
var isEditable = gd._context.edits.legendText && !isPieLike;
367+
var maxNameLength = opts._maxNameLength;
394368

395369
var name = isPieLike ? legendItem.label : trace.name;
396370
if(trace._meta) {
@@ -402,7 +376,7 @@ function drawTexts(g, gd, maxLength) {
402376
textEl.attr('text-anchor', 'start')
403377
.classed('user-select-none', true)
404378
.call(Drawing.font, fullLayout.legend.font)
405-
.text(isEditable ? ensureLength(name, maxLength) : name);
379+
.text(isEditable ? ensureLength(name, maxNameLength) : name);
406380

407381
svgTextUtils.positionText(textEl, constants.textOffsetX, 0);
408382

@@ -416,7 +390,7 @@ function drawTexts(g, gd, maxLength) {
416390
textEl.call(svgTextUtils.makeEditable, {gd: gd, text: name})
417391
.call(textLayout)
418392
.on('edit', function(newName) {
419-
this.text(ensureLength(newName, maxLength))
393+
this.text(ensureLength(newName, maxNameLength))
420394
.call(textLayout);
421395

422396
var fullInput = legendItem.trace._fullInput || {};

src/components/legend/get_legend_data.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@ module.exports = function getLegendData(calcdata, opts) {
1717
var hasOneNonBlankGroup = false;
1818
var slicesShown = {};
1919
var lgroupi = 0;
20+
var maxNameLength = 0;
2021
var i, j;
2122

2223
function addOneItem(legendGroup, legendItem) {
2324
// each '' legend group is treated as a separate group
2425
if(legendGroup === '' || !helpers.isGrouped(opts)) {
25-
var uniqueGroup = '~~i' + lgroupi; // TODO: check this against fullData legendgroups?
26-
26+
// TODO: check this against fullData legendgroups?
27+
var uniqueGroup = '~~i' + lgroupi;
2728
lgroups.push(uniqueGroup);
2829
lgroupToTraces[uniqueGroup] = [[legendItem]];
2930
lgroupi++;
3031
} else if(lgroups.indexOf(legendGroup) === -1) {
3132
lgroups.push(legendGroup);
3233
hasOneNonBlankGroup = true;
3334
lgroupToTraces[legendGroup] = [[legendItem]];
34-
} else lgroupToTraces[legendGroup].push([legendItem]);
35+
} else {
36+
lgroupToTraces[legendGroup].push([legendItem]);
37+
}
3538
}
3639

3740
// build an { legendgroup: [cd0, cd0], ... } object
@@ -59,9 +62,13 @@ module.exports = function getLegendData(calcdata, opts) {
5962
});
6063

6164
slicesShown[lgroup][labelj] = true;
65+
maxNameLength = Math.max(maxNameLength, (labelj || '').length);
6266
}
6367
}
64-
} else addOneItem(lgroup, cd0);
68+
} else {
69+
addOneItem(lgroup, cd0);
70+
maxNameLength = Math.max(maxNameLength, (trace.name || '').length);
71+
}
6572
}
6673

6774
// won't draw a legend in this case
@@ -90,7 +97,10 @@ module.exports = function getLegendData(calcdata, opts) {
9097
lgroupsLength = 1;
9198
}
9299

93-
// needed in repositionLegend
100+
// number of legend groups - needed in legend/draw.js
94101
opts._lgroupsLength = lgroupsLength;
102+
// maximum name/label length - needed in legend/draw.js
103+
opts._maxNameLength = maxNameLength;
104+
95105
return legendData;
96106
};

0 commit comments

Comments
 (0)