diff --git a/docs/general/data-structures.md b/docs/general/data-structures.md index 778aa295ab6..1a62fc02724 100644 --- a/docs/general/data-structures.md +++ b/docs/general/data-structures.md @@ -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 diff --git a/src/controllers/controller.doughnut.js b/src/controllers/controller.doughnut.js index 91c4756cf91..0581fdad99e 100644 --- a/src/controllers/controller.doughnut.js +++ b/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'; @@ -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 index 00000000000..f9043b03313 --- /dev/null +++ b/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' + } + } + } +}; diff --git a/test/fixtures/controller.doughnut/doughnut-parsing.png b/test/fixtures/controller.doughnut/doughnut-parsing.png new file mode 100644 index 00000000000..a01d23fd2c4 Binary files /dev/null and b/test/fixtures/controller.doughnut/doughnut-parsing.png differ