Skip to content

Commit eb8e0d0

Browse files
committed
mv axis calcBoundingBox and autoMargin from drawLabels -> drawOne
1 parent 5858067 commit eb8e0d0

File tree

1 file changed

+117
-132
lines changed

1 file changed

+117
-132
lines changed

src/plots/cartesian/axes.js

+117-132
Original file line numberDiff line numberDiff line change
@@ -1694,8 +1694,6 @@ axes.drawOne = function(gd, ax, opts) {
16941694
layer: mainPlotinfo[axLetter + 'axislayer'],
16951695
// TODO shouldn't need this
16961696
shift: ax._mainLinePosition,
1697-
// TODO calcBoundingBox should be taken out of drawLabels
1698-
subplotsWithAx: subplotsWithAx,
16991697
transFn: transFn,
17001698
labelXFn: labelFns.labelXFn,
17011699
labelYFn: labelFns.labelYFn,
@@ -1711,6 +1709,121 @@ axes.drawOne = function(gd, ax, opts) {
17111709
});
17121710
}
17131711

1712+
function extendRange(range, newRange) {
1713+
range[0] = Math.min(range[0], newRange[0]);
1714+
range[1] = Math.max(range[1], newRange[1]);
1715+
}
1716+
1717+
seq.push(function calcBoundingBox() {
1718+
if(ax.showticklabels) {
1719+
var gdBB = gd.getBoundingClientRect();
1720+
var bBox = mainPlotinfo[axLetter + 'axislayer'].node().getBoundingClientRect();
1721+
1722+
/*
1723+
* the way we're going to use this, the positioning that matters
1724+
* is relative to the origin of gd. This is important particularly
1725+
* if gd is scrollable, and may have been scrolled between the time
1726+
* we calculate this and the time we use it
1727+
*/
1728+
1729+
ax._boundingBox = {
1730+
width: bBox.width,
1731+
height: bBox.height,
1732+
left: bBox.left - gdBB.left,
1733+
right: bBox.right - gdBB.left,
1734+
top: bBox.top - gdBB.top,
1735+
bottom: bBox.bottom - gdBB.top
1736+
};
1737+
} else {
1738+
var gs = fullLayout._size;
1739+
var pos;
1740+
1741+
// set dummy bbox for ticklabel-less axes
1742+
1743+
if(axLetter === 'x') {
1744+
pos = ax.anchor === 'free' ?
1745+
gs.t + gs.h * (1 - ax.position) :
1746+
gs.t + gs.h * (1 - ax._anchorAxis.domain[{bottom: 0, top: 1}[ax.side]]);
1747+
1748+
ax._boundingBox = {
1749+
top: pos,
1750+
bottom: pos,
1751+
left: ax._offset,
1752+
right: ax._offset + ax._length,
1753+
width: ax._length,
1754+
height: 0
1755+
};
1756+
} else {
1757+
pos = ax.anchor === 'free' ?
1758+
gs.l + gs.w * ax.position :
1759+
gs.l + gs.w * ax._anchorAxis.domain[{left: 0, right: 1}[ax.side]];
1760+
1761+
ax._boundingBox = {
1762+
left: pos,
1763+
right: pos,
1764+
bottom: ax._offset + ax._length,
1765+
top: ax._offset,
1766+
height: ax._length,
1767+
width: 0
1768+
};
1769+
}
1770+
}
1771+
1772+
/*
1773+
* for spikelines: what's the full domain of positions in the
1774+
* opposite direction that are associated with this axis?
1775+
* This means any axes that we make a subplot with, plus the
1776+
* position of the axis itself if it's free.
1777+
*/
1778+
if(subplotsWithAx) {
1779+
var fullRange = ax._counterSpan = [Infinity, -Infinity];
1780+
1781+
for(var i = 0; i < subplotsWithAx.length; i++) {
1782+
var plotinfo = fullLayout._plots[subplotsWithAx[i]];
1783+
var counterAxis = plotinfo[(axLetter === 'x') ? 'yaxis' : 'xaxis'];
1784+
1785+
extendRange(fullRange, [
1786+
counterAxis._offset,
1787+
counterAxis._offset + counterAxis._length
1788+
]);
1789+
}
1790+
1791+
if(ax.anchor === 'free') {
1792+
extendRange(fullRange, (axLetter === 'x') ?
1793+
[ax._boundingBox.bottom, ax._boundingBox.top] :
1794+
[ax._boundingBox.right, ax._boundingBox.left]);
1795+
}
1796+
}
1797+
});
1798+
1799+
seq.push(function doAutoMargins() {
1800+
var pushKey = ax._name + '.automargin';
1801+
1802+
if(!ax.automargin) {
1803+
Plots.autoMargin(gd, pushKey);
1804+
return;
1805+
}
1806+
1807+
var s = ax.side.charAt(0);
1808+
var push = {x: 0, y: 0, r: 0, l: 0, t: 0, b: 0};
1809+
1810+
if(axLetter === 'x') {
1811+
push.y = (ax.anchor === 'free' ? ax.position :
1812+
ax._anchorAxis.domain[s === 't' ? 1 : 0]);
1813+
push[s] += ax._boundingBox.height;
1814+
} else {
1815+
push.x = (ax.anchor === 'free' ? ax.position :
1816+
ax._anchorAxis.domain[s === 'r' ? 1 : 0]);
1817+
push[s] += ax._boundingBox.width;
1818+
}
1819+
1820+
if(ax.title !== fullLayout._dfltTitle[axLetter]) {
1821+
push[s] += ax.titlefont.size;
1822+
}
1823+
1824+
Plots.autoMargin(gd, pushKey, push);
1825+
});
1826+
17141827
return Lib.syncOrAsync(seq);
17151828
};
17161829

