]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Use same merging logic for init and update (#8006)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Wed, 4 Nov 2020 20:52:20 +0000 (22:52 +0200)
committerGitHub <noreply@github.com>
Wed, 4 Nov 2020 20:52:20 +0000 (15:52 -0500)
* Use same merging logic for init and update
* Add test

src/core/core.config.js
test/specs/core.controller.tests.js
test/utils.js

index 4fda6e38bf32e3f6b465d09d5abe1a68fdb3640f..9723aa264d214f635f118a20c955c412aba5ca88 100644 (file)
@@ -101,25 +101,12 @@ function mergeConfig(...args/* config objects ... */) {
        });
 }
 
-function includeDefaults(options, type) {
-       return mergeConfig(
+function includeDefaults(config, options) {
+       const scaleConfig = mergeScaleConfig(config, options);
+       options = mergeConfig(
                defaults,
-               defaults.controllers[type],
+               defaults.controllers[config.type],
                options || {});
-}
-
-function initConfig(config) {
-       config = config || {};
-
-       // Do NOT use mergeConfig for the data object because this method merges arrays
-       // and so would change references to labels and datasets, preventing data updates.
-       const data = config.data = config.data || {datasets: [], labels: []};
-       data.datasets = data.datasets || [];
-       data.labels = data.labels || [];
-
-       const scaleConfig = mergeScaleConfig(config, config.options);
-
-       const options = config.options = includeDefaults(config.options, config.type);
 
        options.hover = merge(Object.create(null), [
                defaults.interaction,
@@ -140,6 +127,19 @@ function initConfig(config) {
                options.interaction,
                options.tooltips
        ]);
+       return options;
+}
+
+function initConfig(config) {
+       config = config || {};
+
+       // Do NOT use mergeConfig for the data object because this method merges arrays
+       // and so would change references to labels and datasets, preventing data updates.
+       const data = config.data = config.data || {datasets: [], labels: []};
+       data.datasets = data.datasets || [];
+       data.labels = data.labels || [];
+
+       config.options = includeDefaults(config, config.options);
 
        return config;
 }
@@ -171,11 +171,6 @@ export default class Config {
 
        update(options) {
                const config = this._config;
-               const scaleConfig = mergeScaleConfig(config, options);
-
-               options = includeDefaults(options, config.type);
-
-               options.scales = scaleConfig;
-               config.options = options;
+               config.options = includeDefaults(config, options);
        }
 }
index 60a1a4b0ba2a0e21c875c787e683a87ecd7c059b..00d968810d989413ab4ddba39fc381258737aa52 100644 (file)
@@ -355,6 +355,28 @@ describe('Chart', function() {
                });
        });
 
+       describe('Updating options', function() {
+               it('update should result to same set of options as construct', function() {
+                       var chart = acquireChart({
+                               type: 'line',
+                               data: [],
+                               options: {
+                                       animation: false,
+                                       locale: 'en-US',
+                                       responsive: false
+                               }
+                       });
+                       const options = chart.options;
+                       chart.options = {
+                               animation: false,
+                               locale: 'en-US',
+                               responsive: false
+                       };
+                       chart.update();
+                       expect(chart.options).toEqual(jasmine.objectContaining(options));
+               });
+       });
+
        describe('config.options.responsive: true (maintainAspectRatio: false)', function() {
                it('should fill parent width and height', function() {
                        var chart = acquireChart({
index 4a8508de73248ad3aca18700a702ed8ceaced277..010971dde55d5a6d6f728f1d28ef839a2a3ab8bd 100644 (file)
@@ -58,7 +58,6 @@ function acquireChart(config, options) {
        config.options = config.options || {};
        config.options.animation = config.options.animation === undefined ? false : config.options.animation;
        config.options.responsive = config.options.responsive === undefined ? false : config.options.responsive;
-       config.options.fontFamily = config.options.fontFamily || 'Arial';
        config.options.locale = config.options.locale || 'en-US';
 
        wrapper.appendChild(canvas);