]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
HTML legend handles doughnut/pie charts (#9044)
authorEvert Timberg <evert.timberg+github@gmail.com>
Sat, 8 May 2021 05:10:18 +0000 (01:10 -0400)
committerGitHub <noreply@github.com>
Sat, 8 May 2021 05:10:18 +0000 (08:10 +0300)
* HTML legend handles doughnut/pie charts

* Fix lint error

docs/samples/legend/html.md

index ab0add511f67a4ecb13c0bd719584f90341fa2a3..a80f05c1d45c1115f29c0383f0218fe55248988e 100644 (file)
@@ -45,7 +45,13 @@ const htmlLegendPlugin = {
       li.style.marginLeft = '10px';
 
       li.onclick = () => {
-        chart.setDatasetVisibility(item.datasetIndex, !chart.isDatasetVisible(item.datasetIndex));
+        const {type} = chart.config;
+        if (type === 'pie' || type === 'doughnut') {
+          // Pie and doughnut charts only have a single dataset and visibility is per item
+          chart.toggleDataVisibility(item.index);
+        } else {
+          chart.setDatasetVisibility(item.datasetIndex, !chart.isDatasetVisible(item.datasetIndex));
+        }
         chart.update();
       };