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

Fix determineUnitForFormatting floating point error #6259

Merged
merged 2 commits into from May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/adapters/adapter.moment.js
Expand Up @@ -43,7 +43,7 @@ adapters._date.override(typeof moment === 'function' ? {
},

diff: function(max, min, unit) {
return moment.duration(moment(max).diff(moment(min))).as(unit);
return moment(max).diff(moment(min), unit);
},

startOf: function(time, unit, weekday) {
Expand Down
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) {
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