Skip to content

Commit

Permalink
Check colors definition in the chart options (#11003)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
stockiNail and dangreen committed Jan 18, 2023
1 parent 97bc329 commit ab55f6c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/plugins/plugin.colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ function containsColorsDefinitions(
return false;
}

function containsColorsDefinition(
descriptor: ColorsDescriptor
) {
return descriptor && (descriptor.borderColor || descriptor.backgroundColor);
}

export default {
id: 'colors',

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

Expand Down
37 changes: 37 additions & 0 deletions test/fixtures/plugin.colors/chart-options-colors.js
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ab55f6c

Please sign in to comment.