]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
feat: remove default axis override when custom id is given (#10643)
authorDan Onoshko <danon0404@gmail.com>
Fri, 2 Sep 2022 04:52:48 +0000 (08:52 +0400)
committerGitHub <noreply@github.com>
Fri, 2 Sep 2022 04:52:48 +0000 (07:52 +0300)
* feat: remove default axis override when custom id is given

* docs: add info into migration guide

* test: fix tests for the feat

* docs: add info into migration guide

* test: fix tests for the feat

* feat: review fixes

* feat: review fixes

docs/migration/v4-migration.md
src/core/core.config.js
test/fixtures/controller.bar/stacking/issue-9105.js
test/fixtures/core.scale/ticks/rotated-long.js
test/fixtures/core.scale/ticks/rotated-multi-line.js
test/fixtures/mixed/bar+line.js
test/specs/core.controller.tests.js
test/specs/core.scale.tests.js
types/index.d.ts

index 566f2e080105dd1adffe7a92f951fc81f3804f15..c2700528bdb2f6f46b6df4310ee960052c82775e 100644 (file)
@@ -7,6 +7,7 @@ Chart.js 4.0 introduces a number of breaking changes. We tried keeping the amoun
 ### Charts
 
 * Charts don't override the default tooltip callbacks, so all chart types have the same-looking tooltips.
+* Default scale override has been removed if the configured scale starts with `x`/`y`. Defining `xAxes` in your config will now create a second scale instead of overriding the default `x` axis.
 
 ### Options
 
index 02826667849e1c7d738ed2d9307adbfc9eb2b97b..630c21e89af4c7160022b291e0f490a230b4060c 100644 (file)
@@ -32,17 +32,25 @@ function axisFromPosition(position) {
 }
 
 export function determineAxis(id, scaleOptions) {
-  if (id === 'x' || id === 'y') {
+  if (id === 'x' || id === 'y' || id === 'r') {
     return id;
   }
-  return scaleOptions.axis || axisFromPosition(scaleOptions.position) || id.charAt(0).toLowerCase();
+
+  id = scaleOptions.axis
+    || axisFromPosition(scaleOptions.position)
+    || id.length > 1 && determineAxis(id[0].toLowerCase(), scaleOptions);
+
+  if (id) {
+    return id;
+  }
+
+  throw new Error(`Cannot determine type of '${name}' axis. Please provide 'axis' or 'position' option.`);
 }
 
 function mergeScaleConfig(config, options) {
   const chartDefaults = overrides[config.type] || {scales: {}};
   const configScales = options.scales || {};
   const chartIndexAxis = getIndexAxis(config.type, options);
-  const firstIDs = Object.create(null);
   const scales = Object.create(null);
 
   // First figure out first scale id's per axis.
@@ -57,7 +65,6 @@ function mergeScaleConfig(config, options) {
     const axis = determineAxis(id, scaleConf);
     const defaultId = getDefaultScaleIDFromAxis(axis, chartIndexAxis);
     const defaultScaleOptions = chartDefaults.scales || {};
-    firstIDs[axis] = firstIDs[axis] || id;
     scales[id] = mergeIf(Object.create(null), [{axis}, scaleConf, defaultScaleOptions[axis], defaultScaleOptions[defaultId]]);
   });
 
@@ -69,7 +76,7 @@ function mergeScaleConfig(config, options) {
     const defaultScaleOptions = datasetDefaults.scales || {};
     Object.keys(defaultScaleOptions).forEach(defaultID => {
       const axis = getAxisFromDefaultScaleID(defaultID, indexAxis);
-      const id = dataset[axis + 'AxisID'] || firstIDs[axis] || axis;
+      const id = dataset[axis + 'AxisID'] || axis;
       scales[id] = scales[id] || Object.create(null);
       mergeIf(scales[id], [{axis}, configScales[id], defaultScaleOptions[defaultID]]);
     });
index 200ce4d30a81807e631d4fe42502e5de0dd2da64..992a245ac19b793952fc1d730a05e4a80a862f18 100644 (file)
@@ -10,31 +10,31 @@ module.exports = {
           label: 'Dataset 1',
           data: [12, 19, 3, 5, 2, 3],
           stack: '0',
-          yAxisID: 'y-axis-1'
+          yAxisID: 'y'
         },
         {
           backgroundColor: 'rgba(54,162,235,0.8)',
           label: 'Dataset 2',
           data: [13, 19, 3, 5, 8, 3],
           stack: '0',
-          yAxisID: 'y-axis-1'
+          yAxisID: 'y'
         },
         {
           backgroundColor: 'rgba(75,192,192,0.8)',
           label: 'Dataset 3',
           data: [13, 19, 3, 5, 8, 3],
           stack: '0',
-          yAxisID: 'y-axis-1'
+          yAxisID: 'y'
         }
       ]
     },
     options: {
       plugins: false,
       scales: {
-        xaxis: {
+        x: {
           display: false,
         },
-        'y-axis-1': {
+        y: {
           display: false
         }
       }
index 3009d4cda590e99408105b5c59f7183eb8742ca9..b8bed15a45ce3bcf9a8f1a0156aef245232da17f 100644 (file)
@@ -17,11 +17,11 @@ module.exports = {
         title: false
       },
       scales: {
-        bottom: {
+        x: {
           type: 'category',
           position: 'bottom'
         },
-        top: {
+        x2: {
           type: 'category',
           position: 'top'
         }
index 271c0b312bf10de6d46495d6aa1c59b757c76fef..82fec478a631d84349dbad55bc8349814754ea43 100644 (file)
@@ -17,11 +17,11 @@ module.exports = {
         title: false
       },
       scales: {
-        bottom: {
+        x: {
           type: 'category',
           position: 'bottom'
         },
-        top: {
+        x2: {
           type: 'category',
           position: 'top'
         }
index f6bbfe8cb3e2e474a7599136e51299a634b0ea62..ca6e81548da8ca7946e59791b834281a2c4f4675 100644 (file)
@@ -19,10 +19,10 @@ module.exports = {
     options: {
       indexAxis: 'y',
       scales: {
-        horz: {
+        x: {
           position: 'top'
         },
-        vert: {
+        y: {
           axis: 'y',
           labels: ['a', 'b', 'c', 'd']
         }
index e30da0fe90dab1f0e11bfe78cb7fa45dfeb9bd24..a75267b8d747be03898ba07e907aeb597a489b2b 100644 (file)
@@ -443,6 +443,7 @@ describe('Chart', function() {
         options: {
           scales: {
             foo: {
+              axis: 'x',
               type: 'logarithmic',
               _jasmineCheckC: 'c2',
               _jasmineCheckD: 'd2'
index 04ce097edb9b330b4fda184df8494b914de14b04..e5e94926d563ac2f869f5e9801bdcbda215449fa 100644 (file)
@@ -571,4 +571,102 @@ describe('Core.scale', function() {
       expect(chart.scales.y.max).toEqual(10);
     });
   });
+
+  describe('overrides', () => {
+    it('should create new scale', () => {
+      const chart = window.acquireChart({
+        type: 'scatter',
+        data: {
+          datasets: [{
+            data: [{x: 100, y: 100}, {x: -100, y: -100}]
+          }, {
+            data: [{x: 10, y: 10}, {x: -10, y: -10}]
+          }]
+        },
+        options: {
+          scales: {
+            x2: {
+              type: 'linear',
+              min: -20,
+              max: 20
+            }
+          }
+        }
+      });
+
+      expect(Object.keys(chart.scales).sort()).toEqual(['x', 'x2', 'y']);
+    });
+
+    it('should create new scale with custom name', () => {
+      const chart = window.acquireChart({
+        type: 'scatter',
+        data: {
+          datasets: [{
+            data: [{x: 100, y: 100}, {x: -100, y: -100}]
+          }, {
+            data: [{x: 10, y: 10}, {x: -10, y: -10}]
+          }]
+        },
+        options: {
+          scales: {
+            scaleX: {
+              axis: 'x',
+              type: 'linear',
+              min: -20,
+              max: 20
+            }
+          }
+        }
+      });
+
+      expect(Object.keys(chart.scales).sort()).toEqual(['scaleX', 'x', 'y']);
+    });
+
+    it('should throw error on scale with custom name without axis type', () => {
+      expect(() => window.acquireChart({
+        type: 'scatter',
+        data: {
+          datasets: [{
+            data: [{x: 100, y: 100}, {x: -100, y: -100}]
+          }, {
+            data: [{x: 10, y: 10}, {x: -10, y: -10}]
+          }]
+        },
+        options: {
+          scales: {
+            scaleX: {
+              type: 'linear',
+              min: -20,
+              max: 20
+            }
+          }
+        }
+      })).toThrow();
+    });
+
+    it('should read options first to determine axis', () => {
+      const chart = window.acquireChart({
+        type: 'scatter',
+        data: {
+          datasets: [{
+            data: [{x: 100, y: 100}, {x: -100, y: -100}]
+          }, {
+            data: [{x: 10, y: 10}, {x: -10, y: -10}]
+          }]
+        },
+        options: {
+          scales: {
+            xavier: {
+              axis: 'y',
+              type: 'linear',
+              min: -20,
+              max: 20
+            }
+          }
+        }
+      });
+
+      expect(chart.scales.xavier.axis).toBe('y');
+    });
+  });
 });
index 06803e8b1ed4e26a0ea950825dadad71cb9085a1..a992e62f1332183614acb9248ae524a324b930ff 100644 (file)
@@ -3039,9 +3039,9 @@ export interface CartesianScaleOptions extends CoreScaleOptions {
   stackWeight?: number;
 
   /**
-   *   Which type of axis this is. Possible values are: 'x', 'y'. If not set, this is inferred from the first character of the ID which should be 'x' or 'y'.
+   *   Which type of axis this is. Possible values are: 'x', 'y', 'r'. If not set, this is inferred from the first character of the ID which should be 'x', 'y' or 'r'.
    */
-  axis: 'x' | 'y';
+  axis: 'x' | 'y' | 'r';
 
   /**
    * User defined minimum value for the scale, overrides minimum value from data.