]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Check index bounds of datasets for isDatasetVisible (#7986)
authorTakuya Uehara <indigo.lain@gmail.com>
Sat, 31 Oct 2020 12:10:17 +0000 (21:10 +0900)
committerGitHub <noreply@github.com>
Sat, 31 Oct 2020 12:10:17 +0000 (08:10 -0400)
* Check index bounds of datasets
* Add test for isDatasetVisible

src/core/core.controller.js
test/specs/core.controller.tests.js

index 18849bad5431948a164dff83fe86c16995b82f86..20e097f3e6e4e378362a0145c8145eabf0567558 100644 (file)
@@ -717,11 +717,16 @@ class Chart {
        }
 
        isDatasetVisible(datasetIndex) {
+               const dataset = this.data.datasets[datasetIndex];
+               if (!dataset) {
+                       return false;
+               }
+
                const meta = this.getDatasetMeta(datasetIndex);
 
                // meta.hidden is a per chart dataset hidden flag override with 3 states: if true or false,
                // the dataset.hidden value is ignored, else if null, the dataset hidden state is returned.
-               return typeof meta.hidden === 'boolean' ? !meta.hidden : !this.data.datasets[datasetIndex].hidden;
+               return typeof meta.hidden === 'boolean' ? !meta.hidden : !dataset.hidden;
        }
 
        setDatasetVisibility(datasetIndex, visible) {
index 5d3a5202f1ca838077639880efd2fd0e57545ac3..6192f9bdf33d187bb40b1163fcbba6e049760d6e 100644 (file)
@@ -1493,6 +1493,22 @@ describe('Chart', function() {
                });
        });
 
+       describe('isDatasetVisible', function() {
+               it('should return false if index is out of bounds', function() {
+                       var chart = acquireChart({
+                               type: 'line',
+                               data: {
+                                       datasets: [{
+                                               data: [0, 1, 2]
+                                       }],
+                                       labels: ['a', 'b', 'c']
+                               }
+                       });
+
+                       expect(chart.isDatasetVisible(1)).toBe(false);
+               });
+       });
+
        describe('getChart', function() {
                it('should get the chart from the canvas ID', function() {
                        var chart = acquireChart({