mirror of
https://github.com/chartjs/Chart.js.git
synced 2026-03-12 03:06:54 +01:00
Add parsing support to pie/doughnut charts (#9622)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
21
test/fixtures/controller.doughnut/doughnut-parsing.js
vendored
Normal file
21
test/fixtures/controller.doughnut/doughnut-parsing.js
vendored
Normal 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'
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
BIN
test/fixtures/controller.doughnut/doughnut-parsing.png
vendored
Normal file
BIN
test/fixtures/controller.doughnut/doughnut-parsing.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
Reference in New Issue
Block a user