Skip to content

Commit

Permalink
Fix error when legend label options are not defined (chartjs#4402)
Browse files Browse the repository at this point in the history
  • Loading branch information
etimberg authored and simonbrunel committed Jun 24, 2017
1 parent 2cc3bf6 commit d807986
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core/core.helpers.js
Expand Up @@ -964,7 +964,7 @@ module.exports = function(Chart) {
};
helpers.callback = function(fn, args, thisArg) {
if (fn && typeof fn.call === 'function') {
fn.apply(thisArg, args);
return fn.apply(thisArg, args);
}
};
helpers.getHoverColor = function(colorValue) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/plugin.legend.js
Expand Up @@ -163,8 +163,8 @@ module.exports = function(Chart) {
beforeBuildLabels: noop,
buildLabels: function() {
var me = this;
var labelOpts = me.options.labels;
var legendItems = labelOpts.generateLabels.call(me, me.chart);
var labelOpts = me.options.labels || {};
var legendItems = helpers.callback(labelOpts.generateLabels, [me.chart], me) || [];

if (labelOpts.filter) {
legendItems = legendItems.filter(function(item) {
Expand Down
25 changes: 25 additions & 0 deletions test/specs/plugin.legend.tests.js
Expand Up @@ -156,6 +156,31 @@ describe('Legend block tests', function() {
}]);
});

it('should not throw when the label options are missing', function() {
var makeChart = function() {
window.acquireChart({
type: 'bar',
data: {
datasets: [{
label: 'dataset1',
backgroundColor: '#f31',
borderCapStyle: 'butt',
borderDash: [2, 2],
borderDashOffset: 5.5,
data: []
}],
labels: []
},
options: {
legend: {
labels: false,
}
}
});
};
expect(makeChart).not.toThrow();
});

it('should draw correctly', function() {
var chart = window.acquireChart({
type: 'bar',
Expand Down

0 comments on commit d807986

Please sign in to comment.