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 4 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
2 changes: 1 addition & 1 deletion docs/general/data-structures.md
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
27 changes: 23 additions & 4 deletions src/controllers/controller.polarArea.js
@@ -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, isObject, 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 @@ -59,6 +75,7 @@ export default class PolarAreaController extends DatasetController {
const datasetStartAngle = scale.getIndexAngle(0) - 0.5 * PI;
let angle = datasetStartAngle;
let i;
const {key = 'key'} = this._parsing;

const defaultAngle = 360 / this.countVisibleElements();

Expand All @@ -67,9 +84,10 @@ export default class PolarAreaController extends DatasetController {
}
for (i = start; i < start + count; i++) {
const arc = arcs[i];
const dataPoint = isObject(dataset.data[i]) ? resolveObjectKey(dataset.data[i], key) : dataset.data[i];
LeeLenaleee marked this conversation as resolved.
Show resolved Hide resolved
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(dataPoint) : 0;
angle = endAngle;

if (reset) {
Expand Down Expand Up @@ -98,10 +116,11 @@ export default class PolarAreaController extends DatasetController {
countVisibleElements() {
const dataset = this.getDataset();
const meta = this._cachedMeta;
const {key = 'key'} = this._parsing;
let count = 0;

meta.data.forEach((element, index) => {
if (!isNaN(dataset.data[index]) && this.chart.getDataVisibility(index)) {
if ((!isNaN(dataset.data[index]) || (!isNaN(resolveObjectKey(dataset.data[index], key)))) && this.chart.getDataVisibility(index)) {
LeeLenaleee marked this conversation as resolved.
Show resolved Hide resolved
count++;
}
});
Expand Down
27 changes: 27 additions & 0 deletions test/fixtures/controller.polarArea/parse-object-data.json
@@ -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
}
}
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.