]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add a scale.init method (#7346)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Tue, 12 May 2020 20:44:40 +0000 (13:44 -0700)
committerGitHub <noreply@github.com>
Tue, 12 May 2020 20:44:40 +0000 (16:44 -0400)
src/core/core.controller.js
src/core/core.scale.js
src/scales/scale.radialLinear.js
src/scales/scale.time.js
test/specs/scale.category.tests.js

index 9e3c5fd7a10db4cafa6a3a42992a5ddfd41d8904..bc60d506fc6b5760b12fe1d4d9bc0f78f218541c 100644 (file)
@@ -403,9 +403,6 @@ export default class Chart {
                        let scale = null;
                        if (id in scales && scales[id].type === scaleType) {
                                scale = scales[id];
-                               scale.options = scaleOptions;
-                               scale.ctx = me.ctx;
-                               scale.chart = me;
                        } else {
                                const scaleClass = scaleService.getScaleConstructor(scaleType);
                                if (!scaleClass) {
@@ -414,18 +411,13 @@ export default class Chart {
                                scale = new scaleClass({
                                        id,
                                        type: scaleType,
-                                       options: scaleOptions,
                                        ctx: me.ctx,
                                        chart: me
                                });
                                scales[scale.id] = scale;
                        }
 
-                       scale.axis = scale.options.position === 'chartArea' ? 'r' : scale.isHorizontal() ? 'x' : 'y';
-
-                       // parse min/max value, so we can properly determine min/max for other scales
-                       scale._userMin = scale.parse(scale.options.min);
-                       scale._userMax = scale.parse(scale.options.max);
+                       scale.init(scaleOptions);
 
                        // TODO(SB): I think we should be able to remove this custom case (options.scale)
                        // and consider it as a regular scale part of the "scales"" map only! This would
index eb3438e61a401d36ca656e92e22167e291c640d7..5611fd654105f6875d586fe203c28ca08b9f6ccd 100644 (file)
@@ -278,7 +278,7 @@ export default class Scale extends Element {
                /** @type {string} */
                this.type = cfg.type;
                /** @type {object} */
-               this.options = cfg.options;
+               this.options = undefined;
                /** @type {CanvasRenderingContext2D} */
                this.ctx = cfg.ctx;
                /** @type {Chart} */
@@ -344,10 +344,25 @@ export default class Scale extends Element {
                this._borderValue = 0;
        }
 
+       /**
+        * @param {object} options
+        * @since 3.0
+        */
+       init(options) {
+               const me = this;
+               me.options = options;
+
+               me.axis = me.isHorizontal() ? 'x' : 'y';
+
+               // parse min/max value, so we can properly determine min/max for other scales
+               me._userMin = me.parse(options.min);
+               me._userMax = me.parse(options.max);
+       }
+
        /**
         * Parse a supported input value to internal representation.
         * @param {*} raw
-        * @param {number} index
+        * @param {number} [index]
         * @since 3.0
         */
        parse(raw, index) { // eslint-disable-line no-unused-vars
index 5f94df4b8bd1b547f24a5cd4fc1a64fd055bddd6..dc2faf190144cdc722dcbf4c41b5d304306e98b3 100644 (file)
@@ -309,6 +309,11 @@ export default class RadialLinearScale extends LinearScaleBase {
                this.pointLabels = [];
        }
 
+       init(options) {
+               super.init(options);
+               this.axis = 'r';
+       }
+
        setDimensions() {
                const me = this;
 
index 49e5b1b191ca475f1b471c7e16bfcb86f0ca4d06..a4f232a779c36f4a1c2386ee264b98e03f29b2b1 100644 (file)
@@ -555,10 +555,6 @@ export default class TimeScale extends Scale {
        constructor(props) {
                super(props);
 
-               const options = this.options;
-               const time = options.time || (options.time = {});
-               const adapter = this._adapter = new adapters._date(options.adapters.date);
-
                /** @type {{data: number[], labels: number[], all: number[]}} */
                this._cache = {
                        data: [],
@@ -574,12 +570,19 @@ export default class TimeScale extends Scale {
                this._offsets = {};
                /** @type {object[]} */
                this._table = [];
+       }
+
+       init(options) {
+               const time = options.time || (options.time = {});
+               const adapter = this._adapter = new adapters._date(options.adapters.date);
 
                // Backward compatibility: before introducing adapter, `displayFormats` was
                // supposed to contain *all* unit/string pairs but this can't be resolved
                // when loading the scale (adapters are loaded afterward), so let's populate
                // missing formats on update
                mergeIf(time.displayFormats, adapter.formats());
+
+               super.init(options);
        }
 
        /**
index 00b7cfd9affe49208694812dc7edd4629e482540..131070f7da1d479c0890221e203d1c26d8c0fef7 100644 (file)
@@ -76,13 +76,13 @@ describe('Category scale tests', function() {
                var Constructor = Chart.scaleService.getScaleConstructor('category');
                var scale = new Constructor({
                        ctx: {},
-                       options: config,
                        chart: {
                                data: mockData
                        },
                        id: scaleID
                });
 
+               scale.init(config);
                scale.determineDataLimits();
                scale.ticks = scale.buildTicks();
                expect(getValues(scale)).toEqual(mockData.xLabels);
@@ -104,13 +104,13 @@ describe('Category scale tests', function() {
                var Constructor = Chart.scaleService.getScaleConstructor('category');
                var scale = new Constructor({
                        ctx: {},
-                       options: config,
                        chart: {
                                data: mockData
                        },
                        id: scaleID
                });
 
+               scale.init(config);
                scale.determineDataLimits();
                scale.ticks = scale.buildTicks();
                expect(getValues(scale)).toEqual(mockData.yLabels);