Nik*_* E. 2 javascript default html5-canvas chart.js
我目前正在集成动态深色主题功能,并且需要在更改主题时更改Chart.js的默认值。它看起来像这样(高度简化):
function changeTheme(darkTheme = false) {
if (darkTheme) {
document.body.classList.add('dark-theme');
Chart.defaults.global.defaultFontColor = 'white';
} else {
document.body.classList.remove('dark-theme');
Chart.defaults.global.defaultFontColor = 'black';
}
}
Run Code Online (Sandbox Code Playgroud)
页面上的现有图表不会立即应用更改后的默认设置。仅在更新时进行(例如在工具提示上、数据更改、画布大小调整)。
我可以通过调用 Chart 的实例方法来手动更新图表.update()。但我无权访问所有现有的图表对象(无权访问创建它们的脚本的范围),并且我没有找到全局获取它们的方法。
如果能回答上述问题之一,我就已经很满意了。
显示并运行代码片段。点击“toggleDarkTheme”按钮,然后点击“投票数”或其他内容来触发更新。
// set function for theme
function changeTheme(darkTheme = false) {
if (darkTheme) {
document.body.classList.add('dark-theme');
Chart.defaults.global.defaultFontColor = 'white';
} else {
document.body.classList.remove('dark-theme');
Chart.defaults.global.defaultFontColor = 'black';
}
}
changeTheme(false);
// button to toggle theme
let darkThemeActive = false;
document.getElementById('toggleDarkTheme').addEventListener('click', ()=>{
darkThemeActive = !darkThemeActive;
changeTheme(darkThemeActive);
});
/* example chart, pretend like you dont have access to this scope ;) */
{
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
responsive: true
}
});
document.getElementById('triggerManualUpdate').addEventListener('click', ()=>{
myChart.update();
});
}Run Code Online (Sandbox Code Playgroud)
button {
background-color: #eee;
color: rgba(0,0,0,0.87);
border: none;
border-radius: 3px;
padding: 0.5rem;
}
.dark-theme {
background-color: #212121;
color: white;
}
.dark-theme button {
background-color: #212121;
color: white;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.3/dist/Chart.min.js"></script>
<div>
<button id="toggleDarkTheme">toggleDarkTheme</button>
<button id="triggerManualUpdate">triggerManualUpdate</button>
</div>
<canvas id="myChart" width="400" height="400"></canvas>Run Code Online (Sandbox Code Playgroud)
您可以使用该对象动态循环遍历图表实例Chart.instances。例如:
Chart.helpers.each(Chart.instances, function(instance){
instance.chart.update();
});
Run Code Online (Sandbox Code Playgroud)
我修改了下面的示例以更新切换功能中的所有图表。您会注意到字体颜色现在随着切换而变化。
Chart.helpers.each(Chart.instances, function(instance){
instance.chart.update();
});
Run Code Online (Sandbox Code Playgroud)
// set function for theme
function changeTheme(darkTheme = false) {
if (darkTheme) {
document.body.classList.add('dark-theme');
Chart.defaults.global.defaultFontColor = 'white';
} else {
document.body.classList.remove('dark-theme');
Chart.defaults.global.defaultFontColor = 'black';
}
// Force updates to all charts
Chart.helpers.each(Chart.instances, function(instance){
instance.chart.update();
});
}
changeTheme(false);
// button to toggle theme
let darkThemeActive = false;
document.getElementById('toggleDarkTheme').addEventListener('click', ()=>{
darkThemeActive = !darkThemeActive;
changeTheme(darkThemeActive);
});
/* example chart, pretend like you dont have access to this scope ;) */
{
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
responsive: true
}
});
}Run Code Online (Sandbox Code Playgroud)
button {
background-color: #eee;
color: rgba(0,0,0,0.87);
border: none;
border-radius: 3px;
padding: 0.5rem;
}
.dark-theme {
background-color: #212121;
color: white;
}
.dark-theme button {
background-color: #212121;
color: white;
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3073 次 |
| 最近记录: |