mirror of
https://github.com/chartjs/Chart.js.git
synced 2024-10-06 12:19:08 +02:00
Use same merging logic for init and update (#8006)
* Use same merging logic for init and update * Add test
This commit is contained in:
parent
e277aa4c1f
commit
aae53c835e
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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({
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user