Skip to content

Commit 3d82007

Browse files
committed
Fix error when no title is defined for legend
If no title was defined in `legend.area.title`, a JS error `Line 907: TypeError: title.getBBox is not a function` was thrown. Here is a fix where we test the existence of the title before using its size.
1 parent 29aacb4 commit 3d82007

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

js/jquery.mapael.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@
842842
, paper = {}
843843
, width = 0
844844
, height = 0
845-
, title = {}
845+
, title = null
846846
, elem = {}
847847
, elemBBox = {}
848848
, label = {}
@@ -861,7 +861,7 @@
861861
height = width = 0;
862862

863863
// Set the title of the legend
864-
if(legendOptions.title) {
864+
if(legendOptions.title && legendOptions.title !== "") {
865865
title = paper.text(legendOptions.marginLeftTitle, 0, legendOptions.title).attr(legendOptions.titleAttrs);
866866
title.attr({y : 0.5 * title.getBBox().height});
867867

@@ -871,6 +871,8 @@
871871

872872
// Calculate attrs (and width, height and r (radius)) for legend elements, and yCenter for horizontal legends
873873
for(i = 0, length = legendOptions.slices.length; i < length; ++i) {
874+
var current_yCenter = 0;
875+
874876
if (typeof legendOptions.slices[i].legendSpecificAttrs == "undefined")
875877
legendOptions.slices[i].legendSpecificAttrs = {};
876878

@@ -901,11 +903,19 @@
901903
sliceAttrs[i].r = legendOptions.slices[i].size / 2;
902904
}
903905

906+
// Compute yCenter for this legend slice
907+
current_yCenter = legendOptions.marginBottomTitle;
908+
// Add title height if it exists
909+
if (title) {
910+
current_yCenter += title.getBBox().height;
911+
}
904912
if(legendType == "plot" && (typeof legendOptions.slices[i].type == "undefined" || legendOptions.slices[i].type == "circle")) {
905-
yCenter = Math.max(yCenter, legendOptions.marginBottomTitle + title.getBBox().height + scale * sliceAttrs[i].r);
913+
current_yCenter += scale * sliceAttrs[i].r;
906914
} else {
907-
yCenter = Math.max(yCenter, legendOptions.marginBottomTitle + title.getBBox().height + scale * sliceAttrs[i].height/2);
915+
current_yCenter += scale * sliceAttrs[i].height/2;
908916
}
917+
// Update yCenter if current larger
918+
yCenter = Math.max(yCenter, current_yCenter);
909919
}
910920

911921
if (legendOptions.mode == "horizontal") {
@@ -983,12 +993,16 @@
983993

984994
// Update the width and height for the paper
985995
if (legendOptions.mode == "horizontal") {
996+
var current_height = legendOptions.marginBottom + elemBBox.height;
986997
width += legendOptions.marginLeft + elemBBox.width + legendOptions.marginLeftLabel + label.getBBox().width;
987-
if(legendOptions.slices[i].type == "image" || legendType == "area") {
988-
height = Math.max(height, legendOptions.marginBottom + title.getBBox().height + elemBBox.height);
989-
} else {
990-
height = Math.max(height, legendOptions.marginBottomTitle + legendOptions.marginBottom + title.getBBox().height + elemBBox.height);
998+
if(legendOptions.slices[i].type != "image" && legendType != "area") {
999+
current_height += legendOptions.marginBottomTitle;
1000+
}
1001+
// Add title height if it exists
1002+
if (title) {
1003+
current_height += title.getBBox().height;
9911004
}
1005+
height = Math.max(height, current_height);
9921006
} else {
9931007
width = Math.max(width, legendOptions.marginLeft + elemBBox.width + legendOptions.marginLeftLabel + label.getBBox().width);
9941008
height += legendOptions.marginBottom + elemBBox.height;

0 commit comments

Comments
 (0)