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

Make object notation usable for polarArea and radar #10088

Merged
merged 6 commits into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/general/data-structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ 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.
When using the pie/doughnut or polarArea 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',
Expand Down
26 changes: 20 additions & 6 deletions src/controllers/controller.polarArea.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import DatasetController from '../core/core.datasetController';
import {toRadians, PI} from '../helpers/index';
import {formatNumber} from '../helpers/helpers.intl';
import {toRadians, PI, formatNumber, resolveObjectKey} from '../helpers/index';

export default class PolarAreaController extends DatasetController {

Expand All @@ -23,6 +22,23 @@ export default class PolarAreaController extends DatasetController {
};
}

parseObjectData(meta, data, start, count) {
const {iScale} = meta;
const {key = 'key'} = this._parsing;
LeeLenaleee marked this conversation as resolved.
Show resolved Hide resolved
const parsed = new Array(count);
let i, ilen, index, item;

for (i = 0, ilen = count; i < ilen; ++i) {
index = i + start;
item = data[index];
parsed[i] = {
r: iScale.parse(resolveObjectKey(item, key), index)
};
kurkle marked this conversation as resolved.
Show resolved Hide resolved
}

return parsed;
}

update(mode) {
const arcs = this._cachedMeta.data;

Expand Down Expand Up @@ -50,7 +66,6 @@ export default class PolarAreaController extends DatasetController {
updateElements(arcs, start, count, mode) {
const reset = mode === 'reset';
const chart = this.chart;
const dataset = this.getDataset();
const opts = chart.options;
const animationOpts = opts.animation;
const scale = this._cachedMeta.rScale;
Expand All @@ -69,7 +84,7 @@ export default class PolarAreaController extends DatasetController {
const arc = arcs[i];
let startAngle = angle;
let endAngle = angle + this._computeAngle(i, mode, defaultAngle);
let outerRadius = chart.getDataVisibility(i) ? scale.getDistanceFromCenterForValue(dataset.data[i]) : 0;
let outerRadius = chart.getDataVisibility(i) ? scale.getDistanceFromCenterForValue(this.getParsed(i).r) : 0;
angle = endAngle;

if (reset) {
Expand All @@ -96,12 +111,11 @@ export default class PolarAreaController extends DatasetController {
}

countVisibleElements() {
const dataset = this.getDataset();
const meta = this._cachedMeta;
let count = 0;

meta.data.forEach((element, index) => {
if (!isNaN(dataset.data[index]) && this.chart.getDataVisibility(index)) {
if (!isNaN(this.getParsed(index).r) && this.chart.getDataVisibility(index)) {
count++;
}
});
Expand Down
27 changes: 27 additions & 0 deletions test/fixtures/controller.polarArea/parse-object-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"config": {
"type": "polarArea",
"data": {
"datasets": [
{
"data": [{"id": "Sales", "nested": {"value": 10}}, {"id": "Purchases", "nested": {"value": 20}}],
"backgroundColor": ["red", "blue"]
}
]
},
"options": {
"responsive": false,
"plugins": {
"legend": false
},
"parsing": {
"key": "nested.value"
},
"scales": {
"r": {
"display": false
}
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.