Skip to content

Commit

Permalink
Add parsing support to pie/doughnut charts (#9622)
Browse files Browse the repository at this point in the history
  • Loading branch information
etimberg committed Sep 5, 2021
1 parent 0cdadd2 commit 71fa55a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
16 changes: 16 additions & 0 deletions docs/general/data-structures.md
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions src/controllers/controller.doughnut.js
@@ -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';

Expand Down Expand Up @@ -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) {

This comment has been minimized.

Copy link
@pfried

pfried May 23, 2022

I was looking at the Dougnut Controller and I cannot make sense of the private _parsing property which seems to never get written, can you explain how this is used?

This comment has been minimized.

Copy link
@kurkle

kurkle May 23, 2022

Member

It is the resolved parsing option for the controller.

this._parsing = this.options.parsing;

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

Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/controller.doughnut/doughnut-parsing.js
@@ -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'
}
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 71fa55a

Please sign in to comment.