Skip to content

Commit 7d18677

Browse files
committed
change legend items width calculation if items don't fit in one row
1 parent f49b18e commit 7d18677

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/components/legend/draw.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -608,17 +608,22 @@ function computeLegendDimensions(gd, groups, traces) {
608608
var rowHeight = 0,
609609
maxTraceHeight = 0,
610610
maxTraceWidth = 0,
611-
offsetX = 0;
611+
offsetX = 0,
612+
fullTracesWidth = 0,
613+
traceGap = opts.tracegroupgap || 5,
614+
oneRowLegend;
612615

613616
// calculate largest width for traces and use for width of all legend items
614617
traces.each(function(d) {
615618
maxTraceWidth = Math.max(40 + d[0].width, maxTraceWidth);
619+
fullTracesWidth += 40 + d[0].width + traceGap;
616620
});
617621

622+
// check if legend fits in one row
623+
oneRowLegend = (fullLayout.width - (fullLayout.margin.r + fullLayout.margin.l)) > borderwidth + fullTracesWidth - traceGap;
618624
traces.each(function(d) {
619625
var legendItem = d[0],
620-
traceWidth = maxTraceWidth,
621-
traceGap = opts.tracegroupgap || 5;
626+
traceWidth = oneRowLegend ? 40 + d[0].width : maxTraceWidth;
622627

623628
if((borderwidth + offsetX + traceGap + traceWidth) > (fullLayout.width - (fullLayout.margin.r + fullLayout.margin.l))) {
624629
offsetX = 0;

0 commit comments

Comments
 (0)