我使用了一个很好的方法来更新我在chartjs中的darkmode颜色变化,我从这里获得:Update all Chart.js instances in order to apply changed defaults?
更新到v3或v4后,我的darkmode.js中的以下代码行不再工作,并且颜色更改不会应用,因为图表不再更新:
Chart.helpers.each(Chart.instances, function(instance){
instance.chart.update();
});
我得到以下控制台错误:caught TypeError:* 无法读取undefined的属性(阅读'update')*
我阅读了所有关于破坏性更改的文档,但我无法找到任何与update()结合示例相关的内容。
知道是哪里出了问题吗
我的答案解决了我的问题。我喜欢减少代码。目前我调用这个函数两次,一次是在主题来自本地存储的初始加载时,另一次是在主题切换切换时。有没有办法减少这种情况?
var themeToggleDarkIcon = document.getElementById('theme-toggle-dark-icon');
var themeToggleLightIcon = document.getElementById('theme-toggle-light-icon');
// Change the icons inside the button based on previous settings
if (localStorage.getItem('color-theme') === 'dark' || (!('color-theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
themeToggleLightIcon.classList.remove('hidden');
} else {
themeToggleDarkIcon.classList.remove('hidden');
}
var themeToggleBtn = document.getElementById('theme-toggle');
console.log(Chart.instances);
//Chart.instances.update();
// Force updates to all charts
Chart.helpers.each(Chart.instances, function (instance) {
if (localStorage.getItem('color-theme') === 'dark') {
for (let scale in instance.config.options.scales) {
// Update axes grid color
instance.config.options.scales[scale].grid.color = 'white';
instance.config.options.scales[scale].grid.color = 'white';
Chart.defaults.color = 'white';
instance.config.options.scales[scale].grid.borderColor = 'white';
instance.config.options.scales[scale].grid.borderColor = 'white';
// Update axes label font color
instance.config.options.scales[scale].ticks.color = 'white';
instance.config.options.scales[scale].ticks.color = 'white';
}
} else {
for (let scale in instance.config.options.scales) {
// Update axes grid color
instance.config.options.scales[scale].grid.color =
'rgba(0, 0, 0, 0.1)';
instance.config.options.scales[scale].grid.color =
'rgba(0, 0, 0, 0.1)';
instance.config.options.scales[scale].grid.borderColor =
'rgba(0, 0, 0, 0.1)';
instance.config.options.scales[scale].grid.borderColor =
'rgba(0, 0, 0, 0.1)';
Chart.defaults.color = 'black';
// Update axes label font color
instance.config.options.scales[scale].ticks.color = 'black';
instance.config.options.scales[scale].ticks.color = 'black';
}
}
instance.update();
});
themeToggleBtn.addEventListener('click', function() {
// toggle icons inside button
themeToggleDarkIcon.classList.toggle('hidden');
themeToggleLightIcon.classList.toggle('hidden');
// if set via local storage previously
if (localStorage.getItem('color-theme')) {
if (localStorage.getItem('color-theme') === 'light') {
document.documentElement.classList.add('dark');
localStorage.setItem('color-theme', 'dark');
Chart.defaults.color = 'white';
} else {
document.documentElement.classList.remove('dark');
localStorage.setItem('color-theme', 'light');
Chart.defaults.color = 'black';
}
}
// if NOT set via local storage previously
else {
if (document.documentElement.classList.contains('dark')) {
document.documentElement.classList.remove('dark');
localStorage.setItem('color-theme', 'light');
Chart.defaults.color = 'black';
} else {
document.documentElement.classList.add('dark');
localStorage.setItem('color-theme', 'dark');
Chart.defaults.color = 'white';
}
}
console.log(Chart.instances);
//Chart.instances.update();
// Force updates to all charts
Chart.helpers.each(Chart.instances, function (instance) {
if (localStorage.getItem('color-theme') === 'dark') {
for (let scale in instance.config.options.scales) {
// Update axes grid color
instance.config.options.scales[scale].grid.color = 'white';
instance.config.options.scales[scale].grid.color = 'white';
instance.config.options.scales[scale].grid.borderColor = 'white';
instance.config.options.scales[scale].grid.borderColor = 'white';
// Update axes label font color
instance.config.options.scales[scale].ticks.color = 'white';
instance.config.options.scales[scale].ticks.color = 'white';
}
} else {
for (let scale in instance.config.options.scales) {
// Update axes grid color
instance.config.options.scales[scale].grid.color =
'rgba(0, 0, 0, 0.1)';
instance.config.options.scales[scale].grid.color =
'rgba(0, 0, 0, 0.1)';
instance.config.options.scales[scale].grid.borderColor =
'rgba(0, 0, 0, 0.1)';
instance.config.options.scales[scale].grid.borderColor =
'rgba(0, 0, 0, 0.1)';
// Update axes label font color
instance.config.options.scales[scale].ticks.color = 'black';
instance.config.options.scales[scale].ticks.color = 'black';
}
}
instance.update();
});
});
1条答案
按热度按时间5ssjco0h1#
对于Chart.js v3和v4,
将
instance.chart.update()
替换为instance.update()
应可解决问题。此外,如果您使用Chart.js v1,还需要迁移一些代码,例如Chart.defaults.global,根据文档。
虽然我认为从版本3开始,
Chart.defaults.color
将无法更新轴的字体颜色,轴的标签以及。因此,您应该相应地更新轴(instance.config.options.scales[scale].grid.color
)和轴标签(instance.config.options.scales[scale].ticks.color
)颜色的每个示例。Demo @ StackBlitz
参考:How to Implement Dark Mode in Chart JS (for Chart.JS version 3.6)