Make object notation usable for polarArea and radar (#10088)

* start to make object notation usable for polarArea
* enable object notation also for radar chart, test default key
This commit is contained in:
Jacco van den Berg 2022-02-12 16:23:31 +01:00 committed by GitHub
parent 4b542682f3
commit ed68557a99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 88 additions and 9 deletions

View File

@ -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, radar 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 ```javascript
type: 'doughnut', type: 'doughnut',
@ -85,6 +85,11 @@ options: {
} }
``` ```
:::warning
When using object notation in a radar chart you still need a labels array with labels for the chart to show correctly.
:::
## Object ## Object
```javascript ```javascript

View File

@ -1,6 +1,5 @@
import DatasetController from '../core/core.datasetController'; import DatasetController from '../core/core.datasetController';
import {toRadians, PI} from '../helpers/index'; import {toRadians, PI, formatNumber, _parseObjectDataRadialScale} from '../helpers/index';
import {formatNumber} from '../helpers/helpers.intl';
export default class PolarAreaController extends DatasetController { export default class PolarAreaController extends DatasetController {
@ -23,6 +22,10 @@ export default class PolarAreaController extends DatasetController {
}; };
} }
parseObjectData(meta, data, start, count) {
return _parseObjectDataRadialScale.bind(this)(meta, data, start, count);
}
update(mode) { update(mode) {
const arcs = this._cachedMeta.data; const arcs = this._cachedMeta.data;
@ -50,7 +53,6 @@ export default class PolarAreaController extends DatasetController {
updateElements(arcs, start, count, mode) { updateElements(arcs, start, count, mode) {
const reset = mode === 'reset'; const reset = mode === 'reset';
const chart = this.chart; const chart = this.chart;
const dataset = this.getDataset();
const opts = chart.options; const opts = chart.options;
const animationOpts = opts.animation; const animationOpts = opts.animation;
const scale = this._cachedMeta.rScale; const scale = this._cachedMeta.rScale;
@ -69,7 +71,7 @@ export default class PolarAreaController extends DatasetController {
const arc = arcs[i]; const arc = arcs[i];
let startAngle = angle; let startAngle = angle;
let endAngle = angle + this._computeAngle(i, mode, defaultAngle); 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; angle = endAngle;
if (reset) { if (reset) {
@ -96,12 +98,11 @@ export default class PolarAreaController extends DatasetController {
} }
countVisibleElements() { countVisibleElements() {
const dataset = this.getDataset();
const meta = this._cachedMeta; const meta = this._cachedMeta;
let count = 0; let count = 0;
meta.data.forEach((element, index) => { 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++; count++;
} }
}); });

View File

@ -1,4 +1,5 @@
import DatasetController from '../core/core.datasetController'; import DatasetController from '../core/core.datasetController';
import {_parseObjectDataRadialScale} from '../helpers/index';
export default class RadarController extends DatasetController { export default class RadarController extends DatasetController {
@ -15,6 +16,10 @@ export default class RadarController extends DatasetController {
}; };
} }
parseObjectData(meta, data, start, count) {
return _parseObjectDataRadialScale.bind(this)(meta, data, start, count);
}
update(mode) { update(mode) {
const meta = this._cachedMeta; const meta = this._cachedMeta;
const line = meta.dataset; const line = meta.dataset;
@ -44,14 +49,13 @@ export default class RadarController extends DatasetController {
} }
updateElements(points, start, count, mode) { updateElements(points, start, count, mode) {
const dataset = this.getDataset();
const scale = this._cachedMeta.rScale; const scale = this._cachedMeta.rScale;
const reset = mode === 'reset'; const reset = mode === 'reset';
for (let i = start; i < start + count; i++) { for (let i = start; i < start + count; i++) {
const point = points[i]; const point = points[i];
const options = this.resolveDataElementOptions(i, point.active ? 'active' : mode); const options = this.resolveDataElementOptions(i, point.active ? 'active' : mode);
const pointPosition = scale.getPointPositionForValue(i, dataset.data[i]); const pointPosition = scale.getPointPositionForValue(i, this.getParsed(i).r);
const x = reset ? scale.xCenter : pointPosition.x; const x = reset ? scale.xCenter : pointPosition.x;
const y = reset ? scale.yCenter : pointPosition.y; const y = reset ? scale.yCenter : pointPosition.y;

View File

@ -351,3 +351,19 @@ function resolveKeysFromAllScopes(scopes) {
} }
return Array.from(set); return Array.from(set);
} }
export function _parseObjectDataRadialScale(meta, data, start, count) {
const {iScale} = meta;
const {key = 'r'} = 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;
}

View File

@ -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
}
}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View File

@ -74,6 +74,32 @@ describe('Chart.controllers.radar', function() {
expect(meta.data[3].draw.calls.count()).toBe(1); expect(meta.data[3].draw.calls.count()).toBe(1);
}); });
it('should draw all elements with object notation and default key', function() {
var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
data: [{r: 10}, {r: 20}, {r: 15}]
}],
labels: ['label1', 'label2', 'label3']
}
});
var meta = chart.getDatasetMeta(0);
spyOn(meta.dataset, 'draw');
spyOn(meta.data[0], 'draw');
spyOn(meta.data[1], 'draw');
spyOn(meta.data[2], 'draw');
chart.update();
expect(meta.dataset.draw.calls.count()).toBe(1);
expect(meta.data[0].draw.calls.count()).toBe(1);
expect(meta.data[1].draw.calls.count()).toBe(1);
expect(meta.data[2].draw.calls.count()).toBe(1);
});
it('should update elements', function() { it('should update elements', function() {
var chart = window.acquireChart({ var chart = window.acquireChart({
type: 'radar', type: 'radar',