更新所有 Chart.js 个实例以应用更改后的默认值?

Update all Chart.js instances in order to apply changed defaults?

情况:

我目前正在集成动态深色主题功能,需要更改 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';
  }
}


问题:

页面上的现有图表不会立即应用更改后的默认设置。仅在他们进行更新时(例如,在工具提示、数据更改、调整 canvas 大小)时。

我可以通过调用 Chart 的实例方法 .update() 手动更新图表。但是我无权访问所有现有图表对象(无权访问创建它们的脚本的范围)并且我没有找到全局获取它们的方法。


问题:

  1. 有没有办法在没有图表实例的情况下强制更新所有图表?
  2. 或者:有没有办法让所有的图表实例全部更新?
  3. 或:这是 XY Problem 并且有解决这个问题的方法我没有考虑吗?

我已经对上述问题之一的答案感到满意了。


重现问题的示例:

显示并 运行 代码片段。点击“toggleDarkTheme”按钮并点击“# of Votes”或其他按钮来触发更新。

// 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();
  });
}
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;
}
<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>

您可以使用 Chart.instances 对象动态循环遍历图表实例。例如:

Chart.helpers.each(Chart.instances, function(instance){
  instance.chart.update();
});

我修改了下面的示例以更新切换功能中的所有图表。您会注意到字体颜色现在随着切换而改变。

// 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
      }
  });
}
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;
}
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.3/dist/Chart.min.js"></script>
<div>
  <button id="toggleDarkTheme">toggleDarkTheme</button>
</div>
<canvas id="myChart" width="400" height="400"></canvas>