Add parsing support to pie/doughnut charts (#9622)

This commit is contained in:
Evert Timberg
2021-09-05 13:46:26 -04:00
committed by GitHub
parent 0cdadd2560
commit 71fa55acac
4 changed files with 53 additions and 4 deletions

View 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

View 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);
}
}
}

View File

@@ -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'
}
}
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB