]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Enable colors plugin for pie charts (#10851)
authorstockiNail <stocki.nail@gmail.com>
Fri, 4 Nov 2022 15:17:49 +0000 (16:17 +0100)
committerGitHub <noreply@github.com>
Fri, 4 Nov 2022 15:17:49 +0000 (11:17 -0400)
* Enable colors plugin for pie charts

* fixes CC

src/plugins/plugin.colors.ts
test/fixtures/plugin.colors/pie.js [new file with mode: 0644]
test/fixtures/plugin.colors/pie.png [new file with mode: 0644]

index 8d4766667d4ef765b974c836bf9765322b50450f..234171cd2b3b580205207316dbb920d6985a1de0 100644 (file)
@@ -55,6 +55,15 @@ function createPolarAreaDatasetColorizer() {
   };
 }
 
+function getColorizer(type: string) {
+  if (type === 'doughnut' || type === 'pie') {
+    return createDoughnutDatasetColorizer();
+  } else if (type === 'polarArea') {
+    return createPolarAreaDatasetColorizer();
+  }
+  return createDefaultDatasetColorizer();
+}
+
 function containsColorsDefinitions(
   descriptors: ColorsDescriptor[] | Record<string, ColorsDescriptor>
 ) {
@@ -91,16 +100,7 @@ export default {
       return;
     }
 
-    let colorizer: DatasetColorizer;
-
-    if (type === 'doughnut') {
-      colorizer = createDoughnutDatasetColorizer();
-    } else if (type === 'polarArea') {
-      colorizer = createPolarAreaDatasetColorizer();
-    } else {
-      colorizer = createDefaultDatasetColorizer();
-    }
-
+    const colorizer: DatasetColorizer = getColorizer(type);
     datasets.forEach(colorizer);
   }
 };
diff --git a/test/fixtures/plugin.colors/pie.js b/test/fixtures/plugin.colors/pie.js
new file mode 100644 (file)
index 0000000..ef58bc1
--- /dev/null
@@ -0,0 +1,23 @@
+module.exports = {
+  config: {
+    type: 'pie',
+    data: {
+      datasets: [
+        {
+          data: [0, 2, 4, null, 6, 8]
+        },
+        {
+          data: [5, 1, 6, 2, null, 9]
+        }
+      ]
+    },
+    options: {
+      plugins: {
+        legend: false,
+        colors: {
+          enabled: true
+        }
+      }
+    }
+  }
+};
diff --git a/test/fixtures/plugin.colors/pie.png b/test/fixtures/plugin.colors/pie.png
new file mode 100644 (file)
index 0000000..c92b35b
Binary files /dev/null and b/test/fixtures/plugin.colors/pie.png differ