]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Use all timestamps for calculating offsets (#7573)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Mon, 6 Jul 2020 13:34:43 +0000 (06:34 -0700)
committerGitHub <noreply@github.com>
Mon, 6 Jul 2020 13:34:43 +0000 (09:34 -0400)
src/scales/scale.timeseries.js
test/specs/scale.time.tests.js

index 053e8d099945cf424ce6be3216fb9630c1cb2a4b..1476040eb9944738fa654db18a0feb38c55c6431 100644 (file)
@@ -46,9 +46,13 @@ class TimeSeriesScale extends TimeScale {
                this._table = [];
        }
 
-       initOffsets(timestamps) {
+       /**
+        * @protected
+        */
+       initOffsets() {
                const me = this;
-               me._table = me.buildLookupTable();
+               const timestamps = me._getTimestampsForTable();
+               me._table = me.buildLookupTable(timestamps);
                super.initOffsets(timestamps);
        }
 
@@ -59,14 +63,13 @@ class TimeSeriesScale extends TimeScale {
         * extremity (left + width or top + height). Note that it would be more optimized to directly
         * store pre-computed pixels, but the scale dimensions are not guaranteed at the time we need
         * to create the lookup table. The table ALWAYS contains at least two items: min and max.
-        *
+        * @param {number[]} timestamps
         * @return {object[]}
         * @protected
         */
-       buildLookupTable() {
+       buildLookupTable(timestamps) {
                const me = this;
                const {min, max} = me;
-               const timestamps = me._getTimestampsForTable();
                if (!timestamps.length) {
                        return [
                                {time: min, pos: 0},
@@ -103,6 +106,7 @@ class TimeSeriesScale extends TimeScale {
 
        /**
         * Returns all timestamps
+        * @return {number[]}
         * @private
         */
        _getTimestampsForTable() {
@@ -136,6 +140,7 @@ class TimeSeriesScale extends TimeScale {
        }
 
        /**
+        * @return {number[]}
         * @protected
         */
        getDataTimestamps() {
@@ -151,6 +156,7 @@ class TimeSeriesScale extends TimeScale {
        }
 
        /**
+        * @return {number[]}
         * @protected
         */
        getLabelTimestamps() {
index 4d15aa38f687e85d3f21ba6731071ce79ba47822..45c92df4eed525e47a0163a06cd73d91594d7039 100644 (file)
@@ -1149,6 +1149,41 @@ describe('Time scale tests', function() {
                });
        });
 
+       it ('should handle offset when there are more data points than ticks', function() {
+               const chart = window.acquireChart({
+                       type: 'bar',
+                       data: {
+                               datasets: [{
+                                       data: [{x: 631180800000, y: '31.84'}, {x: 631267200000, y: '30.89'}, {x: 631353600000, y: '33.00'}, {x: 631440000000, y: '33.52'}, {x: 631526400000, y: '32.24'}, {x: 631785600000, y: '32.74'}, {x: 631872000000, y: '31.45'}, {x: 631958400000, y: '32.60'}, {x: 632044800000, y: '31.77'}, {x: 632131200000, y: '32.45'}, {x: 632390400000, y: '31.13'}, {x: 632476800000, y: '31.82'}, {x: 632563200000, y: '30.81'}, {x: 632649600000, y: '30.07'}, {x: 632736000000, y: '29.31'}, {x: 632995200000, y: '29.82'}, {x: 633081600000, y: '30.20'}, {x: 633168000000, y: '30.78'}, {x: 633254400000, y: '30.72'}, {x: 633340800000, y: '31.62'}, {x: 633600000000, y: '30.64'}, {x: 633686400000, y: '32.36'}, {x: 633772800000, y: '34.66'}, {x: 633859200000, y: '33.96'}, {x: 633945600000, y: '34.20'}, {x: 634204800000, y: '32.20'}, {x: 634291200000, y: '32.44'}, {x: 634377600000, y: '32.72'}, {x: 634464000000, y: '32.95'}, {x: 634550400000, y: '32.95'}, {x: 634809600000, y: '30.88'}, {x: 634896000000, y: '29.44'}, {x: 634982400000, y: '29.36'}, {x: 635068800000, y: '28.84'}, {x: 635155200000, y: '30.85'}, {x: 635414400000, y: '32.00'}, {x: 635500800000, y: '32.74'}, {x: 635587200000, y: '33.16'}, {x: 635673600000, y: '34.73'}, {x: 635760000000, y: '32.89'}, {x: 636019200000, y: '32.41'}, {x: 636105600000, y: '31.15'}, {x: 636192000000, y: '30.63'}, {x: 636278400000, y: '29.60'}, {x: 636364800000, y: '29.31'}, {x: 636624000000, y: '29.83'}, {x: 636710400000, y: '27.97'}, {x: 636796800000, y: '26.18'}, {x: 636883200000, y: '26.06'}, {x: 636969600000, y: '26.34'}, {x: 637228800000, y: '27.75'}, {x: 637315200000, y: '29.05'}, {x: 637401600000, y: '28.82'}, {x: 637488000000, y: '29.43'}, {x: 637574400000, y: '29.53'}, {x: 637833600000, y: '28.50'}, {x: 637920000000, y: '28.87'}, {x: 638006400000, y: '28.11'}, {x: 638092800000, y: '27.79'}, {x: 638179200000, y: '28.18'}, {x: 638438400000, y: '28.27'}, {x: 638524800000, y: '28.29'}, {x: 638611200000, y: '29.63'}, {x: 638697600000, y: '29.13'}, {x: 638784000000, y: '26.57'}, {x: 639039600000, y: '27.19'}, {x: 639126000000, y: '27.48'}, {x: 639212400000, y: '27.79'}, {x: 639298800000, y: '28.48'}, {x: 639385200000, y: '27.88'}, {x: 639644400000, y: '25.63'}, {x: 639730800000, y: '25.02'}, {x: 639817200000, y: '25.26'}, {x: 639903600000, y: '25.00'}, {x: 639990000000, y: '26.23'}, {x: 640249200000, y: '26.22'}, {x: 640335600000, y: '26.36'}, {x: 640422000000, y: '25.45'}, {x: 640508400000, y: '24.62'}, {x: 640594800000, y: '26.65'}, {x: 640854000000, y: '26.28'}, {x: 640940400000, y: '27.25'}, {x: 641026800000, y: '25.93'}],
+                                       backgroundColor: '#ff6666'
+                               }]
+                       },
+                       options: {
+                               scales: {
+                                       x: {
+                                               type: 'timeseries',
+                                               offset: true,
+                                               ticks: {
+                                                       source: 'data',
+                                                       autoSkip: true,
+                                                       maxRotation: 0
+                                               }
+                                       },
+                                       y: {
+                                               type: 'linear',
+                                               gridLines: {
+                                                       drawBorder: false
+                                               }
+                                       }
+                               }
+                       },
+                       legend: false
+               });
+               const scale = chart.scales.x;
+               expect(scale.getPixelForDecimal(0)).toBeCloseToPixel(29);
+               expect(scale.getPixelForDecimal(1.0)).toBeCloseToPixel(494);
+       });
+
        ['data', 'labels'].forEach(function(source) {
                ['timeseries', 'time'].forEach(function(type) {
                        describe('when ticks.source is "' + source + '" and scale type is "' + type + '"', function() {