Skip to content

Commit 4855033

Browse files
committed
fixing bug issue 3814 - use integers to increment aixs ticks
1 parent 0a07168 commit 4855033

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/plots/cartesian/axes.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,8 @@ axes.calcTicks = function calcTicks(ax) {
581581
var maxTicks = Math.max(1000, ax._length || 0);
582582
for(var x = ax._tmin;
583583
(axrev) ? (x >= endTick) : (x <= endTick);
584-
x = axes.tickIncrement(x, ax.dtick, axrev, ax.calendar)) {
584+
x = axes.tickIncrement(x, ax.dtick, axrev, ax.calendar)
585+
) {
585586
// prevent infinite loops - no more than one tick per pixel,
586587
// and make sure each value is different from the previous
587588
if(vals.length > maxTicks || x === xPrevious) break;
@@ -846,7 +847,27 @@ axes.tickIncrement = function(x, dtick, axrev, calendar) {
846847
var axSign = axrev ? -1 : 1;
847848

848849
// includes linear, all dates smaller than month, and pure 10^n in log
849-
if(isNumeric(dtick)) return x + axSign * dtick;
850+
if(isNumeric(dtick)) {
851+
// Note 1:
852+
// 0.3 != 0.1 + 0.2 but 0.3 == ((10 * 0.1) + (10 * 0.2)) / 10
853+
// Attempt to use integer steps to increment
854+
var magic = 1 / dtick;
855+
var newX = (
856+
magic * x +
857+
magic * axSign * dtick
858+
) / magic;
859+
860+
// Note 2: now we may also consider rounding to cover few more edge cases
861+
var lenDt = ('' + dtick).length;
862+
var lenX0 = ('' + x).length;
863+
var lenX1 = ('' + newX).length;
864+
865+
if(lenX1 > lenX0 + lenDt) { // this is likey a rounding error!
866+
newX = +parseFloat(newX).toPrecision(12);
867+
}
868+
869+
return newX;
870+
}
850871

851872
// everything else is a string, one character plus a number
852873
var tType = dtick.charAt(0);

test/image/baselines/tick_percent.png

-11.1 KB
Loading

0 commit comments

Comments
 (0)