Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't apply default colors in the colors plugin when defaults are used #11927

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/plugins/plugin.colors.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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',

Expand All @@ -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;
}

Expand Down
34 changes: 34 additions & 0 deletions test/specs/plugin.colors.tests.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
Loading