]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add parsing support to pie/doughnut charts (#9622)
authorEvert Timberg <evert.timberg+github@gmail.com>
Sun, 5 Sep 2021 17:46:26 +0000 (13:46 -0400)
committerGitHub <noreply@github.com>
Sun, 5 Sep 2021 17:46:26 +0000 (13:46 -0400)
docs/general/data-structures.md
src/controllers/controller.doughnut.js
test/fixtures/controller.doughnut/doughnut-parsing.js [new file with mode: 0644]
test/fixtures/controller.doughnut/doughnut-parsing.png [new file with mode: 0644]

index 778aa295ab6763eed70359f327b7bd7f8202e43e..1a62fc02724892db3c8e91ef57c9034390678716 100644 (file)
@@ -49,6 +49,22 @@ options: {
 }
 ```
 
+When using the pie/doughnut chart type, the `parsing` object should have a `key` item that points to the value to look at. In this example, the doughnut chart will show two items with values 1500 and 500.
+
+```javascript
+type: 'doughnut',
+data: {
+    datasets: [{
+        data: [{id: 'Sales', nested: {value: 1500}}, {id: 'Purchases', nested: {value: 500}}]
+    }]
+},
+options: {
+    parsing: {
+        key: 'nested.value'
+    }
+}
+```
+
 ## Object
 
 ```javascript
index 91c4756cf91f47891488f6414a7182b79dbd64cc..0581fdad99ed5d95e8aaa0034a54aa8ce0296eee 100644 (file)
@@ -1,5 +1,5 @@
 import DatasetController from '../core/core.datasetController';
-import {isArray, toPercentage, toDimension, valueOrDefault} from '../helpers/helpers.core';
+import {isArray, isObject, resolveObjectKey, toPercentage, toDimension, valueOrDefault} from '../helpers/helpers.core';
 import {formatNumber} from '../helpers/helpers.intl';
 import {toRadians, PI, TAU, HALF_PI, _angleBetween} from '../helpers/helpers.math';
 
@@ -54,9 +54,21 @@ export default class DoughnutController extends DatasetController {
   parse(start, count) {
     const data = this.getDataset().data;
     const meta = this._cachedMeta;
-    let i, ilen;
-    for (i = start, ilen = start + count; i < ilen; ++i) {
-      meta._parsed[i] = +data[i];
+
+    if (this._parsing === false) {
+      meta._parsed = data;
+    } else {
+      let getter = (i) => +data[i];
+
+      if (isObject(data[start])) {
+        const {key = 'value'} = this._parsing;
+        getter = (i) => +resolveObjectKey(data[i], key);
+      }
+
+      let i, ilen;
+      for (i = start, ilen = start + count; i < ilen; ++i) {
+        meta._parsed[i] = getter(i);
+      }
     }
   }
 
diff --git a/test/fixtures/controller.doughnut/doughnut-parsing.js b/test/fixtures/controller.doughnut/doughnut-parsing.js
new file mode 100644 (file)
index 0000000..f9043b0
--- /dev/null
@@ -0,0 +1,21 @@
+module.exports = {
+  config: {
+    type: 'doughnut',
+    data: {
+      labels: ['Red', 'Blue', 'Yellow'],
+      datasets: [{
+        data: [
+          {foo: 12},
+          {foo: 4},
+          {foo: 6},
+        ],
+        backgroundColor: ['red', 'blue', 'yellow']
+      }]
+    },
+    options: {
+      parsing: {
+        key: 'foo'
+      }
+    }
+  }
+};
diff --git a/test/fixtures/controller.doughnut/doughnut-parsing.png b/test/fixtures/controller.doughnut/doughnut-parsing.png
new file mode 100644 (file)
index 0000000..a01d23f
Binary files /dev/null and b/test/fixtures/controller.doughnut/doughnut-parsing.png differ