mirror of
https://github.com/chartjs/Chart.js.git
synced 2024-10-06 12:19:08 +02:00
Allow updating scale defaults
This commit is contained in:
parent
29ab84c4c5
commit
fc4c23c8d2
@ -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
|
||||
}
|
||||
})
|
||||
```
|
@ -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) {
|
||||
|
29
test/core.scaleService.tests.js
Normal file
29
test/core.scaleService.tests.js
Normal 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
|
||||
}));
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user