From 1ae0f5e4d49c271d0958c7e8514823d476a937a0 Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Wed, 25 May 2016 21:23:46 -0400 Subject: [PATCH 1/4] Legend is attached to the graph as a plugin. --- src/chart.js | 4 ++-- src/core/core.controller.js | 10 ---------- src/core/core.legend.js | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/chart.js b/src/chart.js index 58061c2ab6c..f482369e93a 100644 --- a/src/chart.js +++ b/src/chart.js @@ -6,10 +6,10 @@ 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.legend')(Chart); require('./core/core.scale')(Chart); -require('./core/core.scaleService')(Chart); require('./core/core.title')(Chart); require('./core/core.tooltip')(Chart); diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 36edf9e7a93..b96f57d49e9 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -176,16 +176,6 @@ module.exports = function(Chart) { 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() { diff --git a/src/core/core.legend.js b/src/core/core.legend.js index b8ae19eb145..e3005ddfe3f 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); + } + } + }) }; From d6289c6129f1222980ea571f6c6f675182d78f35 Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Wed, 25 May 2016 21:28:02 -0400 Subject: [PATCH 2/4] Convert title block to a plugin --- src/chart.js | 2 +- src/core/core.controller.js | 4 ++-- src/core/core.legend.js | 2 +- src/core/core.title.js | 18 ++++++++++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/chart.js b/src/chart.js index f482369e93a..90b9c77ee8a 100644 --- a/src/chart.js +++ b/src/chart.js @@ -8,9 +8,9 @@ require('./core/core.datasetController')(Chart); require('./core/core.layoutService')(Chart); require('./core/core.scaleService')(Chart); require('./core/core.plugin.js')(Chart); -require('./core/core.legend')(Chart); require('./core/core.scale')(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 b96f57d49e9..e53faaf6a5e 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -167,7 +167,7 @@ module.exports = function(Chart) { }, buildSurroundingItems: function() { - if (this.options.title) { + /*if (this.options.title) { this.titleBlock = new Chart.Title({ ctx: this.chart.ctx, options: this.options.title, @@ -175,7 +175,7 @@ module.exports = function(Chart) { }); Chart.layoutService.addBox(this, this.titleBlock); - } + }*/ }, updateLayout: function() { diff --git a/src/core/core.legend.js b/src/core/core.legend.js index e3005ddfe3f..f625bb8818e 100644 --- a/src/core/core.legend.js +++ b/src/core/core.legend.js @@ -363,5 +363,5 @@ module.exports = function(Chart) { Chart.layoutService.addBox(chartInstance, chartInstance.legend); } } - }) + }); }; diff --git a/src/core/core.title.js b/src/core/core.title.js index b6d82c07ed1..ab969d461c3 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 From 46fc96bf4d97989096aa1024be3d0ee2c9a6a789 Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Wed, 25 May 2016 21:30:22 -0400 Subject: [PATCH 3/4] Remove unused code from core controller. --- src/core/core.controller.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index e53faaf6a5e..a8a9ed58f67 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,18 +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); - }*/ - }, - updateLayout: function() { Chart.layoutService.update(this, this.chart.width, this.chart.height); }, From ae01f1726c209043cf34e0ca2fad875dae4c8eeb Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Thu, 26 May 2016 19:22:11 -0400 Subject: [PATCH 4/4] Fix tests --- test/core.plugin.tests.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/core.plugin.tests.js b/test/core.plugin.tests.js index 5a6891071ef..520305e3980 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 = []; });