]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Check colors definition in the chart options (#11003)
authorstockiNail <stocki.nail@gmail.com>
Wed, 18 Jan 2023 12:31:33 +0000 (13:31 +0100)
committerGitHub <noreply@github.com>
Wed, 18 Jan 2023 12:31:33 +0000 (13:31 +0100)
* Check colors definition in the chart options

* Update src/plugins/plugin.colors.ts

Co-authored-by: Dan Onoshko <danon0404@gmail.com>
* Update src/plugins/plugin.colors.ts

Co-authored-by: Dan Onoshko <danon0404@gmail.com>
* Update src/plugins/plugin.colors.ts

Co-authored-by: Dan Onoshko <danon0404@gmail.com>
* apply review

Co-authored-by: Dan Onoshko <danon0404@gmail.com>
src/plugins/plugin.colors.ts
test/fixtures/plugin.colors/chart-options-colors.js [new file with mode: 0644]
test/fixtures/plugin.colors/chart-options-colors.png [new file with mode: 0644]

index b2817c85a127017a8f4b2fc7484418d36de6cd02..03af80eeabd19fef86abedb8ddb5b88a078a6359 100644 (file)
@@ -81,6 +81,12 @@ function containsColorsDefinitions(
   return false;
 }
 
+function containsColorsDefinition(
+  descriptor: ColorsDescriptor
+) {
+  return descriptor && (descriptor.borderColor || descriptor.backgroundColor);
+}
+
 export default {
   id: 'colors',
 
@@ -95,11 +101,12 @@ export default {
     }
 
     const {
-      options: {elements},
-      data: {datasets}
+      data: {datasets},
+      options: chartOptions
     } = chart.config;
+    const {elements} = chartOptions;
 
-    if (!options.forceOverride && (containsColorsDefinitions(datasets) || elements && containsColorsDefinitions(elements))) {
+    if (!options.forceOverride && (containsColorsDefinitions(datasets) || containsColorsDefinition(chartOptions) || (elements && containsColorsDefinitions(elements)))) {
       return;
     }
 
diff --git a/test/fixtures/plugin.colors/chart-options-colors.js b/test/fixtures/plugin.colors/chart-options-colors.js
new file mode 100644 (file)
index 0000000..5add0c2
--- /dev/null
@@ -0,0 +1,37 @@
+module.exports = {
+  config: {
+    type: 'bar',
+    data: {
+      labels: [0, 1, 2, 3, 4, 5],
+      datasets: [
+        {
+          data: [0, 5, 10, null, -10, -5],
+        },
+        {
+          data: [10, 2, 3, null, 10, 5]
+        }
+      ]
+    },
+    options: {
+      backgroundColor: ['red', 'green'],
+      scales: {
+        x: {
+          ticks: {
+            display: false,
+          }
+        },
+        y: {
+          ticks: {
+            display: false,
+          }
+        }
+      },
+      plugins: {
+        legend: false,
+        colors: {
+          enabled: true
+        }
+      }
+    }
+  }
+};
diff --git a/test/fixtures/plugin.colors/chart-options-colors.png b/test/fixtures/plugin.colors/chart-options-colors.png
new file mode 100644 (file)
index 0000000..5b24310
Binary files /dev/null and b/test/fixtures/plugin.colors/chart-options-colors.png differ