]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Keep track of parsed array changes when parsing===false (#9525)
authorEvert Timberg <evert.timberg+github@gmail.com>
Sat, 14 Aug 2021 13:07:58 +0000 (09:07 -0400)
committerGitHub <noreply@github.com>
Sat, 14 Aug 2021 13:07:58 +0000 (09:07 -0400)
* Keep track of parsed array changes when parsing===false

* Code review feedback

src/core/core.datasetController.js
test/specs/core.datasetController.tests.js

index f504d3f5cbf0232516bb6a24e8184029e821be66..812ee96bc892755a61297ad7eeb657b288c00be4 100644 (file)
@@ -1003,42 +1003,51 @@ export default class DatasetController {
     meta.data.splice(start, count);
   }
 
+  _sync(args) {
+    if (this._parsing) {
+      this._syncList.push(args);
+    } else {
+      const [method, arg1, arg2] = args;
+      this[method](arg1, arg2);
+    }
+  }
+
 
   /**
         * @private
         */
   _onDataPush() {
     const count = arguments.length;
-    this._syncList.push(['_insertElements', this.getDataset().data.length - count, count]);
+    this._sync(['_insertElements', this.getDataset().data.length - count, count]);
   }
 
   /**
         * @private
         */
   _onDataPop() {
-    this._syncList.push(['_removeElements', this._cachedMeta.data.length - 1, 1]);
+    this._sync(['_removeElements', this._cachedMeta.data.length - 1, 1]);
   }
 
   /**
         * @private
         */
   _onDataShift() {
-    this._syncList.push(['_removeElements', 0, 1]);
+    this._sync(['_removeElements', 0, 1]);
   }
 
   /**
         * @private
         */
   _onDataSplice(start, count) {
-    this._syncList.push(['_removeElements', start, count]);
-    this._syncList.push(['_insertElements', start, arguments.length - 2]);
+    this._sync(['_removeElements', start, count]);
+    this._sync(['_insertElements', start, arguments.length - 2]);
   }
 
   /**
         * @private
         */
   _onDataUnshift() {
-    this._syncList.push(['_insertElements', 0, arguments.length]);
+    this._sync(['_insertElements', 0, arguments.length]);
   }
 }
 
index 5ceeae205cb097256e602d0fd4bbeb286e3039e8..372655c0ac385c5b72192f38d7251ab65b822565 100644 (file)
@@ -376,6 +376,48 @@ describe('Chart.DatasetController', function() {
     expect(controller.getParsed(9)).toBe(last);
   });
 
+  it('should synchronize insert before removal when parsing is off', function() {
+    // https://github.com/chartjs/Chart.js/issues/9511
+    const data = [{x: 0, y: 1}, {x: 2, y: 7}, {x: 3, y: 5}];
+    var chart = acquireChart({
+      type: 'scatter',
+      data: {
+        datasets: [{
+          data: data,
+        }],
+      },
+      options: {
+        parsing: false,
+        scales: {
+          x: {
+            type: 'linear',
+            min: 0,
+            max: 10,
+          },
+          y: {
+            type: 'linear',
+            min: 0,
+            max: 10,
+          },
+        },
+      },
+    });
+
+    var meta = chart.getDatasetMeta(0);
+    var controller = meta.controller;
+
+    data.push({
+      x: 10,
+      y: 6
+    });
+    data.splice(0, 1);
+    chart.update();
+
+    expect(meta.data.length).toBe(3);
+    expect(controller.getParsed(0)).toBe(data[0]);
+    expect(controller.getParsed(2)).toBe(data[2]);
+  });
+
   it('should re-synchronize metadata when the data object reference changes', function() {
     var data0 = [0, 1, 2, 3, 4, 5];
     var data1 = [6, 7, 8];