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

Add tickson: 'boundaries' for category cartesian axes #3275

Merged
merged 10 commits into from
Nov 29, 2018
38 changes: 26 additions & 12 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,20 @@ function formatCategory(ax, out) {
var tt = ax._categories[Math.round(out.x)];
if(tt === undefined) tt = '';
out.text = String(tt);

// Setup ticks and grid lines boundaries
// at 1/2 a 'category' to the left/bottom
if(ax.tickson === 'boundaries') {
var inbounds = function(v) {
var p = ax.l2p(v);
return p >= 0 && p <= ax._length ? v : null;
};

out.xbnd = [
inbounds(out.x - 0.5),
inbounds(out.x + ax.dtick - 0.5)
];
}
}

function formatLinear(ax, out, hover, extraPrecision, hideexp) {
Expand Down Expand Up @@ -1621,18 +1635,18 @@ axes.drawOne = function(gd, ax, opts) {
var tickVals;
var gridVals;

if(ax.tickson === 'boundaries') {
// draw ticks and grid lines 1/2 a 'category' to the left,
// add one item at axis tail
var valsBoundaries = vals.map(function(d) {
var d2 = Lib.extendFlat({}, d);
d2.x -= 0.5;
return d2;
});
// not used for labels; no need to worry about the other tickTextObj keys
var d2 = Lib.extendFlat({}, vals[vals.length - 1]);
d2.x += 0.5;
valsBoundaries.push(d2);
if(ax.tickson === 'boundaries' && vals.length) {
// valsBoundaries is not used for labels;
// no need to worry about the other tickTextObj keys
var valsBoundaries = [];
var _push = function(d, bndIndex) {
var xb = d.xbnd[bndIndex];
if(xb !== null) {
valsBoundaries.push(Lib.extendFlat({}, d, {x: xb}));
}
};
for(i = 0; i < vals.length; i++) _push(vals[i], 0);
_push(vals[i - 1], 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice solution, keeping xbnd with the rest of the tick calculations.


valsClipped = axes.clipEnds(ax, valsBoundaries);
tickVals = ax.ticks === 'inside' ? valsClipped : valsBoundaries;
Expand Down
2 changes: 1 addition & 1 deletion test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3156,7 +3156,7 @@ describe('Test axes', function() {
})
.then(function() {
_assert('outside on boundaries', {
ticks: [-8.869, 230.369, 469.619, 708.869],
ticks: [230.369, 469.619],
gridLines: [230.369, 469.619],
tickLabels: [106.421875, 345.671875, 585.25]
});
Expand Down