]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Bubble: Properly parse radius for non-object data (#9764)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Fri, 15 Oct 2021 11:57:39 +0000 (14:57 +0300)
committerGitHub <noreply@github.com>
Fri, 15 Oct 2021 11:57:39 +0000 (07:57 -0400)
src/controllers/controller.bubble.js
test/fixtures/controller.bubble/clip.png
test/fixtures/controller.bubble/hover-radius-zero.js [new file with mode: 0644]
test/fixtures/controller.bubble/hover-radius-zero.png [new file with mode: 0644]
test/fixtures/controller.bubble/padding.png
test/fixtures/controller.bubble/point-style.png
test/fixtures/controller.bubble/radius-data.png
test/fixtures/core.scale/border-behind-elements.png

index eb770260c3d974f7182c968118adf17876813ef9..1b0241fcae627710838379cfd14b10f5d0729981 100644 (file)
@@ -1,5 +1,5 @@
 import DatasetController from '../core/core.datasetController';
-import {resolveObjectKey, valueOrDefault} from '../helpers/helpers.core';
+import {valueOrDefault} from '../helpers/helpers.core';
 
 export default class BubbleController extends DatasetController {
   initialize() {
@@ -7,22 +7,40 @@ export default class BubbleController extends DatasetController {
     super.initialize();
   }
 
+  /**
+        * Parse array of primitive values
+        * @protected
+        */
+  parsePrimitiveData(meta, data, start, count) {
+    const parsed = super.parsePrimitiveData(meta, data, start, count);
+    for (let i = 0; i < parsed.length; i++) {
+      parsed[i]._custom = this.resolveDataElementOptions(i + start).radius;
+    }
+    return parsed;
+  }
+
+  /**
+        * Parse array of arrays
+        * @protected
+        */
+  parseArrayData(meta, data, start, count) {
+    const parsed = super.parseArrayData(meta, data, start, count);
+    for (let i = 0; i < parsed.length; i++) {
+      const item = data[start + i];
+      parsed[i]._custom = valueOrDefault(item[2], this.resolveDataElementOptions(i + start).radius);
+    }
+    return parsed;
+  }
+
   /**
         * Parse array of objects
         * @protected
         */
   parseObjectData(meta, data, start, count) {
-    const {xScale, yScale} = meta;
-    const {xAxisKey = 'x', yAxisKey = 'y'} = this._parsing;
-    const parsed = [];
-    let i, ilen, item;
-    for (i = start, ilen = start + count; i < ilen; ++i) {
-      item = data[i];
-      parsed.push({
-        x: xScale.parse(resolveObjectKey(item, xAxisKey), i),
-        y: yScale.parse(resolveObjectKey(item, yAxisKey), i),
-        _custom: item && item.r && +item.r
-      });
+    const parsed = super.parseObjectData(meta, data, start, count);
+    for (let i = 0; i < parsed.length; i++) {
+      const item = data[start + i];
+      parsed[i]._custom = valueOrDefault(item && item.r && +item.r, this.resolveDataElementOptions(i + start).radius);
     }
     return parsed;
   }
@@ -31,11 +49,11 @@ export default class BubbleController extends DatasetController {
         * @protected
         */
   getMaxOverflow() {
-    const {data, _parsed} = this._cachedMeta;
+    const data = this._cachedMeta.data;
 
     let max = 0;
     for (let i = data.length - 1; i >= 0; --i) {
-      max = Math.max(max, data[i].size() / 2, _parsed[i]._custom);
+      max = Math.max(max, data[i].size(this.resolveDataElementOptions(i)) / 2);
     }
     return max > 0 && max;
   }
index 6ce0ce1247cce43725ef9067cd80917ed238c008..7214e4e209c59a7cd4d6dbd08443978394f27965 100644 (file)
Binary files a/test/fixtures/controller.bubble/clip.png and b/test/fixtures/controller.bubble/clip.png differ
diff --git a/test/fixtures/controller.bubble/hover-radius-zero.js b/test/fixtures/controller.bubble/hover-radius-zero.js
new file mode 100644 (file)
index 0000000..3628577
--- /dev/null
@@ -0,0 +1,48 @@
+module.exports = {
+  config: {
+    type: 'bubble',
+    data: {
+      labels: [2, 2, 2, 2],
+      datasets: [{
+        data: [
+          [1, 1],
+          [1, 2],
+          [1, 3, 20],
+          [1, 4, 20]
+        ]
+      }, {
+        data: [1, 2, 3, 4]
+      }, {
+        data: [{x: 3, y: 1}, {x: 3, y: 2}, {x: 3, y: 3, r: 15}, {x: 3, y: 4, r: 15}]
+      }]
+    },
+    options: {
+      events: [],
+      radius: 10,
+      hoverRadius: 0,
+      backgroundColor: 'blue',
+      hoverBackgroundColor: 'red',
+      scales: {
+        x: {display: false, bounds: 'data'},
+        y: {display: false}
+      },
+      layout: {
+        padding: 24
+      }
+    }
+  },
+  options: {
+    canvas: {
+      height: 256,
+      width: 256
+    },
+    run(chart) {
+      chart.setActiveElements([
+        {datasetIndex: 0, index: 1}, {datasetIndex: 0, index: 2},
+        {datasetIndex: 1, index: 1}, {datasetIndex: 1, index: 2},
+        {datasetIndex: 2, index: 1}, {datasetIndex: 2, index: 2},
+      ]);
+      chart.update();
+    }
+  }
+};
diff --git a/test/fixtures/controller.bubble/hover-radius-zero.png b/test/fixtures/controller.bubble/hover-radius-zero.png
new file mode 100644 (file)
index 0000000..d86d7dd
Binary files /dev/null and b/test/fixtures/controller.bubble/hover-radius-zero.png differ
index 6a03d5c08dc1b71ded800d8aa3e1ae211f5be5a4..583120e4819d6f17e34c5381868b8b6b95aa01a3 100644 (file)
Binary files a/test/fixtures/controller.bubble/padding.png and b/test/fixtures/controller.bubble/padding.png differ
index d949141d81d4f46dbb844b9a33e63d0b2f81a3fa..1957aba095699993b19e3e61b619adbfe6113e98 100644 (file)
Binary files a/test/fixtures/controller.bubble/point-style.png and b/test/fixtures/controller.bubble/point-style.png differ
index ac819c21e45631137ef113206d0f283a4a4fdf46..d565dbdcffa9b59748c541ccfd8c7725c43d7faf 100644 (file)
Binary files a/test/fixtures/controller.bubble/radius-data.png and b/test/fixtures/controller.bubble/radius-data.png differ
index d3f37719d7eab5fd3c4a6e3419addb2646385e46..f4a9e019b53c818a69dbb50e916a96a2e02078b5 100644 (file)
Binary files a/test/fixtures/core.scale/border-behind-elements.png and b/test/fixtures/core.scale/border-behind-elements.png differ