Merge pull request #2640 from chartjs/legend-and-title-as-plugins

Legend and title as plugins
This commit is contained in:
Evert Timberg 2016-05-27 21:19:28 -04:00
commit b3f8a53ea5
5 changed files with 46 additions and 25 deletions

View File

@ -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);

View File

@ -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);
},

View File

@ -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);
}
}
});
};

View File

@ -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);
}
}
});
};

View File

@ -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 = [];
});