mirror of
https://github.com/chartjs/Chart.js.git
synced 2026-03-21 23:56:52 +01:00
Fix determineUnitForFormatting floating point error (#6259)
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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: [
|
||||
|
||||
Reference in New Issue
Block a user