diff --git a/src/chart.js b/src/chart.js index 58061c2ab..90b9c77ee 100644 --- a/src/chart.js +++ b/src/chart.js @@ -6,11 +6,11 @@ require('./core/core.animation')(Chart); require('./core/core.controller')(Chart); require('./core/core.datasetController')(Chart); require('./core/core.layoutService')(Chart); -require('./core/core.legend')(Chart); +require('./core/core.scaleService')(Chart); require('./core/core.plugin.js')(Chart); require('./core/core.scale')(Chart); -require('./core/core.scaleService')(Chart); require('./core/core.title')(Chart); +require('./core/core.legend')(Chart); require('./core/core.tooltip')(Chart); require('./elements/element.arc')(Chart); diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 36edf9e7a..a8a9ed58f 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -53,7 +53,6 @@ module.exports = function(Chart) { this.ensureScalesHaveIDs(); this.buildOrUpdateControllers(); this.buildScales(); - this.buildSurroundingItems(); this.updateLayout(); this.resetElements(); this.initToolTip(); @@ -166,28 +165,6 @@ module.exports = function(Chart) { Chart.scaleService.addScalesToLayout(this); }, - buildSurroundingItems: function() { - if (this.options.title) { - this.titleBlock = new Chart.Title({ - ctx: this.chart.ctx, - options: this.options.title, - chart: this - }); - - Chart.layoutService.addBox(this, this.titleBlock); - } - - if (this.options.legend) { - this.legend = new Chart.Legend({ - ctx: this.chart.ctx, - options: this.options.legend, - chart: this - }); - - Chart.layoutService.addBox(this, this.legend); - } - }, - updateLayout: function() { Chart.layoutService.update(this, this.chart.width, this.chart.height); }, diff --git a/src/core/core.legend.js b/src/core/core.legend.js index b8ae19eb1..f625bb881 100644 --- a/src/core/core.legend.js +++ b/src/core/core.legend.js @@ -347,4 +347,21 @@ module.exports = function(Chart) { } }); + // Register the legend plugin + Chart.pluginService.register({ + beforeInit: function(chartInstance) { + var opts = chartInstance.options; + var legendOpts = opts.legend; + + if (legendOpts) { + chartInstance.legend = new Chart.Legend({ + ctx: chartInstance.chart.ctx, + options: legendOpts, + chart: chartInstance + }); + + Chart.layoutService.addBox(chartInstance, chartInstance.legend); + } + } + }); }; diff --git a/src/core/core.title.js b/src/core/core.title.js index b6d82c07e..ab969d461 100644 --- a/src/core/core.title.js +++ b/src/core/core.title.js @@ -177,4 +177,22 @@ module.exports = function(Chart) { } } }); + + // Register the title plugin + Chart.pluginService.register({ + beforeInit: function(chartInstance) { + var opts = chartInstance.options; + var titleOpts = opts.title; + + if (titleOpts) { + chartInstance.titleBlock = new Chart.Title({ + ctx: chartInstance.chart.ctx, + options: titleOpts, + chart: chartInstance + }); + + Chart.layoutService.addBox(chartInstance, chartInstance.titleBlock); + } + } + }); }; \ No newline at end of file diff --git a/test/core.plugin.tests.js b/test/core.plugin.tests.js index 5a6891071..520305e39 100644 --- a/test/core.plugin.tests.js +++ b/test/core.plugin.tests.js @@ -1,5 +1,14 @@ // Plugin tests describe('Test the plugin system', function() { + var oldPlugins; + + beforeAll(function() { + oldPlugins = Chart.plugins; + }); + afterAll(function() { + Chart.plugins = oldPlugins; + }); + beforeEach(function() { Chart.plugins = []; });