diff --git a/src/core/core.helpers.js b/src/core/core.helpers.js index 73fc5b5a6a2..273f282e44d 100644 --- a/src/core/core.helpers.js +++ b/src/core/core.helpers.js @@ -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) { diff --git a/src/plugins/plugin.legend.js b/src/plugins/plugin.legend.js index 3d898ad3f14..588e20600fb 100644 --- a/src/plugins/plugin.legend.js +++ b/src/plugins/plugin.legend.js @@ -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) { diff --git a/test/specs/plugin.legend.tests.js b/test/specs/plugin.legend.tests.js index 1b3b84a4b5d..b950c360160 100644 --- a/test/specs/plugin.legend.tests.js +++ b/test/specs/plugin.legend.tests.js @@ -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',