From 3c1e04f928f74356d81c12c2034b2bcfde4472ba Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Thu, 27 Feb 2020 16:38:10 -0800 Subject: [PATCH] Allow registering scale with single parameter (#7157) Allow registering scale with single parameter --- docs/developers/axes.md | 6 ++++-- docs/getting-started/v3-migration.md | 1 + src/core/core.scaleService.js | 8 +++++--- src/index.js | 7 ++----- src/scales/index.js | 18 +++++------------- src/scales/scale.category.js | 3 ++- src/scales/scale.linear.js | 3 ++- src/scales/scale.logarithmic.js | 3 ++- src/scales/scale.radialLinear.js | 3 ++- src/scales/scale.time.js | 3 ++- test/specs/core.scale.tests.js | 4 +++- test/specs/core.scaleService.tests.js | 9 +++++---- 12 files changed, 35 insertions(+), 33 deletions(-) diff --git a/docs/developers/axes.md b/docs/developers/axes.md index 3c22c36ac..2e4aef9b4 100644 --- a/docs/developers/axes.md +++ b/docs/developers/axes.md @@ -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 } } } diff --git a/docs/getting-started/v3-migration.md b/docs/getting-started/v3-migration.md index 9f3dc4f40..982ae7323 100644 --- a/docs/getting-started/v3-migration.md +++ b/docs/getting-started/v3-migration.md @@ -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 diff --git a/src/core/core.scaleService.js b/src/core/core.scaleService.js index 6e83448a7..00308dada 100644 --- a/src/core/core.scaleService.js +++ b/src/core/core.scaleService.js @@ -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; diff --git a/src/index.js b/src/index.js index 46b05d3f1..74dff96d2 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; diff --git a/src/scales/index.js b/src/scales/index.js index 505c5fdf9..a3f8c34cf 100644 --- a/src/scales/index.js +++ b/src/scales/index.js @@ -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'; diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index f95ca33e8..9c998b00c 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -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); diff --git a/src/scales/scale.linear.js b/src/scales/scale.linear.js index c84e89df9..a2a4adcca 100644 --- a/src/scales/scale.linear.js +++ b/src/scales/scale.linear.js @@ -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; diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index 3bd38192c..2b0d5cba0 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -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); diff --git a/src/scales/scale.radialLinear.js b/src/scales/scale.radialLinear.js index bbba10087..5f94df4b8 100644 --- a/src/scales/scale.radialLinear.js +++ b/src/scales/scale.radialLinear.js @@ -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); diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 42d7a1cb8..d3eef58b6 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -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 diff --git a/test/specs/core.scale.tests.js b/test/specs/core.scale.tests.js index 0ab05671d..7a0da05dd 100644 --- a/test/specs/core.scale.tests.js +++ b/test/specs/core.scale.tests.js @@ -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', diff --git a/test/specs/core.scaleService.tests.js b/test/specs/core.scaleService.tests.js index aacac9879..ecf5b21a8 100644 --- a/test/specs/core.scaleService.tests.js +++ b/test/specs/core.scaleService.tests.js @@ -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({ -- 2.47.2