Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify scale registration #7009

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/getting-started/v3-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,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.registerScales` and now takes a variable number of scale constructors which are expected to have `id` and `defaults` properties.

##### Ticks

Expand Down
11 changes: 8 additions & 3 deletions src/core/core.scaleService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ export default {

// Scale config defaults
defaults: {},
registerScaleType: function(type, scaleConstructor, scaleDefaults) {
this.constructors[type] = scaleConstructor;
this.defaults[type] = clone(scaleDefaults);
registerScales: function(...scaleConstructors) {
const me = this;
for (let i = 0; i < scaleConstructors.length; i++) {
const scaleConstructor = scaleConstructors[i];
const type = scaleConstructor.id;
me.constructors[type] = scaleConstructor;
me.defaults[type] = clone(scaleConstructor.defaults);
}
},
getScaleConstructor: function(type) {
return Object.prototype.hasOwnProperty.call(this.constructors, type) ? this.constructors[type] : undefined;
Expand Down
7 changes: 2 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,14 @@ Chart.Ticks = Ticks;

// Register built-in scales
import scales from './scales';
Object.keys(scales).forEach(function(type) {
const scale = scales[type];
Chart.scaleService.registerScaleType(type, scale, scale._defaults);
});
Chart.scaleService.registerScales(...Object.keys(scales).map(key => scales[key]));

// Load to register built-in adapters (as side effects)
import './adapters';

// Loading built-in plugins
import plugins from './plugins';
for (var k in plugins) {
for (let k in plugins) {
if (Object.prototype.hasOwnProperty.call(plugins, k)) {
Chart.plugins.register(plugins[k]);
}
Expand Down
20 changes: 10 additions & 10 deletions src/scales/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

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';
import CategoryScale from './scale.category';
import LinearScale from './scale.linear';
import LogarithmicScale from './scale.logarithmic';
import RadialLinearScale from './scale.radialLinear';
import TimeScale from './scale.time';

export default {
category: category,
linear: linear,
logarithmic: logarithmic,
radialLinear: radialLinear,
time: time
CategoryScale,
LinearScale,
LogarithmicScale,
RadialLinearScale,
TimeScale
};
4 changes: 2 additions & 2 deletions src/scales/scale.category.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ class CategoryScale extends Scale {
}
}

// INTERNAL: static default options, registered in src/index.js
CategoryScale._defaults = defaultConfig;
CategoryScale.id = 'category';
CategoryScale.defaults = defaultConfig;
export default CategoryScale;
5 changes: 3 additions & 2 deletions src/scales/scale.linear.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class LinearScale extends LinearScaleBase {
}
}

// INTERNAL: static default options, registered in src/index.js
LinearScale._defaults = defaultConfig;
LinearScale.id = 'linear';
LinearScale.defaults = defaultConfig;

export default LinearScale;
4 changes: 2 additions & 2 deletions src/scales/scale.logarithmic.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,6 @@ class LogarithmicScale extends Scale {
}
}

// INTERNAL: static default options, registered in src/index.js
LogarithmicScale._defaults = defaultConfig;
LogarithmicScale.id = 'logarithmic';
LogarithmicScale.defaults = defaultConfig;
export default LogarithmicScale;
4 changes: 2 additions & 2 deletions src/scales/scale.radialLinear.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,6 @@ class RadialLinearScale extends LinearScaleBase {
_drawTitle() {}
}

// INTERNAL: static default options, registered in src/index.js
RadialLinearScale._defaults = defaultConfig;
RadialLinearScale.id = 'radialLinear';
RadialLinearScale.defaults = defaultConfig;
export default RadialLinearScale;
4 changes: 2 additions & 2 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,6 @@ class TimeScale extends Scale {
}
}

// INTERNAL: static default options, registered in src/index.js
TimeScale._defaults = defaultConfig;
TimeScale.id = 'time';
TimeScale.defaults = defaultConfig;
export default TimeScale;
4 changes: 3 additions & 1 deletion test/specs/core.scale.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,9 @@ describe('Core.scale', function() {
return ['tick'];
}
});
Chart.scaleService.registerScaleType('customScale', customScale, {});
customScale.id = 'customScale';
customScale.defaults = {};
Chart.scaleService.registerScales(customScale);

var chart = window.acquireChart({
type: 'line',
Expand Down
9 changes: 5 additions & 4 deletions test/specs/core.scaleService.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.registerScales(Constructor);

// Should equal defaults but not be an identical object
expect(Chart.scaleService.getScaleDefaults(type)).toEqual(jasmine.objectContaining({
Expand Down