Skip to content

Commit ad8609f

Browse files
committed
Fix determineUnitForFormatting floating point error
1 parent 2a96d83 commit ad8609f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/scales/scale.time.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ function determineUnitForFormatting(scale, ticks, minUnit, min, max) {
300300

301301
for (i = ilen - 1; i >= UNITS.indexOf(minUnit); i--) {
302302
unit = UNITS[i];
303-
if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= ticks.length) {
303+
if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= ticks.length - 1.1) { // extra 0.1 for epsilon
304304
return unit;
305305
}
306306
}

test/specs/scale.time.tests.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,46 @@ describe('Time scale tests', function() {
315315
expect(ticks).toEqual(['Jan 1', 'Jan 2', 'Jan 3']);
316316
});
317317

318+
it('should correctly determine the unit', function() {
319+
var date = moment('Jan 01 1990', 'MMM DD YYYY');
320+
var data = [];
321+
for (var i = 0; i < 60; i++) {
322+
data.push({x: date.valueOf(), y: Math.random()});
323+
date = date.clone().add(1, 'month');
324+
}
325+
var mockData = {
326+
datasets: [{
327+
data: data
328+
}]
329+
};
330+
331+
var chart = window.acquireChart({
332+
type: 'line',
333+
data: {
334+
datasets: [{
335+
xAxisID: 'xScale0',
336+
data: data
337+
}],
338+
},
339+
options: {
340+
scales: {
341+
xAxes: [{
342+
id: 'xScale0',
343+
type: 'time',
344+
ticks: {
345+
source: 'data',
346+
autoSkip: true
347+
}
348+
}],
349+
}
350+
}
351+
});
352+
353+
var scale = chart.scales.xScale0;
354+
355+
expect(scale._unit).toEqual('month');
356+
});
357+
318358
it('should build ticks based on the appropriate label capacity', function() {
319359
var mockData = {
320360
labels: [

0 commit comments

Comments
 (0)