]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Create parsed object with correct keys (#11690)
authorJacco van den Berg <jaccoberg2281@gmail.com>
Mon, 29 Apr 2024 00:37:29 +0000 (02:37 +0200)
committerGitHub <noreply@github.com>
Mon, 29 Apr 2024 00:37:29 +0000 (20:37 -0400)
* Create parsed object with correct keys

* Add test

src/core/core.datasetController.js
test/fixtures/controller.bar/data/object-index-axis-y.js [new file with mode: 0644]
test/fixtures/controller.bar/data/object-index-axis-y.png [new file with mode: 0644]

index 4c7a66255ee8c64b686306b60d210208fafe538e..976a3a5f6f4f450b855ea762d8c94e791917191e 100644 (file)
@@ -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;
@@ -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.
diff --git a/test/fixtures/controller.bar/data/object-index-axis-y.js b/test/fixtures/controller.bar/data/object-index-axis-y.js
new file mode 100644 (file)
index 0000000..0023d08
--- /dev/null
@@ -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
+    }
+  }
+};
diff --git a/test/fixtures/controller.bar/data/object-index-axis-y.png b/test/fixtures/controller.bar/data/object-index-axis-y.png
new file mode 100644 (file)
index 0000000..ace6956
Binary files /dev/null and b/test/fixtures/controller.bar/data/object-index-axis-y.png differ