]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix error when legend label options are not defined (#4402)
authorEvert Timberg <evert.timberg+github@gmail.com>
Sat, 24 Jun 2017 15:35:46 +0000 (11:35 -0400)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Sat, 24 Jun 2017 15:35:46 +0000 (17:35 +0200)
src/core/core.helpers.js
src/plugins/plugin.legend.js
test/specs/plugin.legend.tests.js

index 73fc5b5a6a20c2deb7185cf59dedb41932a3481c..273f282e44d526c3f4f4c577d448824913857bcb 100644 (file)
@@ -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) {
index 3d898ad3f14c2858ddbce62c48b19a06e0d03625..588e20600fb16fa19fcaadd0e849f97ea74ea71f 100644 (file)
@@ -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) {
index 1b3b84a4b5d3695778e495e58edaf6091cf3ae66..b950c36016089c5018fa11ca55c025a00dd7f127 100644 (file)
@@ -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',