]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Time: Fix offset with low data counts (#8740)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Sat, 27 Mar 2021 19:55:54 +0000 (21:55 +0200)
committerGitHub <noreply@github.com>
Sat, 27 Mar 2021 19:55:54 +0000 (15:55 -0400)
src/scales/scale.time.js
test/fixtures/scale.time/offset-with-1-tick.js [new file with mode: 0644]
test/fixtures/scale.time/offset-with-1-tick.png [new file with mode: 0644]
test/fixtures/scale.time/offset-with-2-ticks.js [new file with mode: 0644]
test/fixtures/scale.time/offset-with-2-ticks.png [new file with mode: 0644]
test/fixtures/scale.time/offset-with-no-ticks.js
test/fixtures/scale.time/offset-with-no-ticks.png

index 144602ba5ad4b110ea02f31756cd51b4b0378eb1..de758952f39cf0ba9f589b5b8daf6aefe45ba79b 100644 (file)
@@ -291,7 +291,7 @@ export default class TimeScale extends Scale {
     max = isFinite(max) && !isNaN(max) ? max : +adapter.endOf(Date.now(), unit) + 1;
 
     // Make sure that max is strictly higher than min (required by the timeseries lookup table)
-    me.min = Math.min(min, max);
+    me.min = Math.min(min, max - 1);
     me.max = Math.max(min + 1, max);
   }
 
@@ -376,8 +376,9 @@ export default class TimeScale extends Scale {
         end = (last - me.getDecimalForValue(timestamps[timestamps.length - 2])) / 2;
       }
     }
-    start = _limitValue(start, 0, 0.25);
-    end = _limitValue(end, 0, 0.25);
+    const limit = timestamps.length < 3 ? 0.5 : 0.25;
+    start = _limitValue(start, 0, limit);
+    end = _limitValue(end, 0, limit);
 
     me._offsets = {start, end, factor: 1 / (start + 1 + end)};
   }
diff --git a/test/fixtures/scale.time/offset-with-1-tick.js b/test/fixtures/scale.time/offset-with-1-tick.js
new file mode 100644 (file)
index 0000000..7487307
--- /dev/null
@@ -0,0 +1,59 @@
+const data = {
+  datasets: [
+    {
+      label: 6,
+      backgroundColor: 'red',
+      data: [
+        {
+          x: '2021-03-24',
+          y: 464
+        }
+      ]
+    },
+    {
+      label: 1,
+      backgroundColor: 'red',
+      data: [
+        {
+          x: '2021-03-24',
+          y: 464
+        }
+      ]
+    },
+    {
+      label: 17,
+      backgroundColor: 'blue',
+      data: [
+        {
+          x: '2021-03-24',
+          y: 390
+        }
+      ]
+    }
+  ]
+};
+
+module.exports = {
+  description: 'https://github.com/chartjs/Chart.js/issues/8718',
+  config: {
+    type: 'bar',
+    data,
+    options: {
+      scales: {
+        x: {
+          type: 'time',
+          time: {
+            unit: 'day',
+          },
+        },
+        y: {
+          display: false
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {width: 256, height: 128}
+  }
+};
diff --git a/test/fixtures/scale.time/offset-with-1-tick.png b/test/fixtures/scale.time/offset-with-1-tick.png
new file mode 100644 (file)
index 0000000..87870bf
Binary files /dev/null and b/test/fixtures/scale.time/offset-with-1-tick.png differ
diff --git a/test/fixtures/scale.time/offset-with-2-ticks.js b/test/fixtures/scale.time/offset-with-2-ticks.js
new file mode 100644 (file)
index 0000000..f5708d0
--- /dev/null
@@ -0,0 +1,89 @@
+const data = {
+  datasets: [
+    {
+      label: 1,
+      backgroundColor: 'orange',
+      data: [
+        {
+          x: '2021-03-24',
+          y: 464
+        }
+      ]
+    },
+    {
+      label: 2,
+      backgroundColor: 'red',
+      data: [
+        {
+          x: '2021-03-24',
+          y: 464
+        }
+      ]
+    },
+    {
+      label: 3,
+      backgroundColor: 'blue',
+      data: [
+        {
+          x: '2021-03-24',
+          y: 390
+        }
+      ]
+    },
+    {
+      label: 4,
+      backgroundColor: 'purple',
+      data: [
+        {
+          x: '2021-03-25',
+          y: 464
+        }
+      ]
+    },
+    {
+      label: 5,
+      backgroundColor: 'black',
+      data: [
+        {
+          x: '2021-03-25',
+          y: 464
+        }
+      ]
+    },
+    {
+      label: 6,
+      backgroundColor: 'cyan',
+      data: [
+        {
+          x: '2021-03-25',
+          y: 390
+        }
+      ]
+    }
+  ]
+};
+
+module.exports = {
+  description: 'https://github.com/chartjs/Chart.js/issues/8718',
+  config: {
+    type: 'bar',
+    data,
+    options: {
+      scales: {
+        x: {
+          type: 'time',
+          time: {
+            unit: 'day',
+          },
+        },
+        y: {
+          display: false
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {width: 256, height: 128}
+  }
+};
diff --git a/test/fixtures/scale.time/offset-with-2-ticks.png b/test/fixtures/scale.time/offset-with-2-ticks.png
new file mode 100644 (file)
index 0000000..5b02f00
Binary files /dev/null and b/test/fixtures/scale.time/offset-with-2-ticks.png differ
index 010dd519b524e4d434dc5f0903cbda1ef797a4c2..c3b184e3654951dce9fede59bbfb7ae6975c430b 100644 (file)
@@ -63,7 +63,6 @@ module.exports = {
       scales: {
         x: {
           type: 'time',
-          // offset: false,
           time: {
             unit: 'month',
           },
index a43c8c00604e94f71484f31b1ddcbfcbe5c1f138..cfc78366b5e312450cb05862e1f234445af3c00e 100644 (file)
Binary files a/test/fixtures/scale.time/offset-with-no-ticks.png and b/test/fixtures/scale.time/offset-with-no-ticks.png differ