Skip to content

Commit

Permalink
Allow dynamic datasets to be colored (#10964)
Browse files Browse the repository at this point in the history
* Allow dynamic datasets to be colored

* revieuw
  • Loading branch information
LeeLenaleee committed Dec 14, 2022
1 parent 532c140 commit 41f8128
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 5 deletions.
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 `forceOverride` option to true:

```js
const options = {
plugins: {
colors: {
forceOverride: true
}
}
};
```


### 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;
forceOverride?: boolean;
}

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

defaults: {
enabled: true,
},
forceOverride: false
} as ColorsPluginOptions,

beforeLayout(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 (!options.forceOverride && (containsColorsDefinitions(datasets) || elements && containsColorsDefinitions(elements))) {
return;
}

Expand Down
42 changes: 42 additions & 0 deletions test/fixtures/plugin.colors/dynamic-datasets-default.js
@@ -0,0 +1,42 @@
module.exports = {
config: {
type: 'bar',
data: {
labels: [0, 1, 2, 3, 4, 5],
datasets: [
{
data: [5, 5, 5, 5, 5, 5]
}
]
},
options: {
scales: {
x: {
ticks: {
display: false,
}
},
y: {
ticks: {
display: false,
}
}
},
plugins: {
legend: false,
colors: {
enabled: true
}
}
}
},
options: {
run(chart) {
chart.data.datasets.push({
data: [5, 5, 5, 5, 5, 5]
});

chart.update();
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions test/fixtures/plugin.colors/dynamic-datasets-force-override.js
@@ -0,0 +1,43 @@
module.exports = {
config: {
type: 'bar',
data: {
labels: [0, 1, 2, 3, 4, 5],
datasets: [
{
data: [5, 5, 5, 5, 5, 5]
}
]
},
options: {
scales: {
x: {
ticks: {
display: false
}
},
y: {
ticks: {
display: false
}
}
},
plugins: {
legend: false,
colors: {
enabled: true,
forceOverride: true
}
}
}
},
options: {
run(chart) {
chart.data.datasets.push({
data: [5, 5, 5, 5, 5, 5]
});

chart.update();
}
}
};
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 41f8128

Please sign in to comment.