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

Scales defaults for "grid.color" and "ticks.color" not updating #11910

Open
teodorachiosa opened this issue Sep 27, 2024 · 1 comment
Open

Scales defaults for "grid.color" and "ticks.color" not updating #11910

teodorachiosa opened this issue Sep 27, 2024 · 1 comment

Comments

@teodorachiosa
Copy link

teodorachiosa commented Sep 27, 2024

Expected behavior

Just like the legend and tooltip color, I expect the scale colors to be updated when the color changes.

Current behavior

The scale grid color and ticks color only update once. Updating the chart (chart.update()) doesn't trigger the color changes.

Reproducible sample

https://codepen.io/teodorachiosa/pen/yLmNPMy

Optional extra steps/info to reproduce

No response

Possible solution

No response

Context

We use a theme switcher to change between a light and a dark mode. We needs to update the defaults when the theme changes.

chart.js version

4.4.4

Browser name and version

Chromium: 129.0.6668.70 (Official Build) (64-bit)

Link to your project

No response

@teodorachiosa teodorachiosa changed the title Default scales grid and ticks color not updating Default scales "grid.color" and "ticks.color" not updating Sep 27, 2024
@teodorachiosa teodorachiosa changed the title Default scales "grid.color" and "ticks.color" not updating Scales defaults for "grid.color" and "ticks.color" not updating Sep 27, 2024
@apurva63
Copy link

The issue you're encountering occurs because Chart.defaults is only applied when the chart is first initialized. Changing Chart.defaults after the chart has been created does not automatically update the existing chart's settings. Chart.defaults are global and do not affect already rendered charts.

To change the chart's colors dynamically after initialization, you should directly modify the chart's options instead of Chart.defaults. Here's how you can fix this:

`function changeScaleColors() {
myChart.options.scales.x.ticks.color = 'teal';
myChart.options.scales.x.grid.color = 'teal';
myChart.options.scales.y.ticks.color = 'teal';
myChart.options.scales.y.grid.color = 'teal';

myChart.options.plugins.legend.labels.color = 'teal';
myChart.options.plugins.tooltip.titleColor = 'teal';
myChart.options.plugins.tooltip.bodyColor = 'teal';

myChart.update();

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants