Skip to content

Commit 2933bcf

Browse files
committed
Fix unit determination when autoSkip is enabled
1 parent feaf418 commit 2933bcf

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/scales/scale.time.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,12 @@ function determineUnitForAutoTicks(minUnit, min, max, capacity) {
270270
/**
271271
* Figures out what unit to format a set of ticks with
272272
*/
273-
function determineUnitForFormatting(scale, ticks, minUnit, min, max) {
273+
function determineUnitForFormatting(scale, numTicks, minUnit, min, max) {
274274
var i, unit;
275275

276276
for (i = UNITS.length - 1; i >= UNITS.indexOf(minUnit); i--) {
277277
unit = UNITS[i];
278-
if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= ticks.length - 1) {
278+
if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= numTicks - 1) {
279279
return unit;
280280
}
281281
}
@@ -562,11 +562,12 @@ module.exports = Scale.extend({
562562
var min = me.min;
563563
var max = me.max;
564564
var options = me.options;
565+
var tickOpts = options.ticks;
565566
var timeOpts = options.time;
566567
var timestamps = me._timestamps;
567568
var ticks = [];
568569
var capacity = me.getLabelCapacity(min);
569-
var source = options.ticks.source;
570+
var source = tickOpts.source;
570571
var distribution = options.distribution;
571572
var i, ilen, timestamp;
572573

@@ -599,13 +600,17 @@ module.exports = Scale.extend({
599600
me.max = max;
600601

601602
// PRIVATE
602-
me._unit = timeOpts.unit || determineUnitForFormatting(me, ticks, timeOpts.minUnit, me.min, me.max);
603-
me._majorUnit = !options.ticks.major.enabled || me._unit === 'year' ? undefined
603+
// determineUnitForFormatting relies on the number of ticks so we don't use it when
604+
// autoSkip is enabled because we don't yet know what the final number of ticks will be
605+
me._unit = timeOpts.unit || (tickOpts.autoSkip
606+
? determineUnitForAutoTicks(timeOpts.minUnit, me.min, me.max, capacity)
607+
: determineUnitForFormatting(me, ticks.length, timeOpts.minUnit, me.min, me.max));
608+
me._majorUnit = !tickOpts.major.enabled || me._unit === 'year' ? undefined
604609
: determineMajorUnit(me._unit);
605610
me._table = buildLookupTable(me._timestamps.data, min, max, distribution);
606611
me._offsets = computeOffsets(me._table, ticks, min, max, options);
607612

608-
if (options.ticks.reverse) {
613+
if (tickOpts.reverse) {
609614
ticks.reverse();
610615
}
611616

0 commit comments

Comments
 (0)