]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Allow updating scale defaults 2486/head
authorEvert Timberg <evert.timberg+github@gmail.com>
Sat, 7 May 2016 15:43:24 +0000 (11:43 -0400)
committerEvert Timberg <evert.timberg+github@gmail.com>
Sat, 7 May 2016 15:43:24 +0000 (11:43 -0400)
docs/01-Scales.md
src/core/core.scaleService.js
test/core.scaleService.tests.js [new file with mode: 0644]

index 9872e94ecb9c5815cf925b7b72777c66af78618e..fd6c27dda997292a2aca083ee9014a8066f84833 100644 (file)
@@ -299,3 +299,15 @@ The radial linear scale extends the core scale class with the following tick tem
        },
 }
 ```
+
+### Update Default Scale config
+The default configuration for a scale can be easily changed using the scale service. Pass in a partial configuration that will be merged with the current scale default configuration.
+
+For example, to set the minimum value of 0 for all linear scales, you would do the following. Any linear scales created after this time would now have a minimum of 0.
+```
+Chart.scaleService.updateScaleDefaults('linear', {
+       ticks: {
+               min: 0
+       }
+})
+```
\ No newline at end of file
index 741c48b679961fe88650f64d624b58d1e83ab0a5..7c351e5d289f6e2396854255376b811782ed1d28 100644 (file)
@@ -24,6 +24,12 @@ module.exports = function(Chart) {
                        // Return the scale defaults merged with the global settings so that we always use the latest ones
                        return this.defaults.hasOwnProperty(type) ? helpers.scaleMerge(Chart.defaults.scale, this.defaults[type]) : {};
                },
+               updateScaleDefaults: function(type, additions) {
+                       var defaults = this.defaults;
+                       if (defaults.hasOwnProperty(type)) {
+                               defaults[type] = helpers.extend(defaults[type], additions);
+                       }
+               },
                addScalesToLayout: function(chartInstance) {
                        // Adds each scale to the chart.boxes array to be sized accordingly
                        helpers.each(chartInstance.scales, function(scale) {
diff --git a/test/core.scaleService.tests.js b/test/core.scaleService.tests.js
new file mode 100644 (file)
index 0000000..ca0eaf2
--- /dev/null
@@ -0,0 +1,29 @@
+// Tests of the scale service
+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);
+
+               // Should equal defaults but not be an identical object
+               expect(Chart.scaleService.getScaleDefaults(type)).toEqual(jasmine.objectContaining({
+                       testProp: true
+               }));
+               
+               Chart.scaleService.updateScaleDefaults(type, {
+                       testProp: 'red',
+                       newProp: 42
+               });
+
+               expect(Chart.scaleService.getScaleDefaults(type)).toEqual(jasmine.objectContaining({
+                       testProp: 'red',
+                       newProp: 42
+               }));
+       });
+});