Skip to content

Commit f606c23

Browse files
benmccannetimberg
authored andcommitted
Fix unit determination when autoSkip is enabled (#6583)
1 parent 6c9f202 commit f606c23

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/scales/scale.time.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ function determineUnitForAutoTicks(minUnit, min, max, capacity) {
257257

258258
for (i = UNITS.indexOf(minUnit); i < ilen - 1; ++i) {
259259
interval = INTERVALS[UNITS[i]];
260-
factor = interval.steps ? interval.steps / 2 : MAX_INTEGER;
260+
factor = interval.steps ? interval.steps : MAX_INTEGER;
261261

262262
if (interval.common && Math.ceil((max - min) / (factor * interval.size)) <= capacity) {
263263
return UNITS[i];
@@ -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

test/specs/scale.time.tests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe('Time scale tests', function() {
128128
var ticks = getTicksLabels(scale);
129129

130130
// `bounds === 'data'`: first and last ticks removed since outside the data range
131-
expect(ticks).toEqual(['Jan 2', 'Jan 3', 'Jan 4', 'Jan 5', 'Jan 6', 'Jan 7', 'Jan 8', 'Jan 9', 'Jan 10']);
131+
expect(ticks.length).toEqual(217);
132132
});
133133

134134
it('should accept labels as date objects', function() {
@@ -139,7 +139,7 @@ describe('Time scale tests', function() {
139139
var ticks = getTicksLabels(scale);
140140

141141
// `bounds === 'data'`: first and last ticks removed since outside the data range
142-
expect(ticks).toEqual(['Jan 2', 'Jan 3', 'Jan 4', 'Jan 5', 'Jan 6', 'Jan 7', 'Jan 8', 'Jan 9', 'Jan 10']);
142+
expect(ticks.length).toEqual(217);
143143
});
144144

145145
it('should accept data as xy points', function() {
@@ -187,7 +187,7 @@ describe('Time scale tests', function() {
187187
var ticks = getTicksLabels(xScale);
188188

189189
// `bounds === 'data'`: first and last ticks removed since outside the data range
190-
expect(ticks).toEqual(['Jan 2', 'Jan 3', 'Jan 4', 'Jan 5', 'Jan 6', 'Jan 7', 'Jan 8', 'Jan 9', 'Jan 10']);
190+
expect(ticks.length).toEqual(217);
191191
});
192192

193193
it('should accept data as ty points', function() {
@@ -235,7 +235,7 @@ describe('Time scale tests', function() {
235235
var ticks = getTicksLabels(tScale);
236236

237237
// `bounds === 'data'`: first and last ticks removed since outside the data range
238-
expect(ticks).toEqual(['Jan 2', 'Jan 3', 'Jan 4', 'Jan 5', 'Jan 6', 'Jan 7', 'Jan 8', 'Jan 9', 'Jan 10']);
238+
expect(ticks.length).toEqual(217);
239239
});
240240
});
241241

0 commit comments

Comments
 (0)