Skip to content

Commit

Permalink
Create parsed object with correct keys (#11690)
Browse files Browse the repository at this point in the history
* Create parsed object with correct keys

* Add test
  • Loading branch information
LeeLenaleee committed Apr 29, 2024
1 parent 1777f95 commit 8c004a1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/core/core.datasetController.js
Expand Up @@ -92,15 +92,18 @@ function applyStack(stack, value, dsIndex, options = {}) {
return value;
}

function convertObjectDataToArray(data) {
function convertObjectDataToArray(data, meta) {
const {iScale, vScale} = meta;
const iAxisKey = iScale.axis === 'x' ? 'x' : 'y';
const vAxisKey = vScale.axis === 'x' ? 'x' : 'y';
const keys = Object.keys(data);
const adata = new Array(keys.length);
let i, ilen, key;
for (i = 0, ilen = keys.length; i < ilen; ++i) {
key = keys[i];
adata[i] = {
x: key,
y: data[key]
[iAxisKey]: key,
[vAxisKey]: data[key]
};
}
return adata;
Expand Down Expand Up @@ -362,7 +365,8 @@ export default class DatasetController {
// the internal metadata accordingly.

if (isObject(data)) {
this._data = convertObjectDataToArray(data);
const meta = this._cachedMeta;
this._data = convertObjectDataToArray(data, meta);
} else if (_data !== data) {
if (_data) {
// This case happens when the user replaced the data array instance.
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/controller.bar/data/object-index-axis-y.js
@@ -0,0 +1,21 @@
module.exports = {
config: {
type: 'bar',
data: {
datasets: [{
label: '# of Votes',
data: {a: 1, b: 3, c: 2}
}]
},
options: {
indexAxis: 'y'
}
},
options: {
spriteText: true,
canvas: {
height: 256,
width: 512
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8c004a1

Please sign in to comment.