Skip to content

Commit 8ffc514

Browse files
author
Deepak kudi
committed
Fix near-zero linear tick labels
1 parent 8965c3d commit 8ffc514

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

src/plots/cartesian/axes.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ function expandRange(range) {
9090
];
9191
}
9292

93+
function cleanLinearTickValue(ax, x, dtick) {
94+
if(ax.type !== 'linear' || !isNumeric(dtick)) return x;
95+
96+
var tolerance = Math.abs(dtick) * 1e-10;
97+
var r2l = ax.r2l || Number;
98+
var tick0 = r2l(ax.tick0);
99+
100+
if(Math.abs(x - tick0) < tolerance) return tick0;
101+
if(Math.abs(x) < tolerance) return 0;
102+
return x;
103+
}
104+
93105
/*
94106
* find the list of possible axes to reference with an xref or yref attribute
95107
* and coerce it to that list
@@ -1105,6 +1117,8 @@ axes.calcTicks = function calcTicks(ax, opts) {
11051117
}
11061118
}
11071119

1120+
x = cleanLinearTickValue(mockAx, x, dtick);
1121+
11081122
// prevent infinite loops - no more than one tick per pixel,
11091123
// and make sure each value is different from the previous
11101124
if(tickVals.length > maxTicks || x === prevX) break;

test/jasmine/tests/axes_test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3440,6 +3440,21 @@ describe('Test axes', function() {
34403440
});
34413441
}
34423442

3443+
it('snaps ticks close to tick0 before formatting', function() {
3444+
var textOut = mockCalc({
3445+
type: 'linear',
3446+
tickmode: 'linear',
3447+
tickformat: '~r',
3448+
tick0: 0,
3449+
dtick: 0.2,
3450+
range: [-0.6, 0.6]
3451+
});
3452+
3453+
expect(textOut).toEqual([
3454+
'−0.6', '−0.4', '−0.2', '0', '0.2', '0.4', '0.6'
3455+
]);
3456+
});
3457+
34433458
it('reverts to "power" for SI/B exponentformat beyond the prefix range (linear case)', function() {
34443459
var textOut = mockCalc({
34453460
type: 'linear',

0 commit comments

Comments
 (0)