]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix unit determination when autoSkip is enabled (#6583)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Thu, 24 Oct 2019 23:21:45 +0000 (16:21 -0700)
committerEvert Timberg <evert.timberg+github@gmail.com>
Thu, 24 Oct 2019 23:21:45 +0000 (19:21 -0400)
src/scales/scale.time.js
test/specs/scale.time.tests.js

index 38428c8ffd78ad18eea78e3fd4cf78bdf4955aad..83bf4e496e52729a68ea49973f9cc6f5409ba3ca 100644 (file)
@@ -257,7 +257,7 @@ function determineUnitForAutoTicks(minUnit, min, max, capacity) {
 
        for (i = UNITS.indexOf(minUnit); i < ilen - 1; ++i) {
                interval = INTERVALS[UNITS[i]];
-               factor = interval.steps ? interval.steps / 2 : MAX_INTEGER;
+               factor = interval.steps ? interval.steps : MAX_INTEGER;
 
                if (interval.common && Math.ceil((max - min) / (factor * interval.size)) <= capacity) {
                        return UNITS[i];
@@ -270,12 +270,12 @@ function determineUnitForAutoTicks(minUnit, min, max, capacity) {
 /**
  * Figures out what unit to format a set of ticks with
  */
-function determineUnitForFormatting(scale, ticks, minUnit, min, max) {
+function determineUnitForFormatting(scale, numTicks, minUnit, min, max) {
        var i, unit;
 
        for (i = UNITS.length - 1; i >= UNITS.indexOf(minUnit); i--) {
                unit = UNITS[i];
-               if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= ticks.length - 1) {
+               if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= numTicks - 1) {
                        return unit;
                }
        }
@@ -562,11 +562,12 @@ module.exports = Scale.extend({
                var min = me.min;
                var max = me.max;
                var options = me.options;
+               var tickOpts = options.ticks;
                var timeOpts = options.time;
                var timestamps = me._timestamps;
                var ticks = [];
                var capacity = me.getLabelCapacity(min);
-               var source = options.ticks.source;
+               var source = tickOpts.source;
                var distribution = options.distribution;
                var i, ilen, timestamp;
 
@@ -599,13 +600,17 @@ module.exports = Scale.extend({
                me.max = max;
 
                // PRIVATE
-               me._unit = timeOpts.unit || determineUnitForFormatting(me, ticks, timeOpts.minUnit, me.min, me.max);
-               me._majorUnit = !options.ticks.major.enabled || me._unit === 'year' ? undefined
+               // determineUnitForFormatting relies on the number of ticks so we don't use it when
+               // autoSkip is enabled because we don't yet know what the final number of ticks will be
+               me._unit = timeOpts.unit || (tickOpts.autoSkip
+                       ? determineUnitForAutoTicks(timeOpts.minUnit, me.min, me.max, capacity)
+                       : determineUnitForFormatting(me, ticks.length, timeOpts.minUnit, me.min, me.max));
+               me._majorUnit = !tickOpts.major.enabled || me._unit === 'year' ? undefined
                        : determineMajorUnit(me._unit);
                me._table = buildLookupTable(me._timestamps.data, min, max, distribution);
                me._offsets = computeOffsets(me._table, ticks, min, max, options);
 
-               if (options.ticks.reverse) {
+               if (tickOpts.reverse) {
                        ticks.reverse();
                }
 
index 593a846b0f918a4428cad8cde00328f85546349f..71967491d4c85d6d959612e0dc1c8835c6b9c09a 100755 (executable)
@@ -128,7 +128,7 @@ describe('Time scale tests', function() {
                        var ticks = getTicksLabels(scale);
 
                        // `bounds === 'data'`: first and last ticks removed since outside the data range
-                       expect(ticks).toEqual(['Jan 2', 'Jan 3', 'Jan 4', 'Jan 5', 'Jan 6', 'Jan 7', 'Jan 8', 'Jan 9', 'Jan 10']);
+                       expect(ticks.length).toEqual(217);
                });
 
                it('should accept labels as date objects', function() {
@@ -139,7 +139,7 @@ describe('Time scale tests', function() {
                        var ticks = getTicksLabels(scale);
 
                        // `bounds === 'data'`: first and last ticks removed since outside the data range
-                       expect(ticks).toEqual(['Jan 2', 'Jan 3', 'Jan 4', 'Jan 5', 'Jan 6', 'Jan 7', 'Jan 8', 'Jan 9', 'Jan 10']);
+                       expect(ticks.length).toEqual(217);
                });
 
                it('should accept data as xy points', function() {
@@ -187,7 +187,7 @@ describe('Time scale tests', function() {
                        var ticks = getTicksLabels(xScale);
 
                        // `bounds === 'data'`: first and last ticks removed since outside the data range
-                       expect(ticks).toEqual(['Jan 2', 'Jan 3', 'Jan 4', 'Jan 5', 'Jan 6', 'Jan 7', 'Jan 8', 'Jan 9', 'Jan 10']);
+                       expect(ticks.length).toEqual(217);
                });
 
                it('should accept data as ty points', function() {
@@ -235,7 +235,7 @@ describe('Time scale tests', function() {
                        var ticks = getTicksLabels(tScale);
 
                        // `bounds === 'data'`: first and last ticks removed since outside the data range
-                       expect(ticks).toEqual(['Jan 2', 'Jan 3', 'Jan 4', 'Jan 5', 'Jan 6', 'Jan 7', 'Jan 8', 'Jan 9', 'Jan 10']);
+                       expect(ticks.length).toEqual(217);
                });
        });