]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Adding threshold option to decimation plugin (#9327)
authorNico-DF <difalco.nicola@gmail.com>
Sun, 4 Jul 2021 17:19:35 +0000 (19:19 +0200)
committerGitHub <noreply@github.com>
Sun, 4 Jul 2021 17:19:35 +0000 (13:19 -0400)
docs/configuration/decimation.md
src/plugins/plugin.decimation.js
test/specs/plugin.decimation.tests.js
types/index.esm.d.ts

index 9f3c286b459e65df328587d63ea0a3d80ff40243..703ea5c4d29b36ee61b634e7e0ec4e555bb15949 100644 (file)
@@ -11,6 +11,7 @@ Namespace: `options.plugins.decimation`, the global options for the plugin are d
 | `enabled` | `boolean` | `false` | Is decimation enabled?
 | `algorithm` | `string` | `'min-max'` | Decimation algorithm to use. See the [more...](#decimation-algorithms)
 | `samples` | `number` | | If the `'lttb'` algorithm is used, this is the number of samples in the output dataset. Defaults to the canvas width to pick 1 sample per pixel.
+| `threshold` | `number` | | If the number of samples in the current axis range is above this value, the decimation will be triggered. Defaults to 4 times the canvas width.<br />The number of point after decimation can be higher than the `threshold` value.
 
 ## Decimation Algorithms
 
index 4f1db3661bd7bbca0482a1ab50820a2b923eedab..701d673c0614e311265479b798e8c46f511173e5 100644 (file)
@@ -234,7 +234,8 @@ export default {
       }
 
       let {start, count} = getStartAndCountOfVisiblePointsSimplified(meta, data);
-      if (count <= 4 * availableWidth) {
+      const threshold = options.threshold || 4 * availableWidth;
+      if (count <= threshold) {
         // No decimation is required until we are above this threshold
         cleanDecimatedDataset(dataset);
         return;
index 11b7d6a4d289d4378a13fe67c9adc17d4fc9dcc0..f9efbc29af70d8f9cacf29b35b9ae9241c15002d 100644 (file)
@@ -15,7 +15,7 @@ describe('Plugin.decimation', function() {
       {x: 8, y: 8},
       {x: 9, y: 9}];
 
-    it('should draw all element if sample is greater than data', function() {
+    it('should draw all element if sample is greater than data based on canvas width', function() {
       var chart = window.acquireChart({
         type: 'line',
         data: {
@@ -54,7 +54,7 @@ describe('Plugin.decimation', function() {
       expect(chart.data.datasets[0].data.length).toBe(10);
     });
 
-    it('should draw the specified number of elements', function() {
+    it('should draw the specified number of elements based on canvas width', function() {
       var chart = window.acquireChart({
         type: 'line',
         data: {
@@ -94,6 +94,45 @@ describe('Plugin.decimation', function() {
       expect(chart.data.datasets[0].data.length).toBe(7);
     });
 
+    it('should draw the specified number of elements based on threshold', function() {
+      var chart = window.acquireChart({
+        type: 'line',
+        data: {
+          datasets: [{
+            data: originalData,
+            label: 'dataset1'
+          }]
+        },
+        options: {
+          parsing: false,
+          scales: {
+            x: {
+              type: 'linear'
+            }
+          },
+          plugins: {
+            decimation: {
+              enabled: true,
+              algorithm: 'lttb',
+              samples: 5,
+              threshold: 7
+            }
+          }
+        }
+      }, {
+        canvas: {
+          height: 100,
+          width: 100
+        },
+        wrapper: {
+          height: 100,
+          width: 100
+        }
+      });
+
+      expect(chart.data.datasets[0].data.length).toBe(5);
+    });
+
     it('should draw all element only in range', function() {
       var chart = window.acquireChart({
         type: 'line',
index e56376fae2242ba0ed24555819e0de71f850b0f8..1598bc8288e2e70b50a240ad799a57a4703bee22 100644 (file)
@@ -1991,6 +1991,7 @@ export const enum DecimationAlgorithm {
 }
 interface BaseDecimationOptions {
   enabled: boolean;
+  threshold?: number;
 }
 
 interface LttbDecimationOptions extends BaseDecimationOptions {