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

Allow dynamic datasets to be colored #10964

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/general/colors.md
Expand Up @@ -84,6 +84,22 @@ const options = {

:::

### Dynamic datasets at runtime

By default the colors plugin only works when you initialize the chart without any colors for the border or background specified.
If you want to force the colors plugin to always color your datasets, for example when using dynamic datasets at runtime you will need to set the `forceColorsPlugin` option to true:

```js
const options = {
plugins: {
colors: {
forceColorsPlugin: true
LeeLenaleee marked this conversation as resolved.
Show resolved Hide resolved
}
}
};
```


### Advanced color palettes

See the [awesome list](https://github.com/chartjs/awesome#plugins) for plugins that would give you more flexibility defining color palettes.
Expand Down
11 changes: 6 additions & 5 deletions src/plugins/plugin.colors.ts
@@ -1,8 +1,9 @@
import {DoughnutController, PolarAreaController} from '../index.js';
import type {Chart, ChartConfiguration, ChartDataset} from '../types.js';
import type {Chart, ChartDataset} from '../types.js';

export interface ColorsPluginOptions {
enabled?: boolean;
forceColorsPlugin: boolean;
LeeLenaleee marked this conversation as resolved.
Show resolved Hide resolved
}

interface ColorsDescriptor {
Expand Down Expand Up @@ -85,20 +86,20 @@ export default {

defaults: {
enabled: true,
forceColorsPlugin: false
},

beforeLayout(chart: Chart, _args, options: ColorsPluginOptions) {
beforeDatasetsUpdate(chart: Chart, _args, options: ColorsPluginOptions) {
if (!options.enabled) {
return;
}

const {
type,
options: {elements},
data: {datasets}
} = chart.config as ChartConfiguration;
} = chart.config;

if (containsColorsDefinitions(datasets) || elements && containsColorsDefinitions(elements)) {
if ((containsColorsDefinitions(datasets) || elements && containsColorsDefinitions(elements)) && !options.forceColorsPlugin) {
LeeLenaleee marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Expand Down