]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Allow registering scale with single parameter (#7157)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Fri, 28 Feb 2020 00:38:10 +0000 (16:38 -0800)
committerGitHub <noreply@github.com>
Fri, 28 Feb 2020 00:38:10 +0000 (19:38 -0500)
Allow registering scale with single parameter

12 files changed:
docs/developers/axes.md
docs/getting-started/v3-migration.md
src/core/core.scaleService.js
src/index.js
src/scales/index.js
src/scales/scale.category.js
src/scales/scale.linear.js
src/scales/scale.logarithmic.js
src/scales/scale.radialLinear.js
src/scales/scale.time.js
test/specs/core.scale.tests.js
test/specs/core.scaleService.tests.js

index 3c22c36ac8d2ef6d00174cab17bd2896a521ec21..2e4aef9b4c3e2f664e8c8e274bfe816b7c290986 100644 (file)
@@ -6,6 +6,8 @@ Axes in Chart.js can be individually extended. Axes should always derive from `C
 let MyScale = Chart.Scale.extend({
     /* extensions ... */
 });
+MyScale.id = 'myScale';
+MyScale.defaults = defaultConfigObject;
 
 // MyScale is now derived from Chart.Scale
 ```
@@ -13,7 +15,7 @@ let MyScale = Chart.Scale.extend({
 Once you have created your scale class, you need to register it with the global chart object so that it can be used. A default config for the scale may be provided when registering the constructor. The first parameter to the register function is a string key that is used later to identify which scale type to use for a chart.
 
 ```javascript
-Chart.scaleService.registerScaleType('myScale', MyScale, defaultConfigObject);
+Chart.scaleService.registerScale(MyScale);
 ```
 
 To use the new scale, simply pass in the string key to the config when creating a chart.
@@ -25,7 +27,7 @@ var lineChart = new Chart(ctx, {
     options: {
         scales: {
             y: {
-                type: 'myScale' // this is the same key that was passed to the registerScaleType function
+                type: 'myScale' // this is the same id that was set on the scale
             }
         }
     }
index 9f3dc4f40b4e91d81ac62d893e9575a3bbb965ab..982ae7323bff1bb22fd64f1fa410145f03d0f4d4 100644 (file)
@@ -223,6 +223,7 @@ Animation system was completely rewritten in Chart.js v3. Each property can now
 
 * `Scale.getLabelForIndex` was replaced by `scale.getLabelForValue`
 * `Scale.getPixelForValue` now has only one parameter. For the `TimeScale` that parameter must be millis since the epoch
+* `ScaleService.registerScaleType` was renamed to `ScaleService.registerScale` and now takes a scale constructors which is expected to have `id` and `defaults` properties.
 
 ##### Ticks
 
index 6e83448a7b50c6b26a655207b23bf24a0d5a2991..00308dada66b21c672430b73ee9f2ff641015f1b 100644 (file)
@@ -11,9 +11,11 @@ export default {
 
        // Scale config defaults
        defaults: {},
-       registerScaleType(type, scaleConstructor, scaleDefaults) {
-               this.constructors[type] = scaleConstructor;
-               this.defaults[type] = clone(scaleDefaults);
+       registerScale(scaleConstructor) {
+               const me = this;
+               const type = scaleConstructor.id;
+               me.constructors[type] = scaleConstructor;
+               me.defaults[type] = clone(scaleConstructor.defaults);
        },
        getScaleConstructor(type) {
                return Object.prototype.hasOwnProperty.call(this.constructors, type) ? this.constructors[type] : undefined;
index 46b05d3f116b1f846e06f27250d152f775c0887a..74dff96d2ab1dbe2a3874592338021bbcd962180 100644 (file)
@@ -42,11 +42,8 @@ Chart.scaleService = scaleService;
 Chart.Ticks = Ticks;
 
 // Register built-in scales
-import scales from './scales/index';
-Object.keys(scales).forEach((type) => {
-       const scale = scales[type];
-       Chart.scaleService.registerScaleType(type, scale, scale._defaults);
-});
+import * as scales from './scales/index';
+Object.keys(scales).forEach(key => Chart.scaleService.registerScale(scales[key]));
 
 // Load to register built-in adapters (as side effects)
 import './adapters/index';
index 505c5fdf9c48fdf914fa8174f544674f03e6b338..a3f8c34cf58bf75de47b872701f6ca6b178d963c 100644 (file)
@@ -1,13 +1,5 @@
-import category from './scale.category';
-import linear from './scale.linear';
-import logarithmic from './scale.logarithmic';
-import radialLinear from './scale.radialLinear';
-import time from './scale.time';
-
-export default {
-       category,
-       linear,
-       logarithmic,
-       radialLinear,
-       time
-};
+export {default as CategoryScale} from './scale.category';
+export {default as LinearScale} from './scale.linear';
+export {default as LogarithmicScale} from './scale.logarithmic';
+export {default as RadialLinearScale} from './scale.radialLinear';
+export {default as TimeScale} from './scale.time';
index f95ca33e84073456745a839b4724f624341e7f48..9c998b00c2ffadcd0e13399400b04e5c795c38a8 100644 (file)
@@ -5,8 +5,9 @@ const defaultConfig = {
 
 export default class CategoryScale extends Scale {
 
+       static id = 'category';
        // INTERNAL: static default options, registered in src/index.js
-       static _defaults = defaultConfig;
+       static defaults = defaultConfig;
 
        constructor(cfg) {
                super(cfg);
index c84e89df982dfb8782ceb8ca1b9fb413011b4ea0..a2a4adccac9bce65441203a02e7b3dc1a274b686 100644 (file)
@@ -11,8 +11,9 @@ const defaultConfig = {
 
 export default class LinearScale extends LinearScaleBase {
 
+       static id = 'linear';
        // INTERNAL: static default options, registered in src/index.js
-       static _defaults = defaultConfig;
+       static defaults = defaultConfig;
 
        determineDataLimits() {
                const me = this;
index 3bd38192c4669bec73f95ac6668aeb6920bbcd82..2b0d5cba0d9ec0456b8195d369e00e558c35a508 100644 (file)
@@ -59,8 +59,9 @@ const defaultConfig = {
 
 export default class LogarithmicScale extends Scale {
 
+       static id = 'logarithmic';
        // INTERNAL: static default options, registered in src/index.js
-       static _defaults = defaultConfig;
+       static defaults = defaultConfig;
 
        constructor(cfg) {
                super(cfg);
index bbba10087faf1739eff7917053c00fd128d2ef2a..5f94df4b8bd1b547f24a5cd4fc1a64fd055bddd6 100644 (file)
@@ -292,8 +292,9 @@ function numberOrZero(param) {
 
 export default class RadialLinearScale extends LinearScaleBase {
 
+       static id = 'radialLinear';
        // INTERNAL: static default options, registered in src/index.js
-       static _defaults = defaultConfig;
+       static defaults = defaultConfig;
 
        constructor(cfg) {
                super(cfg);
index 42d7a1cb8bde337a744147dfa09eaae49317f3b3..d3eef58b6b5c671cd6e678deaca36edd33e952c9 100644 (file)
@@ -567,8 +567,9 @@ const defaultConfig = {
 
 export default class TimeScale extends Scale {
 
+       static id = 'time';
        // INTERNAL: static default options, registered in src/index.js
-       static _defaults = defaultConfig;
+       static defaults = defaultConfig;
 
        /**
         * @param {object} props
index 0ab05671d252d38f09b4e8cab85aee13174fc51a..7a0da05dd8fb07b5afb1589e15cfcb118a4fb7a6 100644 (file)
@@ -460,7 +460,9 @@ describe('Core.scale', function() {
                                        return ['tick'];
                                }
                        });
-                       Chart.scaleService.registerScaleType('customScale', customScale, {});
+                       customScale.id = 'customScale';
+                       customScale.defaults = {};
+                       Chart.scaleService.registerScale(customScale);
 
                        var chart = window.acquireChart({
                                type: 'line',
index aacac9879a9e1e26738e01fa0f9085a94ef73ed3..ecf5b21a8d69df77fb6491d21ea82d0a005db9c8 100644 (file)
@@ -2,14 +2,15 @@
 describe('Test the scale service', function() {
 
        it('should update scale defaults', function() {
-               var defaults = {
-                       testProp: true
-               };
                var type = 'my_test_type';
                var Constructor = function() {
                        this.initialized = true;
                };
-               Chart.scaleService.registerScaleType(type, Constructor, defaults);
+               Constructor.id = type;
+               Constructor.defaults = {
+                       testProp: true
+               };
+               Chart.scaleService.registerScale(Constructor);
 
                // Should equal defaults but not be an identical object
                expect(Chart.scaleService.getScaleDefaults(type)).toEqual(jasmine.objectContaining({