@@ -1909,7 +2022,6 @@ axes.drawZeroLine = function(gd, ax, opts) {
19092022
axes.drawLabels = function(gd, ax, opts) {
19102023
opts = opts || {};
19112024

1912-
var fullLayout = gd._fullLayout;
19132025
var axId = ax._id;
19142026
var axLetter = axId.charAt(0);
19152027
var cls = axId + 'tick';
@@ -1921,15 +2033,10 @@ axes.drawLabels = function(gd, ax, opts) {
19212033
var tickLabels = opts.layer.selectAll('g.' + cls)
19222034
.data(vals, makeDataFn(ax));
19232035

1924-
if(!isNumeric(opts.shift)) {
2036+
if(!isNumeric(opts.shift) || !ax.showticklabels) {
19252037
tickLabels.remove();
19262038
return;
19272039
}
1928-
if(!ax.showticklabels) {
1929-
tickLabels.remove();
1930-
calcBoundingBox();
1931-
return;
1932-
}
19332040

19342041
var maxFontSize = 0;
19352042
var autoangle = 0;
@@ -2099,129 +2206,7 @@ axes.drawLabels = function(gd, ax, opts) {
20992206
}
21002207
}
21012208

2102-
function calcBoundingBox() {
2103-
if(ax.showticklabels) {
2104-
var gdBB = gd.getBoundingClientRect();
2105-
var bBox = opts.layer.node().getBoundingClientRect();
2106-
2107-
/*
2108-
* the way we're going to use this, the positioning that matters
2109-
* is relative to the origin of gd. This is important particularly
2110-
* if gd is scrollable, and may have been scrolled between the time
2111-
* we calculate this and the time we use it
2112-
*/
2113-
2114-
ax._boundingBox = {
2115-
width: bBox.width,
2116-
height: bBox.height,
2117-
left: bBox.left - gdBB.left,
2118-
right: bBox.right - gdBB.left,
2119-
top: bBox.top - gdBB.top,
2120-
bottom: bBox.bottom - gdBB.top
2121-
};
2122-
} else {
2123-
var gs = fullLayout._size;
2124-
var pos;
2125-
2126-
// set dummy bbox for ticklabel-less axes
2127-
2128-
if(axLetter === 'x') {
2129-
pos = ax.anchor === 'free' ?
2130-
gs.t + gs.h * (1 - ax.position) :
2131-
gs.t + gs.h * (1 - ax._anchorAxis.domain[{bottom: 0, top: 1}[ax.side]]);
2132-
2133-
ax._boundingBox = {
2134-
top: pos,
2135-
bottom: pos,
2136-
left: ax._offset,
2137-
right: ax._offset + ax._length,
2138-
width: ax._length,
2139-
height: 0
2140-
};
2141-
} else {
2142-
pos = ax.anchor === 'free' ?
2143-
gs.l + gs.w * ax.position :
2144-
gs.l + gs.w * ax._anchorAxis.domain[{left: 0, right: 1}[ax.side]];
2145-
2146-
ax._boundingBox = {
2147-
left: pos,
2148-
right: pos,
2149-
bottom: ax._offset + ax._length,
2150-
top: ax._offset,
2151-
height: ax._length,
2152-
width: 0
2153-
};
2154-
}
2155-
}
2156-
2157-
var subplotsWithAx = opts.subplotsWithAx;
2158-
2159-
function extendRange(range, newRange) {
2160-
range[0] = Math.min(range[0], newRange[0]);
2161-
range[1] = Math.max(range[1], newRange[1]);
2162-
}
2163-
2164-
/*
2165-
* for spikelines: what's the full domain of positions in the
2166-
* opposite direction that are associated with this axis?
2167-
* This means any axes that we make a subplot with, plus the
2168-
* position of the axis itself if it's free.
2169-
*/
2170-
if(subplotsWithAx) {
2171-
var fullRange = ax._counterSpan = [Infinity, -Infinity];
2172-
2173-
for(var i = 0; i < subplotsWithAx.length; i++) {
2174-
var plotinfo = fullLayout._plots[subplotsWithAx[i]];
2175-
var counterAxis = plotinfo[(axLetter === 'x') ? 'yaxis' : 'xaxis'];
2176-
2177-
extendRange(fullRange, [
2178-
counterAxis._offset,
2179-
counterAxis._offset + counterAxis._length
2180-
]);
2181-
}
2182-
2183-
if(ax.anchor === 'free') {
2184-
extendRange(fullRange, (axLetter === 'x') ?
2185-
[ax._boundingBox.bottom, ax._boundingBox.top] :
2186-
[ax._boundingBox.right, ax._boundingBox.left]);
2187-
}
2188-
}
2189-
}
2190-
2191-
function doAutoMargins() {
2192-
var pushKey = ax._name + '.automargin';
2193-
2194-
if(!ax.automargin) {
2195-
Plots.autoMargin(gd, pushKey);
2196-
return;
2197-
}
2198-
2199-
var s = ax.side.charAt(0);
2200-
var push = {x: 0, y: 0, r: 0, l: 0, t: 0, b: 0};
2201-
2202-
if(axLetter === 'x') {
2203-
push.y = (ax.anchor === 'free' ? ax.position :
2204-
ax._anchorAxis.domain[s === 't' ? 1 : 0]);
2205-
push[s] += ax._boundingBox.height;
2206-
} else {
2207-
push.x = (ax.anchor === 'free' ? ax.position :
2208-
ax._anchorAxis.domain[s === 'r' ? 1 : 0]);
2209-
push[s] += ax._boundingBox.width;
2210-
}
2211-
2212-
if(ax.title !== fullLayout._dfltTitle[axLetter]) {
2213-
push[s] += ax.titlefont.size;
2214-
}
2215-
2216-
Plots.autoMargin(gd, pushKey, push);
2217-
}
2218-
2219-
var done = Lib.syncOrAsync([
2220-
allLabelsReady,
2221-
fixLabelOverlaps,
2222-
calcBoundingBox,
2223-
doAutoMargins
2224-
]);
2209+
var done = Lib.syncOrAsync([allLabelsReady, fixLabelOverlaps]);
22252210
if(done && done.then) gd._promises.push(done);
22262211
return done;
22272212
};

0 commit comments

Comments
 (0)