Skip to content

Commit 06a4a0f

Browse files
committed
margin option now accept an object in order to fine tune x and y offset of text position
1 parent d1b3cef commit 06a4a0f

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

js/jquery.mapael.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -1839,36 +1839,47 @@
18391839
* Get the coordinates of the text relative to a bbox and a position
18401840
* @param bbox the boundary box of the element
18411841
* @param textPosition the wanted text position (inner, right, left, top or bottom)
1842+
* @param margin number or object {x: val, y:val} margin between the bbox and the text
18421843
*/
18431844
getTextPosition: function (bbox, textPosition, margin) {
18441845
var textX = 0;
18451846
var textY = 0;
18461847
var textAnchor = "";
18471848

1849+
if (typeof margin === "number") {
1850+
if (textPosition === "bottom" || textPosition === "top") {
1851+
margin = {x: 0, y: margin};
1852+
} else if (textPosition === "right" || textPosition === "left") {
1853+
margin = {x: margin, y: 0};
1854+
} else {
1855+
margin = {x: 0, y: 0};
1856+
}
1857+
}
1858+
18481859
switch (textPosition) {
18491860
case "bottom" :
1850-
textX = (bbox.x + bbox.x2) / 2;
1851-
textY = bbox.y2 + margin;
1861+
textX = ((bbox.x + bbox.x2) / 2) + margin.x;
1862+
textY = bbox.y2 + margin.y;
18521863
textAnchor = "middle";
18531864
break;
18541865
case "top" :
1855-
textX = (bbox.x + bbox.x2) / 2;
1856-
textY = bbox.y - margin;
1866+
textX = ((bbox.x + bbox.x2) / 2) + margin.x;
1867+
textY = bbox.y - margin.y;
18571868
textAnchor = "middle";
18581869
break;
18591870
case "left" :
1860-
textX = bbox.x - margin;
1861-
textY = (bbox.y + bbox.y2) / 2;
1871+
textX = bbox.x - margin.x;
1872+
textY = ((bbox.y + bbox.y2) / 2) + margin.y;
18621873
textAnchor = "end";
18631874
break;
18641875
case "right" :
1865-
textX = bbox.x2 + margin;
1866-
textY = (bbox.y + bbox.y2) / 2;
1876+
textX = bbox.x2 + margin.x;
1877+
textY = ((bbox.y + bbox.y2) / 2) + margin.y;
18671878
textAnchor = "start";
18681879
break;
18691880
default : // "inner" position
1870-
textX = (bbox.x + bbox.x2) / 2;
1871-
textY = (bbox.y + bbox.y2) / 2;
1881+
textX = ((bbox.x + bbox.x2) / 2) + margin.x;
1882+
textY = ((bbox.y + bbox.y2) / 2) + margin.y;
18721883
textAnchor = "middle";
18731884
}
18741885
return {"x": textX, "y": textY, "textAnchor": textAnchor};

0 commit comments

Comments
 (0)