Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parsing support to pie/doughnut charts #9622

Merged
merged 1 commit into from Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'
etimberg marked this conversation as resolved.
Show resolved Hide resolved
}
}
```

## 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) {
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.