Skip to content

Commit 919d8c8

Browse files
authored
Merge pull request #1 from psalmody/h-legend-wrap
Legend item wrap with layout.legend.orientation = h
2 parents a3aebfe + 7ef2dff commit 919d8c8

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/components/legend/draw.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -568,18 +568,40 @@ function computeLegendDimensions(gd, groups, traces) {
568568
else {
569569
opts.width = 0;
570570
opts.height = 0;
571+
var rowHeight = 0,
572+
maxTraceHeight = 0,
573+
maxTraceWidth = 0,
574+
offsetX = 0;
571575

576+
//calculate largest width for traces and use for width of all legend items
572577
traces.each(function(d) {
578+
maxTraceWidth = Math.max(40 + d[0].width, maxTraceWidth);
579+
});
580+
581+
traces.each(function(d) {
582+
573583
var legendItem = d[0],
574-
traceWidth = 40 + legendItem.width,
584+
traceWidth = maxTraceWidth,
575585
traceGap = opts.tracegroupgap || 5;
576586

587+
if((borderwidth + offsetX + traceGap + traceWidth) > (fullLayout.width - (fullLayout.margin.r + fullLayout.margin.l))) {
588+
offsetX = 0;
589+
rowHeight = rowHeight + maxTraceHeight;
590+
opts.height = opts.height + maxTraceHeight;
591+
//reset for next row
592+
maxTraceHeight = 0;
593+
}
594+
577595
Lib.setTranslate(this,
578-
(borderwidth + opts.width),
579-
(5 + borderwidth + legendItem.height / 2));
596+
(borderwidth + offsetX),
597+
(5 + borderwidth + legendItem.height / 2) + rowHeight);
580598

581599
opts.width += traceGap + traceWidth;
582600
opts.height = Math.max(opts.height, legendItem.height);
601+
602+
//keep track of tallest trace in group
603+
offsetX += traceGap + traceWidth;
604+
maxTraceHeight = Math.max(legendItem.height, maxTraceHeight);
583605
});
584606

585607
opts.width += borderwidth * 2;

0 commit comments

Comments
 (0)