如何将自定义主题应用于视觉效果?

How to apply custom theme to the visuals?

我有一个嵌入式报表,我想根据视觉效果的偶数和奇数来设置视觉效果的主题。我可以通过计算所有过滤器来检查视觉效果的数量。谁能建议我如何将主题应用于视觉效果?

要将主题应用到视觉效果,请找到以下代码片段:

  1. 创建自定义主题:
var themes = [
{
"name": "light",
"dataColors": ["#93A299","#057BE0","#848058"],
"background": "#FFFFFF",
"foreground": "#CF543F",
"tableAccent": "#93A299"
},
{
"name": "dark",
"dataColors": ["#31B6FD","#4584D3", "#5BD078"],
"background": "#000000",
"foreground": "#4584D3",
"tableAccent": "#31B6FD"
}
]
  1. 获取视觉数量:
const visuals = await page.getVisuals();
const num_of_visuals = visuals.length;
  1. 使用 applyTheme API 将主题应用到视觉对象:
// Apply the custom theme for even number of visuals
if(num_of_visuals % 2 == 0){
report.applyTheme({ themeJson: themes.find(theme => theme.name ==="light")});
}
else { // Apply the custom theme for odd number of visuals
report.applyTheme({ themeJson: themes.find(theme => theme.name === "dark") });
}

您可以从以下链接中找到参考 https://docs.microsoft.com/javascript/api/overview/powerbi/get-visuals https://docs.microsoft.com/javascript/api/overview/powerbi/apply-report-themes