Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polar better angular tick label placement #3538

Merged
merged 7 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/components/colorbar/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,6 @@ module.exports = function draw(gd, id) {

var vals = Axes.calcTicks(cbAxisOut);
var transFn = Axes.makeTransFn(cbAxisOut);
var labelFns = Axes.makeLabelFns(cbAxisOut, shift);
var tickSign = Axes.getTickSigns(cbAxisOut)[2];

Axes.drawTicks(gd, cbAxisOut, {
Expand All @@ -448,9 +447,7 @@ module.exports = function draw(gd, id) {
vals: vals,
layer: axisLayer,
transFn: transFn,
labelXFn: labelFns.labelXFn,
labelYFn: labelFns.labelYFn,
labelAnchorFn: labelFns.labelAnchorFn
labelFns: Axes.makeLabelFns(cbAxisOut, shift)
});
},
function() {
Expand Down
111 changes: 48 additions & 63 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1802,14 +1802,11 @@ axes.drawOne = function(gd, ax, opts) {
// TODO: mirror labels, esp for subplots

seq.push(function() {
var labelFns = axes.makeLabelFns(ax, mainLinePosition);
return axes.drawLabels(gd, ax, {
vals: vals,
layer: mainAxLayer,
transFn: transFn,
labelXFn: labelFns.labelXFn,
labelYFn: labelFns.labelYFn,
labelAnchorFn: labelFns.labelAnchorFn,
labelFns: axes.makeLabelFns(ax, mainLinePosition)
});
});

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

return axes.drawLabels(gd, ax, {
vals: getSecondaryLabelVals(ax, vals),
Expand All @@ -1831,9 +1826,7 @@ axes.drawOne = function(gd, ax, opts) {
repositionOnUpdate: true,
secondary: true,
transFn: transFn,
labelXFn: secondaryLabelFns.labelXFn,
labelYFn: secondaryLabelFns.labelYFn,
labelAnchorFn: secondaryLabelFns.labelAnchorFn,
labelFns: axes.makeLabelFns(ax, mainLinePosition + labelLength * sgn)
});
});

Expand Down Expand Up @@ -2178,66 +2171,79 @@ axes.makeTickPath = function(ax, shift, sgn, len) {
* @param {number} shift
* @param {number} angle [in degrees] ...
* @return {object}
* - {fn} labelXFn
* - {fn} labelYFn
* - {fn} labelAnchorFn
* - {number} labelStandoff
* - {number} labelShift
* - {fn} xFn
* - {fn} yFn
* - {fn} anchorFn
* - {fn} heightFn
* - {number} labelStandoff (gap parallel to ticks)
* - {number} labelShift (gap perpendicular to ticks)
*/
axes.makeLabelFns = function(ax, shift, angle) {
var axLetter = ax._id.charAt(0);
var pad = (ax.linewidth || 1) / 2;
var ticksOnOutsideLabels = ax.tickson !== 'boundaries' && ax.ticks === 'outside';

var labelStandoff = ticksOnOutsideLabels ? ax.ticklen : 0;
var labelStandoff = 0;
var labelShift = 0;

if(ticksOnOutsideLabels) {
labelStandoff += ax.ticklen;
}
if(angle && ax.ticks === 'outside') {
var rad = Lib.deg2rad(angle);
labelStandoff = ax.ticklen * Math.cos(rad) + 1;
labelShift = ax.ticklen * Math.sin(rad);
}

if(ax.showticklabels && (ticksOnOutsideLabels || ax.showline)) {
labelStandoff += 0.2 * ax.tickfont.size;
}
labelStandoff += (ax.linewidth || 1) / 2;

// Used in polar angular label x/y functions
// TODO generalize makeLabelFns so that it just work for angular axes
var out = {
labelStandoff: labelStandoff,
labelShift: labelShift
};

var x0, y0, ff, flipIt;

if(axLetter === 'x') {
flipIt = ax.side === 'bottom' ? 1 : -1;
x0 = labelShift * flipIt;
y0 = shift + (labelStandoff + pad) * flipIt;
y0 = shift + labelStandoff * flipIt;
ff = ax.side === 'bottom' ? 1 : -0.2;

out.labelXFn = function(d) { return d.dx + x0; };
out.labelYFn = function(d) { return d.dy + y0 + d.fontSize * ff; };
out.labelAnchorFn = function(a) {
out.xFn = function(d) { return d.dx + x0; };
out.yFn = function(d) { return d.dy + y0 + d.fontSize * ff; };
out.anchorFn = function(d, a) {
if(!isNumeric(a) || a === 0 || a === 180) {
return 'middle';
}
return (a * flipIt < 0) ? 'end' : 'start';
};
out.heightFn = function(d, a, h) {
return (a < -60 || a > 60) ? -0.5 * h :
ax.side === 'top' ? -h :
0;
};
} else if(axLetter === 'y') {
flipIt = ax.side === 'right' ? 1 : -1;
x0 = labelStandoff + pad;
x0 = labelStandoff;
y0 = -labelShift * flipIt;
ff = Math.abs(ax.tickangle) === 90 ? 0.5 : 0;

out.labelXFn = function(d) { return d.dx + shift + (x0 + d.fontSize * ff) * flipIt; };
out.labelYFn = function(d) { return d.dy + y0 + d.fontSize * MID_SHIFT; };
out.labelAnchorFn = function(a) {
out.xFn = function(d) { return d.dx + shift + (x0 + d.fontSize * ff) * flipIt; };
out.yFn = function(d) { return d.dy + y0 + d.fontSize * MID_SHIFT; };
out.anchorFn = function(d, a) {
if(isNumeric(a) && Math.abs(a) === 90) {
return 'middle';
}
return ax.side === 'right' ? 'start' : 'end';
};
out.heightFn = function(d, a, h) {
a *= ax.side === 'left' ? 1 : -1;
return a < -30 ? -h :
a < 30 ? -0.5 * h :
0;
};
}

return out;
Expand Down Expand Up @@ -2412,9 +2418,11 @@ axes.drawZeroLine = function(gd, ax, opts) {
* - {boolean} repositionOnUpdate (set to true to reposition update selection)
* - {boolean} secondary
* - {fn} transFn
* - {fn} labelXFn
* - {fn} labelYFn
* - {fn} labelAnchorFn
* - {object} labelFns
* + {fn} xFn
* + {fn} yFn
* + {fn} anchorFn
* + {fn} heightFn
*/
axes.drawLabels = function(gd, ax, opts) {
opts = opts || {};
Expand All @@ -2423,9 +2431,7 @@ axes.drawLabels = function(gd, ax, opts) {
var axLetter = axId.charAt(0);
var cls = opts.cls || axId + 'tick';
var vals = opts.vals;
var labelXFn = opts.labelXFn;
var labelYFn = opts.labelYFn;
var labelAnchorFn = opts.labelAnchorFn;
var labelFns = opts.labelFns;
var tickAngle = opts.secondary ? 0 : ax.tickangle;
var lastAngle = (ax._tickAngles || {})[cls];

Expand All @@ -2445,7 +2451,7 @@ axes.drawLabels = function(gd, ax, opts) {
var newPromise = gd._promises.length;

thisLabel
.call(svgTextUtils.positionText, labelXFn(d), labelYFn(d))
.call(svgTextUtils.positionText, labelFns.xFn(d), labelFns.yFn(d))
.call(Drawing.font, d.font, d.fontSize, d.fontColor)
.text(d.text)
.call(svgTextUtils.convertToTspans, gd);
Expand All @@ -2469,47 +2475,26 @@ axes.drawLabels = function(gd, ax, opts) {
if(opts.repositionOnUpdate) {
tickLabels.each(function(d) {
d3.select(this).select('text')
.call(svgTextUtils.positionText, labelXFn(d), labelYFn(d));
.call(svgTextUtils.positionText, labelFns.xFn(d), labelFns.yFn(d));
});
}

// How much to shift a multi-line label to center it vertically.
function getAnchorHeight(lineCount, lineHeight, angle) {
var h = (lineCount - 1) * lineHeight;
if(axLetter === 'x') {
if(angle < -60 || 60 < angle) {
return -0.5 * h;
} else if(ax.side === 'top') {
return -h;
}
} else {
angle *= ax.side === 'left' ? 1 : -1;
if(angle < -30) {
return -h;
} else if(angle < 30) {
return -0.5 * h;
}
}
return 0;
}

function positionLabels(s, angle) {
s.each(function(d) {
var thisLabel = d3.select(this);
var mathjaxGroup = thisLabel.select('.text-math-group');
var anchor = labelAnchorFn(angle, d);
var anchor = labelFns.anchorFn(d, angle);

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

var anchorHeight = getAnchorHeight(
svgTextUtils.lineCount(thisLabel),
LINE_SPACING * d.fontSize,
isNumeric(angle) ? +angle : 0
);
// how much to shift a multi-line label to center it vertically.
var nLines = svgTextUtils.lineCount(thisLabel);
var lineHeight = LINE_SPACING * d.fontSize;
var anchorHeight = labelFns.heightFn(d, isNumeric(angle) ? +angle : 0, (nLines - 1) * lineHeight);

if(anchorHeight) {
transform += ' translate(0, ' + anchorHeight + ')';
Expand Down
62 changes: 19 additions & 43 deletions src/plots/polar/polar.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ proto.updateRadialAxis = function(fullLayout, polarLayout) {

var vals = Axes.calcTicks(ax);
var valsClipped = Axes.clipEnds(ax, vals);
var labelFns = Axes.makeLabelFns(ax, 0);
var tickSign = Axes.getTickSigns(ax)[2];

Axes.drawTicks(gd, ax, {
Expand All @@ -427,9 +426,7 @@ proto.updateRadialAxis = function(fullLayout, polarLayout) {
vals: vals,
layer: layers['radial-axis'],
transFn: transFn,
labelXFn: labelFns.labelXFn,
labelYFn: labelFns.labelYFn,
labelAnchorFn: labelFns.labelAnchorFn
labelFns: Axes.makeLabelFns(ax, 0)
});
}

Expand Down Expand Up @@ -556,36 +553,31 @@ proto.updateAngularAxis = function(fullLayout, polarLayout) {

var out = Axes.makeLabelFns(ax, 0);
var labelStandoff = out.labelStandoff;
var labelShift = out.labelShift;
var offset4fontsize = (angularLayout.ticks !== 'outside' ? 0.7 : 0.5);
var pad = (ax.linewidth || 1) / 2;
var labelFns = {};

var labelXFn = function(d) {
labelFns.xFn = function(d) {
var rad = t2g(d);

var offset4tx = signSin(rad) === 0 ?
0 :
Math.cos(rad) * (labelStandoff + pad + offset4fontsize * d.fontSize);
var offset4tick = signCos(rad) * (d.dx + labelStandoff + pad);

return offset4tx + offset4tick;
return Math.cos(rad) * labelStandoff;
};

var labelYFn = function(d) {
labelFns.yFn = function(d) {
var rad = t2g(d);
var ff = Math.sin(rad) > 0 ? 0.2 : 1;
return -Math.sin(rad) * (labelStandoff + d.fontSize * ff) +
Math.abs(Math.cos(rad)) * (d.fontSize * MID_SHIFT);
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved
};

var offset4tx = d.dy + d.fontSize * MID_SHIFT - labelShift;
var offset4tick = -Math.sin(rad) * (labelStandoff + pad + offset4fontsize * d.fontSize);

return offset4tx + offset4tick;
labelFns.anchorFn = function(d) {
var rad = t2g(d);
var cos = Math.cos(rad);
return Math.abs(cos) < 0.1 ?
'middle' :
(cos > 0 ? 'start' : 'end');
};

// TODO maybe switch angle, d ordering ??
var labelAnchorFn = function(angle, d) {
labelFns.heightFn = function(d, a, h) {
var rad = t2g(d);
return signSin(rad) === 0 ?
(signCos(rad) > 0 ? 'start' : 'end') :
'middle';
return -0.5 * (1 + Math.sin(rad)) * h;
};

var newTickLayout = strTickLayout(angularLayout);
Expand Down Expand Up @@ -623,6 +615,7 @@ proto.updateAngularAxis = function(fullLayout, polarLayout) {

if(ax.visible) {
var tickSign = ax.ticks === 'inside' ? -1 : 1;
var pad = (ax.linewidth || 1) / 2;

Axes.drawTicks(gd, ax, {
vals: vals,
Expand All @@ -645,9 +638,7 @@ proto.updateAngularAxis = function(fullLayout, polarLayout) {
layer: layers['angular-axis'],
repositionOnUpdate: true,
transFn: transFn,
labelXFn: labelXFn,
labelYFn: labelYFn,
labelAnchorFn: labelAnchorFn
labelFns: labelFns
});
}

Expand Down Expand Up @@ -1413,18 +1404,3 @@ function strTranslate(x, y) {
function strRotate(angle) {
return 'rotate(' + angle + ')';
}

// because Math.sign(Math.cos(Math.PI / 2)) === 1
// oh javascript ;)
function sign(v) {
return Math.abs(v) < 1e-10 ? 0 :
v > 0 ? 1 : -1;
}

function signCos(v) {
return sign(Math.cos(v));
}

function signSin(v) {
return sign(Math.sin(v));
}
6 changes: 1 addition & 5 deletions src/plots/ternary/ternary.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,11 @@ proto.drawAx = function(ax) {
crisp: false
});

var labelFns = Axes.makeLabelFns(ax, 0, counterAngle);

Axes.drawLabels(gd, ax, {
vals: vals,
layer: axLayer,
transFn: transFn,
labelXFn: labelFns.labelXFn,
labelYFn: labelFns.labelYFn,
labelAnchorFn: labelFns.labelAnchorFn
labelFns: Axes.makeLabelFns(ax, 0, counterAngle)
});
};

Expand Down
Binary file modified test/image/baselines/glpolar_scatter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/glpolar_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/glpolar_subplots.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/layout_metatext.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_bar-overlay.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_bar-stacked.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_bar-width-base-offset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_blank.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_categories.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_dates.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_direction.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_fills.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_funky-bars.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_hole.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_polygon-bars.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_polygon-grids.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_r0dr-theta0dtheta.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_radial-range.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_scatter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_sector.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_subplots.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_template.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_ticks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_transforms.png
Binary file modified test/image/baselines/polar_wind-rose.png
Loading