Skip to content

Commit 3f2968c

Browse files
authored
Add check to colors plugin if defaults are set (#11927)
1 parent 5d2dfbe commit 3f2968c

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/plugins/plugin.colors.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {DoughnutController, PolarAreaController} from '../index.js';
1+
import {DoughnutController, PolarAreaController, defaults} from '../index.js';
22
import type {Chart, ChartDataset} from '../types.js';
33

44
export interface ColorsPluginOptions {
@@ -87,6 +87,10 @@ function containsColorsDefinition(
8787
return descriptor && (descriptor.borderColor || descriptor.backgroundColor);
8888
}
8989

90+
function containsDefaultColorsDefenitions() {
91+
return defaults.borderColor !== 'rgba(0,0,0,0.1)' || defaults.backgroundColor !== 'rgba(0,0,0,0.1)';
92+
}
93+
9094
export default {
9195
id: 'colors',
9296

@@ -106,7 +110,13 @@ export default {
106110
} = chart.config;
107111
const {elements} = chartOptions;
108112

109-
if (!options.forceOverride && (containsColorsDefinitions(datasets) || containsColorsDefinition(chartOptions) || (elements && containsColorsDefinitions(elements)))) {
113+
const containsColorDefenition = (
114+
containsColorsDefinitions(datasets) ||
115+
containsColorsDefinition(chartOptions) ||
116+
(elements && containsColorsDefinitions(elements)) ||
117+
containsDefaultColorsDefenitions());
118+
119+
if (!options.forceOverride && containsColorDefenition) {
110120
return;
111121
}
112122

test/specs/plugin.colors.tests.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
11
describe('Plugin.colors', () => {
22
describe('auto', jasmine.fixture.specs('plugin.colors'));
3+
4+
describe('Plugin.colors.chartDefaults', () => {
5+
beforeAll(() => {
6+
Chart.defaults.backgroundColor = ['green', 'yellow'];
7+
});
8+
9+
afterAll(() => {
10+
Chart.defaults.backgroundColor = 'rgba(0,0,0,0.1)';
11+
});
12+
13+
it('should not use colors plugin when chart defaults are given', () => {
14+
const chart = window.acquireChart({
15+
type: 'bar',
16+
data: {
17+
datasets: [{
18+
data: [1, 10],
19+
label: 'dataset1'
20+
}],
21+
labels: ['label1', 'label2']
22+
},
23+
options: {
24+
plugins: {
25+
colors: {
26+
enabled: true
27+
}
28+
}
29+
}
30+
});
31+
32+
const meta = chart.getDatasetMeta(0);
33+
expect(meta.data[0].options.backgroundColor).toBe('green');
34+
expect(meta.data[1].options.backgroundColor).toBe('yellow');
35+
});
36+
});
337
});

0 commit comments

Comments
 (0)