Allow updating scale defaults

This commit is contained in:
Evert Timberg 2016-05-07 11:43:24 -04:00
parent 29ab84c4c5
commit fc4c23c8d2
3 changed files with 47 additions and 0 deletions

View 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
}
})
```

View 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) {

View File

@ -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
}));
});
});