Skip to content

Commit 543d6b0

Browse files
committed
code cleanup in lsInner
no functionality changes here, just simplifying and linting
1 parent 5b495e6 commit 543d6b0

File tree

1 file changed

+81
-76
lines changed

1 file changed

+81
-76
lines changed

src/plot_api/subroutines.js

+81-76
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ function overlappingDomain(xDomain, yDomain, domains) {
4343
exports.lsInner = function(gd) {
4444
var fullLayout = gd._fullLayout;
4545
var gs = fullLayout._size;
46+
var pad = gs.p;
4647
var axList = Plotly.Axes.list(gd);
48+
var hasSVGCartesian = fullLayout._has('cartesian');
4749
var i;
4850

4951
// clear axis line positions, to be set in the subplot loop below
@@ -122,7 +124,7 @@ exports.lsInner = function(gd) {
122124
fullLayout._plots[subplot].bg = d3.select(this);
123125
});
124126

125-
var freefinished = [];
127+
var freeFinished = {};
126128
subplotSelection.each(function(subplot) {
127129
var plotinfo = fullLayout._plots[subplot];
128130
var xa = plotinfo.xaxis;
@@ -132,11 +134,11 @@ exports.lsInner = function(gd) {
132134
xa.setScale();
133135
ya.setScale();
134136

135-
if(plotinfo.bg && fullLayout._has('cartesian')) {
137+
if(plotinfo.bg && hasSVGCartesian) {
136138
plotinfo.bg
137139
.call(Drawing.setRect,
138-
xa._offset - gs.p, ya._offset - gs.p,
139-
xa._length + 2 * gs.p, ya._length + 2 * gs.p)
140+
xa._offset - pad, ya._offset - pad,
141+
xa._length + 2 * pad, ya._length + 2 * pad)
140142
.call(Color.fill, fullLayout.plot_bgcolor)
141143
.style('stroke-width', 0);
142144
}
@@ -162,129 +164,132 @@ exports.lsInner = function(gd) {
162164
});
163165

164166

165-
plotinfo.plot.call(Drawing.setTranslate, xa._offset, ya._offset);
166-
plotinfo.plot.call(Drawing.setClipUrl, plotinfo.clipId);
167+
plotinfo.plot
168+
.call(Drawing.setTranslate, xa._offset, ya._offset)
169+
.call(Drawing.setClipUrl, plotinfo.clipId);
170+
171+
function shouldShowLine(ax, counterAx, side) {
172+
return (ax.anchor === counterAx._id && (ax.mirror || ax.side === side)) ||
173+
ax.mirror === 'all' || ax.mirror === 'allticks' ||
174+
(ax.mirrors && ax.mirrors[counterAx._id + side]);
175+
}
176+
177+
var showFreeX = xa.anchor === 'free' && !freeFinished[xa._id];
178+
var showBottom = shouldShowLine(xa, ya, 'bottom');
179+
var showTop = shouldShowLine(xa, ya, 'top');
180+
181+
var showFreeY = ya.anchor === 'free' && !freeFinished[ya._id];
182+
var showLeft = shouldShowLine(ya, xa, 'left');
183+
var showRight = shouldShowLine(ya, xa, 'right');
167184

168185
var xlw = Drawing.crispRound(gd, xa.linewidth, 1);
169186
var ylw = Drawing.crispRound(gd, ya.linewidth, 1);
170-
var xp = gs.p + ylw;
171-
var xpathPrefix = 'M' + (-xp) + ',';
172-
var xpathSuffix = 'h' + (xa._length + 2 * xp);
173-
var showfreex = xa.anchor === 'free' &&
174-
freefinished.indexOf(xa._id) === -1;
175-
var freeposx = gs.h * (1 - (xa.position||0)) + ((xlw / 2) % 1);
176-
var showbottom =
177-
(xa.anchor === ya._id && (xa.mirror || xa.side !== 'top')) ||
178-
xa.mirror === 'all' || xa.mirror === 'allticks' ||
179-
(xa.mirrors && xa.mirrors[ya._id + 'bottom']);
180-
var bottompos = ya._length + gs.p + xlw / 2;
181-
var showtop =
182-
(xa.anchor === ya._id && (xa.mirror || xa.side === 'top')) ||
183-
xa.mirror === 'all' || xa.mirror === 'allticks' ||
184-
(xa.mirrors && xa.mirrors[ya._id + 'top']);
185-
var toppos = -gs.p - xlw / 2;
187+
188+
// TODO: this gets more complicated with multiple x and y axes
189+
var xLinesXLeft = -pad - ylw;
190+
var xLinesXRight = xa._length + pad + ylw;
191+
var xLinesYFree = gs.h * (1 - (xa.position || 0)) + ((xlw / 2) % 1);
192+
var xLinesYBottom = ya._length + pad + xlw / 2;
193+
var xLinesYTop = -pad - xlw / 2;
186194

187195
// shorten y axis lines so they don't overlap x axis lines
188-
var yp = gs.p;
189196
// except where there's no x line
190197
// TODO: this gets more complicated with multiple x and y axes
191-
var ypbottom = showbottom ? 0 : xlw;
192-
var yptop = showtop ? 0 : xlw;
193-
var ypathSuffix = ',' + (-yp - yptop) +
194-
'v' + (ya._length + 2 * yp + yptop + ypbottom);
195-
var showfreey = ya.anchor === 'free' &&
196-
freefinished.indexOf(ya._id) === -1;
197-
var freeposy = gs.w * (ya.position||0) + ((ylw / 2) % 1);
198-
var showleft =
199-
(ya.anchor === xa._id && (ya.mirror || ya.side !== 'right')) ||
200-
ya.mirror === 'all' || ya.mirror === 'allticks' ||
201-
(ya.mirrors && ya.mirrors[xa._id + 'left']);
202-
var leftpos = -gs.p - ylw / 2;
203-
var showright =
204-
(ya.anchor === xa._id && (ya.mirror || ya.side === 'right')) ||
205-
ya.mirror === 'all' || ya.mirror === 'allticks' ||
206-
(ya.mirrors && ya.mirrors[xa._id + 'right']);
207-
var rightpos = xa._length + gs.p + ylw / 2;
198+
var yLinesYBottom = ya._length + (showBottom ? 0 : xlw) + pad;
199+
var yLinesYTop = (showTop ? 0 : -xlw) - pad;
200+
var yLinesXFree = gs.w * (ya.position || 0) + ((ylw / 2) % 1);
201+
var yLinesXLeft = -pad - ylw / 2;
202+
var yLinesXRight = xa._length + pad + ylw / 2;
203+
204+
function xLinePath(y, showThis) {
205+
if(!showThis) return '';
206+
return 'M' + xLinesXLeft + ',' + y + 'H' + xLinesXRight;
207+
}
208+
209+
function yLinePath(x, showThis) {
210+
if(!showThis) return '';
211+
return 'M' + x + ',' + yLinesYTop + 'V' + yLinesYBottom;
212+
}
208213

209214
// save axis line positions for ticks, draggers, etc to reference
210215
// each subplot gets an entry:
211216
// [left or bottom, right or top, free, main]
212217
// main is the position at which to draw labels and draggers, if any
213218
xa._linepositions[subplot] = [
214-
showbottom ? bottompos : undefined,
215-
showtop ? toppos : undefined,
216-
showfreex ? freeposx : undefined
219+
showBottom ? xLinesYBottom : undefined,
220+
showTop ? xLinesYTop : undefined,
221+
showFreeX ? xLinesYFree : undefined
217222
];
218223
if(xa.anchor === ya._id) {
219224
xa._linepositions[subplot][3] = xa.side === 'top' ?
220-
toppos : bottompos;
225+
xLinesYTop : xLinesYBottom;
221226
}
222-
else if(showfreex) {
223-
xa._linepositions[subplot][3] = freeposx;
227+
else if(showFreeX) {
228+
xa._linepositions[subplot][3] = xLinesYFree;
224229
}
225230

226231
ya._linepositions[subplot] = [
227-
showleft ? leftpos : undefined,
228-
showright ? rightpos : undefined,
229-
showfreey ? freeposy : undefined
232+
showLeft ? yLinesXLeft : undefined,
233+
showRight ? yLinesXRight : undefined,
234+
showFreeY ? yLinesXFree : undefined
230235
];
231236
if(ya.anchor === xa._id) {
232237
ya._linepositions[subplot][3] = ya.side === 'right' ?
233-
rightpos : leftpos;
238+
yLinesXRight : yLinesXLeft;
234239
}
235-
else if(showfreey) {
236-
ya._linepositions[subplot][3] = freeposy;
240+
else if(showFreeY) {
241+
ya._linepositions[subplot][3] = yLinesXFree;
237242
}
238243

239244
// translate all the extra stuff to have the
240245
// same origin as the plot area or axes
241246
var origin = 'translate(' + xa._offset + ',' + ya._offset + ')';
242-
var originx = origin;
243-
var originy = origin;
244-
if(showfreex) {
245-
originx = 'translate(' + xa._offset + ',' + gs.t + ')';
246-
toppos += ya._offset - gs.t;
247-
bottompos += ya._offset - gs.t;
247+
var originX = origin;
248+
var originY = origin;
249+
if(showFreeX) {
250+
originX = 'translate(' + xa._offset + ',' + gs.t + ')';
251+
xLinesYTop += ya._offset - gs.t;
252+
xLinesYBottom += ya._offset - gs.t;
248253
}
249-
if(showfreey) {
250-
originy = 'translate(' + gs.l + ',' + ya._offset + ')';
251-
leftpos += xa._offset - gs.l;
252-
rightpos += xa._offset - gs.l;
254+
if(showFreeY) {
255+
originY = 'translate(' + gs.l + ',' + ya._offset + ')';
256+
yLinesXLeft += xa._offset - gs.l;
257+
yLinesXRight += xa._offset - gs.l;
253258
}
254259

255-
if(fullLayout._has('cartesian')) {
260+
if(hasSVGCartesian) {
256261
plotinfo.xlines
257-
.attr('transform', originx)
262+
.attr('transform', originX)
258263
.attr('d', (
259-
(showbottom ? (xpathPrefix + bottompos + xpathSuffix) : '') +
260-
(showtop ? (xpathPrefix + toppos + xpathSuffix) : '') +
261-
(showfreex ? (xpathPrefix + freeposx + xpathSuffix) : '')) ||
264+
xLinePath(xLinesYBottom, showBottom) +
265+
xLinePath(xLinesYTop, showTop) +
266+
xLinePath(xLinesYFree, showFreeX)) ||
262267
// so it doesn't barf with no lines shown
263268
'M0,0')
264269
.style('stroke-width', xlw + 'px')
265270
.call(Color.stroke, xa.showline ?
266271
xa.linecolor : 'rgba(0,0,0,0)');
267272
plotinfo.ylines
268-
.attr('transform', originy)
273+
.attr('transform', originY)
269274
.attr('d', (
270-
(showleft ? ('M' + leftpos + ypathSuffix) : '') +
271-
(showright ? ('M' + rightpos + ypathSuffix) : '') +
272-
(showfreey ? ('M' + freeposy + ypathSuffix) : '')) ||
275+
yLinePath(yLinesXLeft, showLeft) +
276+
yLinePath(yLinesXRight, showRight) +
277+
yLinePath(yLinesXFree, showFreeY)) ||
273278
'M0,0')
274-
.attr('stroke-width', ylw + 'px')
279+
.style('stroke-width', ylw + 'px')
275280
.call(Color.stroke, ya.showline ?
276281
ya.linecolor : 'rgba(0,0,0,0)');
277282
}
278283

279-
plotinfo.xaxislayer.attr('transform', originx);
280-
plotinfo.yaxislayer.attr('transform', originy);
284+
plotinfo.xaxislayer.attr('transform', originX);
285+
plotinfo.yaxislayer.attr('transform', originY);
281286
plotinfo.gridlayer.attr('transform', origin);
282287
plotinfo.zerolinelayer.attr('transform', origin);
283288
plotinfo.draglayer.attr('transform', origin);
284289

285290
// mark free axes as displayed, so we don't draw them again
286-
if(showfreex) { freefinished.push(xa._id); }
287-
if(showfreey) { freefinished.push(ya._id); }
291+
if(showFreeX) freeFinished[xa._id] = 1;
292+
if(showFreeY) freeFinished[ya._id] = 1;
288293
});
289294

290295
Plotly.Axes.makeClipPaths(gd);

0 commit comments

Comments
 (0)