Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
start to make object notation usable for polarArea
  • Loading branch information
LeeLenaleee committed Jan 18, 2022
commit 3c0633000b2f5077984033c6650ce07ebdaa7b6e
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
67 changes: 65 additions & 2 deletions src/controllers/controller.polarArea.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import DatasetController from '../core/core.datasetController';
import {toRadians, PI} from '../helpers/index';
import {formatNumber} from '../helpers/helpers.intl';
import {updateStacks} from '../core/core.datasetController';
import {isArray, isObject} from '../helpers/helpers.core';
import {resolveObjectKey} from '../helpers/helpers.core';

export default class PolarAreaController extends DatasetController {

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

parse(start, count) {
const {_cachedMeta: meta, _data: data} = this;
const {iScale, _stacked} = meta;
const iAxis = iScale.axis;

let sorted = start === 0 && count === data.length ? true : meta._sorted;
let prev = start > 0 && meta._parsed[start - 1];
let i, cur, parsed;

if (this._parsing === false) {
meta._parsed = data;
meta._sorted = true;
parsed = data;
} else {
if (isArray(data[start])) {
parsed = this.parseArrayData(meta, data, start, count);
} else if (isObject(data[start])) {
parsed = this.parseObjectData(meta, data, start, count);
} else {
parsed = this.parsePrimitiveData(meta, data, start, count);
}

const isNotInOrderComparedToPrev = () => cur[iAxis] === null || (prev && cur[iAxis] < prev[iAxis]);
for (i = 0; i < count; ++i) {
meta._parsed[i + start] = cur = parsed[i];
if (sorted) {
if (isNotInOrderComparedToPrev()) {
sorted = false;
}
prev = cur;
}
}
meta._sorted = sorted;
}

if (_stacked) {
updateStacks(this, parsed);
}
}

parseObjectData(meta, data, start, count) {
const {iScale} = meta;
const {key = 'key'} = this._parsing;
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)
};
}

return parsed;
}

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

Expand Down Expand Up @@ -59,6 +119,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 +128,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];
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 +160,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)) {
count++;
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/core/core.datasetController.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function getLastIndexInStack(stack, vScale, positive, type) {
return null;
}

function updateStacks(controller, parsed) {
export function updateStacks(controller, parsed) {
const {chart, _cachedMeta: meta} = controller;
const stacks = chart._stacks || (chart._stacks = {}); // map structure is {stackKey: {datasetIndex: value}}
const {iScale, vScale, index: datasetIndex} = meta;
Expand Down Expand Up @@ -475,6 +475,7 @@ export default class DatasetController {
[vAxis]: vScale.parse(data[index], index)
};
}

return parsed;
}

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.