From fc4c23c8d24166701aa7d47ee1f94dec50f0b49a Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Sat, 7 May 2016 11:43:24 -0400 Subject: [PATCH] Allow updating scale defaults --- docs/01-Scales.md | 12 ++++++++++++ src/core/core.scaleService.js | 6 ++++++ test/core.scaleService.tests.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 test/core.scaleService.tests.js diff --git a/docs/01-Scales.md b/docs/01-Scales.md index 9872e94ec..fd6c27dda 100644 --- a/docs/01-Scales.md +++ b/docs/01-Scales.md @@ -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 diff --git a/src/core/core.scaleService.js b/src/core/core.scaleService.js index 741c48b67..7c351e5d2 100644 --- a/src/core/core.scaleService.js +++ b/src/core/core.scaleService.js @@ -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 index 000000000..ca0eaf20d --- /dev/null +++ b/test/core.scaleService.tests.js @@ -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 + })); + }); +});