|
1839 | 1839 | * Get the coordinates of the text relative to a bbox and a position
|
1840 | 1840 | * @param bbox the boundary box of the element
|
1841 | 1841 | * @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 |
1842 | 1843 | */
|
1843 | 1844 | getTextPosition: function (bbox, textPosition, margin) {
|
1844 | 1845 | var textX = 0;
|
1845 | 1846 | var textY = 0;
|
1846 | 1847 | var textAnchor = "";
|
1847 | 1848 |
|
| 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 | + |
1848 | 1859 | switch (textPosition) {
|
1849 | 1860 | 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; |
1852 | 1863 | textAnchor = "middle";
|
1853 | 1864 | break;
|
1854 | 1865 | 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; |
1857 | 1868 | textAnchor = "middle";
|
1858 | 1869 | break;
|
1859 | 1870 | 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; |
1862 | 1873 | textAnchor = "end";
|
1863 | 1874 | break;
|
1864 | 1875 | 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; |
1867 | 1878 | textAnchor = "start";
|
1868 | 1879 | break;
|
1869 | 1880 | 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; |
1872 | 1883 | textAnchor = "middle";
|
1873 | 1884 | }
|
1874 | 1885 | return {"x": textX, "y": textY, "textAnchor": textAnchor};
|
|
0 commit comments