mirror of
https://github.com/chartjs/Chart.js.git
synced 2025-12-08 20:36:08 +00:00
Merge pull request #2640 from chartjs/legend-and-title-as-plugins
Legend and title as plugins
This commit is contained in:
commit
b3f8a53ea5
@ -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);
|
||||
|
||||
@ -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);
|
||||
},
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
@ -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 = [];
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user