Skip to content

Commit

Permalink
Fix determineUnitForFormatting floating point error
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed May 9, 2019
1 parent 2a96d83 commit b41969b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/scales/scale.time.js
Expand Up @@ -300,7 +300,7 @@ function determineUnitForFormatting(scale, ticks, minUnit, min, max) {

for (i = ilen - 1; i >= UNITS.indexOf(minUnit); i--) {
unit = UNITS[i];
if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= ticks.length) {
if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= ticks.length - 1.1) { // extra 0.1 for epsilon
return unit;
}
}
Expand Down
35 changes: 35 additions & 0 deletions test/specs/scale.time.tests.js
Expand Up @@ -315,6 +315,41 @@ describe('Time scale tests', function() {
expect(ticks).toEqual(['Jan 1', 'Jan 2', 'Jan 3']);
});

it('should correctly determine the unit', function() {
var date = moment('Jan 01 1990', 'MMM DD YYYY');
var data = [];
for (var i = 0; i < 60; i++) {
data.push({x: date.valueOf(), y: Math.random()});
date = date.clone().add(1, 'month');
}

var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
xAxisID: 'xScale0',
data: data
}],
},
options: {
scales: {
xAxes: [{
id: 'xScale0',
type: 'time',
ticks: {
source: 'data',
autoSkip: true
}
}],
}
}
});

var scale = chart.scales.xScale0;

expect(scale._unit).toEqual('month');
});

it('should build ticks based on the appropriate label capacity', function() {
var mockData = {
labels: [
Expand Down

0 comments on commit b41969b

Please sign in to comment.