Prevent attempt to remove the legend or title layout items if they haven't been created but also check if the item to remove is registered with the layout manager to avoid removing the wrong box `splice(-1, 1)`. Add ids to the legend and title plugins to allow to fully disable them (`options: {plugins: {legend: false, title: false}}`).
* @param {Object} layoutItem - the item to remove from the layout
*/
removeBox: function(chart, layoutItem) {
- if (!chart.boxes) {
- return;
+ var index = chart.boxes? chart.boxes.indexOf(layoutItem) : -1;
+ if (index !== -1) {
+ chart.boxes.splice(index, 1);
}
- chart.boxes.splice(chart.boxes.indexOf(layoutItem), 1);
},
/**
// Register the legend plugin
Chart.plugins.register({
+ id: 'legend',
+
beforeInit: function(chart) {
var legendOpts = chart.options.legend;
},
beforeUpdate: function(chart) {
var legendOpts = chart.options.legend;
+ var legend = chart.legend;
if (legendOpts) {
legendOpts = helpers.configMerge(Chart.defaults.global.legend, legendOpts);
- if (chart.legend) {
- chart.legend.options = legendOpts;
+ if (legend) {
+ legend.options = legendOpts;
} else {
createNewLegendAndAttach(chart, legendOpts);
}
- } else {
- Chart.layoutService.removeBox(chart, chart.legend);
+ } else if (legend) {
+ Chart.layoutService.removeBox(chart, legend);
delete chart.legend;
}
},
var noop = helpers.noop;
Chart.Title = Chart.Element.extend({
-
initialize: function(config) {
var me = this;
helpers.extend(me, config);
// Register the title plugin
Chart.plugins.register({
+ id: 'title',
+
beforeInit: function(chart) {
var titleOpts = chart.options.title;
},
beforeUpdate: function(chart) {
var titleOpts = chart.options.title;
+ var titleBlock = chart.titleBlock;
if (titleOpts) {
titleOpts = helpers.configMerge(Chart.defaults.global.title, titleOpts);
- if (chart.titleBlock) {
- chart.titleBlock.options = titleOpts;
+ if (titleBlock) {
+ titleBlock.options = titleOpts;
} else {
createNewTitleBlockAndAttach(chart, titleOpts);
}
- } else {
- Chart.layoutService.removeBox(chart, chart.titleBlock);
+ } else if (titleBlock) {
+ Chart.layoutService.removeBox(chart, titleBlock);
delete chart.titleBlock;
}
}