Skip to content

Commit 7e55b51

Browse files
committed
group all Axes.makeLabelFns functions in one object
1 parent 06a3c0e commit 7e55b51

File tree

4 files changed

+31
-49
lines changed

4 files changed

+31
-49
lines changed

src/components/colorbar/draw.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,6 @@ module.exports = function draw(gd, id) {
434434

435435
var vals = Axes.calcTicks(cbAxisOut);
436436
var transFn = Axes.makeTransFn(cbAxisOut);
437-
var labelFns = Axes.makeLabelFns(cbAxisOut, shift);
438437
var tickSign = Axes.getTickSigns(cbAxisOut)[2];
439438

440439
Axes.drawTicks(gd, cbAxisOut, {
@@ -448,9 +447,7 @@ module.exports = function draw(gd, id) {
448447
vals: vals,
449448
layer: axisLayer,
450449
transFn: transFn,
451-
labelXFn: labelFns.labelXFn,
452-
labelYFn: labelFns.labelYFn,
453-
labelAnchorFn: labelFns.labelAnchorFn
450+
labelFns: Axes.makeLabelFns(cbAxisOut, shift)
454451
});
455452
},
456453
function() {

src/plots/cartesian/axes.js

+23-30
Original file line numberDiff line numberDiff line change
@@ -1802,14 +1802,11 @@ axes.drawOne = function(gd, ax, opts) {
18021802
// TODO: mirror labels, esp for subplots
18031803

18041804
seq.push(function() {
1805-
var labelFns = axes.makeLabelFns(ax, mainLinePosition);
18061805
return axes.drawLabels(gd, ax, {
18071806
vals: vals,
18081807
layer: mainAxLayer,
18091808
transFn: transFn,
1810-
labelXFn: labelFns.labelXFn,
1811-
labelYFn: labelFns.labelYFn,
1812-
labelAnchorFn: labelFns.labelAnchorFn,
1809+
labelFns: axes.makeLabelFns(ax, mainLinePosition)
18131810
});
18141811
});
18151812

@@ -1821,8 +1818,6 @@ axes.drawOne = function(gd, ax, opts) {
18211818
seq.push(function() {
18221819
labelLength += getLabelLevelSpan(ax, axId + 'tick') + pad;
18231820
labelLength += ax._tickAngles[axId + 'tick'] ? ax.tickfont.size * LINE_SPACING : 0;
1824-
var secondaryPosition = mainLinePosition + labelLength * sgn;
1825-
var secondaryLabelFns = axes.makeLabelFns(ax, secondaryPosition);
18261821

18271822
return axes.drawLabels(gd, ax, {
18281823
vals: getSecondaryLabelVals(ax, vals),
@@ -1831,9 +1826,7 @@ axes.drawOne = function(gd, ax, opts) {
18311826
repositionOnUpdate: true,
18321827
secondary: true,
18331828
transFn: transFn,
1834-
labelXFn: secondaryLabelFns.labelXFn,
1835-
labelYFn: secondaryLabelFns.labelYFn,
1836-
labelAnchorFn: secondaryLabelFns.labelAnchorFn,
1829+
labelFns: axes.makeLabelFns(ax, mainLinePosition + labelLength * sgn)
18371830
});
18381831
});
18391832

@@ -2178,9 +2171,9 @@ axes.makeTickPath = function(ax, shift, sgn, len) {
21782171
* @param {number} shift
21792172
* @param {number} angle [in degrees] ...
21802173
* @return {object}
2181-
* - {fn} labelXFn
2182-
* - {fn} labelYFn
2183-
* - {fn} labelAnchorFn
2174+
* - {fn} xFn
2175+
* - {fn} yFn
2176+
* - {fn} anchorFn
21842177
* - {number} labelStandoff (gap parallel to ticks)
21852178
* - {number} labelShift (gap perpendicular to ticks)
21862179
*/
@@ -2210,15 +2203,16 @@ axes.makeLabelFns = function(ax, shift, angle) {
22102203
};
22112204

22122205
var x0, y0, ff, flipIt;
2206+
22132207
if(axLetter === 'x') {
22142208
flipIt = ax.side === 'bottom' ? 1 : -1;
22152209
x0 = labelShift * flipIt;
22162210
y0 = shift + labelStandoff * flipIt;
22172211
ff = ax.side === 'bottom' ? 1 : -0.2;
22182212

2219-
out.labelXFn = function(d) { return d.dx + x0; };
2220-
out.labelYFn = function(d) { return d.dy + y0 + d.fontSize * ff; };
2221-
out.labelAnchorFn = function(a) {
2213+
out.xFn = function(d) { return d.dx + x0; };
2214+
out.yFn = function(d) { return d.dy + y0 + d.fontSize * ff; };
2215+
out.anchorFn = function(d, a) {
22222216
if(!isNumeric(a) || a === 0 || a === 180) {
22232217
return 'middle';
22242218
}
@@ -2230,9 +2224,9 @@ axes.makeLabelFns = function(ax, shift, angle) {
22302224
y0 = -labelShift * flipIt;
22312225
ff = Math.abs(ax.tickangle) === 90 ? 0.5 : 0;
22322226

2233-
out.labelXFn = function(d) { return d.dx + shift + (x0 + d.fontSize * ff) * flipIt; };
2234-
out.labelYFn = function(d) { return d.dy + y0 + d.fontSize * MID_SHIFT; };
2235-
out.labelAnchorFn = function(a) {
2227+
out.xFn = function(d) { return d.dx + shift + (x0 + d.fontSize * ff) * flipIt; };
2228+
out.yFn = function(d) { return d.dy + y0 + d.fontSize * MID_SHIFT; };
2229+
out.anchorFn = function(d, a) {
22362230
if(isNumeric(a) && Math.abs(a) === 90) {
22372231
return 'middle';
22382232
}
@@ -2411,10 +2405,11 @@ axes.drawZeroLine = function(gd, ax, opts) {
24112405
* - {string (optional)} cls (node className)
24122406
* - {boolean} repositionOnUpdate (set to true to reposition update selection)
24132407
* - {boolean} secondary
2414-
* - {fn} transFn
2415-
* - {fn} labelXFn
2416-
* - {fn} labelYFn
2417-
* - {fn} labelAnchorFn
2408+
* - {object} labelFns
2409+
* + {fn} transFn
2410+
* + {fn} labelXFn
2411+
* + {fn} labelYFn
2412+
* + {fn} labelAnchorFn
24182413
*/
24192414
axes.drawLabels = function(gd, ax, opts) {
24202415
opts = opts || {};
@@ -2423,9 +2418,7 @@ axes.drawLabels = function(gd, ax, opts) {
24232418
var axLetter = axId.charAt(0);
24242419
var cls = opts.cls || axId + 'tick';
24252420
var vals = opts.vals;
2426-
var labelXFn = opts.labelXFn;
2427-
var labelYFn = opts.labelYFn;
2428-
var labelAnchorFn = opts.labelAnchorFn;
2421+
var labelFns = opts.labelFns;
24292422
var tickAngle = opts.secondary ? 0 : ax.tickangle;
24302423
var lastAngle = (ax._tickAngles || {})[cls];
24312424

@@ -2445,7 +2438,7 @@ axes.drawLabels = function(gd, ax, opts) {
24452438
var newPromise = gd._promises.length;
24462439

24472440
thisLabel
2448-
.call(svgTextUtils.positionText, labelXFn(d), labelYFn(d))
2441+
.call(svgTextUtils.positionText, labelFns.xFn(d), labelFns.yFn(d))
24492442
.call(Drawing.font, d.font, d.fontSize, d.fontColor)
24502443
.text(d.text)
24512444
.call(svgTextUtils.convertToTspans, gd);
@@ -2469,7 +2462,7 @@ axes.drawLabels = function(gd, ax, opts) {
24692462
if(opts.repositionOnUpdate) {
24702463
tickLabels.each(function(d) {
24712464
d3.select(this).select('text')
2472-
.call(svgTextUtils.positionText, labelXFn(d), labelYFn(d));
2465+
.call(svgTextUtils.positionText, labelFns.xFn(d), labelFns.yFn(d));
24732466
});
24742467
}
24752468

@@ -2497,12 +2490,12 @@ axes.drawLabels = function(gd, ax, opts) {
24972490
s.each(function(d) {
24982491
var thisLabel = d3.select(this);
24992492
var mathjaxGroup = thisLabel.select('.text-math-group');
2500-
var anchor = labelAnchorFn(angle, d);
2493+
var anchor = labelFns.anchorFn(d, angle);
25012494

25022495
var transform = opts.transFn.call(thisLabel.node(), d) +
25032496
((isNumeric(angle) && +angle !== 0) ?
2504-
(' rotate(' + angle + ',' + labelXFn(d) + ',' +
2505-
(labelYFn(d) - d.fontSize / 2) + ')') :
2497+
(' rotate(' + angle + ',' + labelFns.xFn(d) + ',' +
2498+
(labelFns.yFn(d) - d.fontSize / 2) + ')') :
25062499
'');
25072500

25082501
var anchorHeight = getAnchorHeight(

src/plots/polar/polar.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,6 @@ proto.updateRadialAxis = function(fullLayout, polarLayout) {
404404

405405
var vals = Axes.calcTicks(ax);
406406
var valsClipped = Axes.clipEnds(ax, vals);
407-
var labelFns = Axes.makeLabelFns(ax, 0);
408407
var tickSign = Axes.getTickSigns(ax)[2];
409408

410409
Axes.drawTicks(gd, ax, {
@@ -427,9 +426,7 @@ proto.updateRadialAxis = function(fullLayout, polarLayout) {
427426
vals: vals,
428427
layer: layers['radial-axis'],
429428
transFn: transFn,
430-
labelXFn: labelFns.labelXFn,
431-
labelYFn: labelFns.labelYFn,
432-
labelAnchorFn: labelFns.labelAnchorFn
429+
labelFns: Axes.makeLabelFns(ax, 0)
433430
});
434431
}
435432

@@ -556,20 +553,21 @@ proto.updateAngularAxis = function(fullLayout, polarLayout) {
556553

557554
var out = Axes.makeLabelFns(ax, 0);
558555
var labelStandoff = out.labelStandoff;
556+
var labelFns = {};
559557

560-
var labelXFn = function(d) {
558+
labelFns.xFn = function(d) {
561559
var rad = t2g(d);
562560
return Math.cos(rad) * labelStandoff;
563561
};
564562

565-
var labelYFn = function(d) {
563+
labelFns.yFn = function(d) {
566564
var rad = t2g(d);
567565
var ff = Math.sin(rad) > 0 ? 0.2 : 1;
568566
return -Math.sin(rad) * (labelStandoff + d.fontSize * ff) +
569567
Math.abs(Math.cos(rad)) * (d.fontSize * MID_SHIFT);
570568
};
571569

572-
var labelAnchorFn = function(angle, d) {
570+
labelFns.anchorFn = function(d) {
573571
var rad = t2g(d);
574572
var cos = Math.cos(rad);
575573
return Math.abs(cos) < 0.1 ?
@@ -635,9 +633,7 @@ proto.updateAngularAxis = function(fullLayout, polarLayout) {
635633
layer: layers['angular-axis'],
636634
repositionOnUpdate: true,
637635
transFn: transFn,
638-
labelXFn: labelXFn,
639-
labelYFn: labelYFn,
640-
labelAnchorFn: labelAnchorFn
636+
labelFns: labelFns
641637
});
642638
}
643639

src/plots/ternary/ternary.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -459,15 +459,11 @@ proto.drawAx = function(ax) {
459459
crisp: false
460460
});
461461

462-
var labelFns = Axes.makeLabelFns(ax, 0, counterAngle);
463-
464462
Axes.drawLabels(gd, ax, {
465463
vals: vals,
466464
layer: axLayer,
467465
transFn: transFn,
468-
labelXFn: labelFns.labelXFn,
469-
labelYFn: labelFns.labelYFn,
470-
labelAnchorFn: labelFns.labelAnchorFn
466+
labelFns: Axes.makeLabelFns(ax, 0, counterAngle)
471467
});
472468
};
473469

0 commit comments

Comments
 (0)