]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add check to colors plugin if defaults are set (#11927)
authorJacco van den Berg <jaccoberg2281@gmail.com>
Sun, 13 Oct 2024 07:12:16 +0000 (09:12 +0200)
committerGitHub <noreply@github.com>
Sun, 13 Oct 2024 07:12:16 +0000 (09:12 +0200)
src/plugins/plugin.colors.ts
test/specs/plugin.colors.tests.js

index 03af80eeabd19fef86abedb8ddb5b88a078a6359..a32998a95dcc583ca9da8a5656937d0180c53deb 100644 (file)
@@ -1,4 +1,4 @@
-import {DoughnutController, PolarAreaController} from '../index.js';
+import {DoughnutController, PolarAreaController, defaults} from '../index.js';
 import type {Chart, ChartDataset} from '../types.js';
 
 export interface ColorsPluginOptions {
@@ -87,6 +87,10 @@ function containsColorsDefinition(
   return descriptor && (descriptor.borderColor || descriptor.backgroundColor);
 }
 
+function containsDefaultColorsDefenitions() {
+  return defaults.borderColor !== 'rgba(0,0,0,0.1)' || defaults.backgroundColor !== 'rgba(0,0,0,0.1)';
+}
+
 export default {
   id: 'colors',
 
@@ -106,7 +110,13 @@ export default {
     } = chart.config;
     const {elements} = chartOptions;
 
-    if (!options.forceOverride && (containsColorsDefinitions(datasets) || containsColorsDefinition(chartOptions) || (elements && containsColorsDefinitions(elements)))) {
+    const containsColorDefenition = (
+      containsColorsDefinitions(datasets) ||
+      containsColorsDefinition(chartOptions) ||
+      (elements && containsColorsDefinitions(elements)) ||
+      containsDefaultColorsDefenitions());
+
+    if (!options.forceOverride && containsColorDefenition) {
       return;
     }
 
index df5ff8fb993198cb1dfba139c46b023ae731b6fb..9e66f6f5ea862c533bc2b2ec8c421147a009b5f4 100644 (file)
@@ -1,3 +1,37 @@
 describe('Plugin.colors', () => {
   describe('auto', jasmine.fixture.specs('plugin.colors'));
+
+  describe('Plugin.colors.chartDefaults', () => {
+    beforeAll(() => {
+      Chart.defaults.backgroundColor = ['green', 'yellow'];
+    });
+
+    afterAll(() => {
+      Chart.defaults.backgroundColor = 'rgba(0,0,0,0.1)';
+    });
+
+    it('should not use colors plugin when chart defaults are given', () => {
+      const chart = window.acquireChart({
+        type: 'bar',
+        data: {
+          datasets: [{
+            data: [1, 10],
+            label: 'dataset1'
+          }],
+          labels: ['label1', 'label2']
+        },
+        options: {
+          plugins: {
+            colors: {
+              enabled: true
+            }
+          }
+        }
+      });
+
+      const meta = chart.getDatasetMeta(0);
+      expect(meta.data[0].options.backgroundColor).toBe('green');
+      expect(meta.data[1].options.backgroundColor).toBe('yellow');
+    });
+  });
 });