let MyScale = Chart.Scale.extend({
/* extensions ... */
});
+MyScale.id = 'myScale';
+MyScale.defaults = defaultConfigObject;
// MyScale is now derived from Chart.Scale
```
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.
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
}
}
}
* `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
// 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;
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';
-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';
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);
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;
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);
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);
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
return ['tick'];
}
});
- Chart.scaleService.registerScaleType('customScale', customScale, {});
+ customScale.id = 'customScale';
+ customScale.defaults = {};
+ Chart.scaleService.registerScale(customScale);
var chart = window.acquireChart({
type: 'line',
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({