Skip to content

Commit e807484

Browse files
committed
Allow a dataset to specify a different data key
1 parent 969e829 commit e807484

File tree

6 files changed

+25
-10
lines changed

6 files changed

+25
-10
lines changed

docs/docs/general/data-structures.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ In this mode, property name is used for `index` scale and value for `value` scal
6464

6565
| Name | Type | Description
6666
| ---- | ---- | -----------
67+
| `dataKey` | `string` | If specified, tells the chart to look for the dataset data in `dataset[dataset.dataKey]`. If unspecified, this defaults to `'data'`.
6768
| `label` | `string` | The label for the dataset which appears in the legend and tooltips.
6869
| `clip` | `number`\|`object` | How to clip relative to chartArea. Positive value allows overflow, negative value clips that many pixels inside chartArea. 0 = clip at chartArea. Clipping can also be configured per side: clip: {left: 5, top: false, right: -2, bottom: 0}
6970
| `order` | `number` | The drawing order of dataset. Also affects order for stacking, tooltip and legend.

src/controllers/controller.doughnut.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ export default class DoughnutController extends DatasetController {
5656
* Override data parsing, since we are not using scales
5757
*/
5858
parse(start, count) {
59-
const data = this.getDataset().data;
59+
const dataKey = this.getDataKey();
60+
const data = this.getDataset()[dataKey];
6061
const meta = this._cachedMeta;
6162
let i, ilen;
6263
for (i = start, ilen = start + count; i < ilen; ++i) {

src/controllers/controller.polarArea.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export default class PolarAreaController extends DatasetController {
5252
const centerX = scale.xCenter;
5353
const centerY = scale.yCenter;
5454
const datasetStartAngle = getStartAngleRadians(opts.startAngle);
55+
const dataKey = me.getDataKey();
5556
let angle = datasetStartAngle;
5657
let i;
5758

@@ -64,7 +65,7 @@ export default class PolarAreaController extends DatasetController {
6465
const arc = arcs[i];
6566
let startAngle = angle;
6667
let endAngle = angle + me._computeAngle(i, mode);
67-
let outerRadius = this.chart.getDataVisibility(i) ? scale.getDistanceFromCenterForValue(dataset.data[i]) : 0;
68+
let outerRadius = this.chart.getDataVisibility(i) ? scale.getDistanceFromCenterForValue(dataset[dataKey][i]) : 0;
6869
angle = endAngle;
6970

7071
if (reset) {
@@ -94,10 +95,11 @@ export default class PolarAreaController extends DatasetController {
9495
countVisibleElements() {
9596
const dataset = this.getDataset();
9697
const meta = this._cachedMeta;
98+
const dataKey = this.getDataKey();
9799
let count = 0;
98100

99101
meta.data.forEach((element, index) => {
100-
if (!isNaN(dataset.data[index]) && this.chart.getDataVisibility(index)) {
102+
if (!isNaN(dataset[dataKey][index]) && this.chart.getDataVisibility(index)) {
101103
count++;
102104
}
103105
});
@@ -113,8 +115,9 @@ export default class PolarAreaController extends DatasetController {
113115
const meta = me._cachedMeta;
114116
const count = meta.count;
115117
const dataset = me.getDataset();
118+
const dataKey = me.getDataKey();
116119

117-
if (isNaN(dataset.data[index]) || !this.chart.getDataVisibility(index)) {
120+
if (isNaN(dataset[dataKey][index]) || !this.chart.getDataVisibility(index)) {
118121
return 0;
119122
}
120123

src/controllers/controller.radar.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ export default class RadarController extends DatasetController {
4444
updateElements(points, start, count, mode) {
4545
const me = this;
4646
const dataset = me.getDataset();
47+
const dataKey = me.getDataKey();
4748
const scale = me._cachedMeta.rScale;
4849
const reset = mode === 'reset';
4950

5051
for (let i = start; i < start + count; i++) {
5152
const point = points[i];
5253
const options = me.resolveDataElementOptions(i, mode);
53-
const pointPosition = scale.getPointPositionForValue(i, dataset.data[i]);
54+
const pointPosition = scale.getPointPositionForValue(i, dataset[dataKey][i]);
5455

5556
const x = reset ? scale.xCenter : pointPosition.x;
5657
const y = reset ? scale.yCenter : pointPosition.y;

src/core/core.datasetController.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Animations from './core.animations';
22
import defaults from './core.defaults';
3-
import {isObject, merge, _merger, isArray, valueOrDefault, mergeIf, resolveObjectKey, _capitalize} from '../helpers/helpers.core';
3+
import {isObject, merge, _merger, isArray, valueOrDefault, mergeIf, resolveObjectKey, _capitalize, isNullOrUndef} from '../helpers/helpers.core';
44
import {listenArrayEvents, unlistenArrayEvents} from '../helpers/helpers.collection';
55
import {resolve} from '../helpers/helpers.options';
66
import {getHoverColor} from '../helpers/helpers.color';
@@ -275,6 +275,11 @@ export default class DatasetController {
275275
return this.chart.data.datasets[this.index];
276276
}
277277

278+
getDataKey() {
279+
const {dataKey} = this.getDataset();
280+
return isNullOrUndef(dataKey) ? 'data' : dataKey;
281+
}
282+
278283
getMeta() {
279284
return this.chart.getDatasetMeta(this.index);
280285
}
@@ -320,7 +325,8 @@ export default class DatasetController {
320325
_dataCheck() {
321326
const me = this;
322327
const dataset = me.getDataset();
323-
const data = dataset.data || (dataset.data = []);
328+
const dataKey = me.getDataKey();
329+
const data = dataset[dataKey] || (dataset[dataKey] = []);
324330

325331
// In order to correctly handle data addition/deletion animation (an thus simulate
326332
// real-time charts), we need to monitor these data modifications and synchronize
@@ -386,6 +392,7 @@ export default class DatasetController {
386392
*/
387393
configure() {
388394
const me = this;
395+
const dataKey = me.getDataKey();
389396
me._config = merge(Object.create(null), [
390397
defaults.controllers[me._type].datasets,
391398
(me.chart.options.datasets || {})[me._type],
@@ -395,7 +402,7 @@ export default class DatasetController {
395402
// Cloning the data is expensive and unnecessary.
396403
// Additionally, plugins may add dataset level fields that should
397404
// not be cloned. We identify those via an underscore prefix
398-
if (key !== 'data' && key.charAt(0) !== '_') {
405+
if (key !== 'data' && key !== dataKey && key.charAt(0) !== '_') {
399406
_merger(key, target, source);
400407
}
401408
}
@@ -1023,7 +1030,8 @@ export default class DatasetController {
10231030
*/
10241031
_onDataPush() {
10251032
const count = arguments.length;
1026-
this._insertElements(this.getDataset().data.length - count, count);
1033+
const dataKey = this.getDataKey();
1034+
this._insertElements(this.getDataset()[dataKey].length - count, count);
10271035
}
10281036

10291037
/**

types/index.esm.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,8 @@ export class DatasetController<TElement extends Element = Element, TDatasetEleme
527527
protected getMaxOverflow(): boolean | number;
528528
draw(): void;
529529
reset(): void;
530-
getDataset(): ChartDataset;
530+
getDataset(): ChartDataset;
531+
getDataKey(): string;
531532
getMeta(): ChartMeta<TElement, TDatasetElement>;
532533
getScaleForId(scaleID: string): Scale | undefined;
533534
configure(): void;

0 commit comments

Comments
 (0)