Skip to content

Commit b3765b4

Browse files
committed
expand legend area to include long titles
1 parent a9759a2 commit b3765b4

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

src/components/legend/draw.js

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -523,13 +523,26 @@ function computeTextDimensions(g, gd) {
523523
legendItem.height = Math.max(height, 16) + 3;
524524
legendItem.width = width;
525525
} else { // case of title
526-
if(opts.title.side.indexOf('left') !== -1) {
527-
opts._titleWidth = width;
526+
opts._titleWidth = width;
527+
opts._titleHeight = height;
528+
}
529+
}
530+
531+
function getTitleSize(opts) {
532+
var w = 0;
533+
var h = 0;
534+
535+
var side = opts.title.side;
536+
if(side) {
537+
if(side.indexOf('left') !== -1) {
538+
w = opts._titleWidth;
528539
}
529-
if(opts.title.side.indexOf('top') !== -1) {
530-
opts._titleHeight = height;
540+
if(side.indexOf('top') !== -1) {
541+
h = opts._titleHeight;
531542
}
532543
}
544+
545+
return [w, h];
533546
}
534547

535548
/*
@@ -570,13 +583,14 @@ function computeLegendDimensions(gd, groups, traces) {
570583
var toggleRectWidth = 0;
571584
opts._width = 0;
572585
opts._height = 0;
586+
var titleSize = getTitleSize(opts);
573587

574588
if(isVertical) {
575589
traces.each(function(d) {
576590
var h = d[0].height;
577591
Drawing.setTranslate(this,
578-
bw + opts._titleWidth,
579-
bw + opts._titleHeight + opts._height + h / 2 + itemGap
592+
bw + titleSize[0],
593+
bw + titleSize[1] + opts._height + h / 2 + itemGap
580594
);
581595
opts._height += h;
582596
opts._width = Math.max(opts._width, d[0].width);
@@ -628,8 +642,8 @@ function computeLegendDimensions(gd, groups, traces) {
628642
d3.select(this).selectAll('g.traces').each(function(d) {
629643
var h = d[0].height;
630644
Drawing.setTranslate(this,
631-
opts._titleWidth,
632-
bw + opts._titleHeight + itemGap + h / 2 + offsetY
645+
titleSize[0],
646+
titleSize[1] + bw + itemGap + h / 2 + offsetY
633647
);
634648
offsetY += h;
635649
maxWidthInGroup = Math.max(maxWidthInGroup, textGap + d[0].width);
@@ -674,8 +688,8 @@ function computeLegendDimensions(gd, groups, traces) {
674688
}
675689

676690
Drawing.setTranslate(this,
677-
bw + opts._titleWidth + offsetX,
678-
bw + opts._titleHeight + offsetY + h / 2 + itemGap
691+
titleSize[0] + bw + offsetX,
692+
titleSize[1] + bw + offsetY + h / 2 + itemGap
679693
);
680694

681695
rowWidth = offsetX + w + itemGap;
@@ -693,8 +707,19 @@ function computeLegendDimensions(gd, groups, traces) {
693707
}
694708
}
695709

696-
opts._width = Math.ceil(opts._width + opts._titleWidth);
697-
opts._height = Math.ceil(opts._height + opts._titleHeight);
710+
opts._width = Math.ceil(
711+
Math.max(
712+
opts._width + titleSize[0],
713+
opts._titleWidth + 2 * (bw + constants.titlePad)
714+
)
715+
);
716+
717+
opts._height = Math.ceil(
718+
Math.max(
719+
opts._height + titleSize[1],
720+
opts._titleHeight + 2 * (bw + constants.itemGap)
721+
)
722+
);
698723

699724
opts._effHeight = Math.min(opts._height, opts._maxHeight);
700725

-1.64 KB
Loading
411 Bytes
Loading

test/image/mocks/geo_choropleth-legend.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"bgcolor": "yellow",
4848
"orientation": "h",
4949
"title": {
50-
"text": "<b>World</b> <i>Countries</i>",
50+
"text": "<b>World</b> <i>Countries</i><br>legends on <sub>different lines!</sub>",
5151
"font": {
5252
"size": 15,
5353
"family": "Times New Roman"

test/image/mocks/geo_choropleth-usa_legend.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@
110110
"WY"
111111
],
112112
"colorbar": {
113-
"title": "Millions USD"
113+
"title": {
114+
"text": "Millions USD"
115+
}
114116
},
115117
"type": "choropleth",
116118
"z": [
@@ -174,6 +176,11 @@
174176
"title": {
175177
"text": "2011 US Agriculture Exports by State"
176178
},
179+
"legend": {
180+
"title": {
181+
"text": "2011 US Agriculture<br>Exports by State"
182+
}
183+
},
177184
"height": 250,
178185
"width": 450,
179186
"margin": {

0 commit comments

Comments
 (0)