Working with Themes – Part 5

In Part 1 of this series, you learned how to use some of Power BI’s built-in themes to change the color themes of your reports. This of course was better than having to manually customize all the elements in your visualizations, but you were limited in the number of themes available.

In Part 2 you learned about Microsoft’s Theme Gallery where 3rd party developers have offered their custom themes, mostly for free, to anyone who wanted to use them. Yes, you had to go through a few more steps, but this opened the doors to dozens of new themes that you could use to add different colors to your reports.

In Part 3, you learned how to use the new Customize the Current Theme feature that was added as a preview feature in December 2019. One of the great things about this feature is that it allows you to start from any theme and customize it to look exactly the way you want. But that is just part of the benefit of customizing a theme.

In Part 4, you learned how to export your customized theme and to use it in your future Power BI report projects. You discovered that you can create custom themes based on your organization’s colors/branding that you can share them within your department or the entire organization. You also learned how to resolve simple issues when your branding does not appear to work due to prior manual customizations to colors or fonts in your visualizations.

So why do you need to know how to create your own themes from scratch using a simple text editor and JSON? Honestly, after seeing the new Customize the Current Theme feature back in December 2019, I wondered the same thing. I remember many times during my IT career where new languages, tools, or updates to existing products would simplify things that I would have previously spent hours doing directly in code. Many years ago, I came up with an answer to that question which for me said it all.

Use every tool available to you that can make your IT life simpler, but at the same time, understand as much as possible about how things work under the covers so that if you should need to customize something that the new fancy tool does not allow you to do, you can always dive into the code and directly do what is needed.

I remember my first editor from my FoxPro book days (actually it was for FoxPro 2.0, before Microsoft bought them) who said real programmers who buy computer books do not want to read a chapter about how to use form builders or report builders. They want to know how to code forms and reports directly. It took a while, but I eventually won my argument that making basic tasks like building forms and reports easier freed up more time for programmers to focus on more challenging customizations or additional features, or even writing documentation which they never want to do anyway. Besides, suggested that they use the builders as the first pass or foundation of what they needed and then dive back into the code to make the forms or reports do things that the programmers of those builders never imagined. I believe the same applies to this day.

Starting A JSON Theme File

First, what is a JSON file? Simply put, it is a collection of item-value pairs that can be created using any word processing software. Even something like Notepad or Notepad++ (my preference) provides all you need to get started. If you have ever worked with XML, you may already have insight into the way a JSON file looks and works. To illustrate that, let’s look at the JSON for one of the simpler themes I’ve downloaded for Power BI called Civic.

{

“name”: “Civic”,

“dataColors”: [“#D16349”, “#CCB400”, “#8CADAE”, “#8C7B70”,

“#8FB08C”, “#D19049”, “#D1A649”, “#81CC00”, “#8C9DAE”,

“#8C8870”, “#8CB09A”, “#CFD149”],

“background”:”#FFFFFF”,

“foreground”: “#8CADAE”,

“tableAccent”: “#D16349”

}

First, you can see that the JSON file is enclosed in curly braces. Within those braces are a series of “item”: “value” data pairs, five of them in this case each separated with a comma. All JSON files start with a name. In general, a JSON file is considered valid as long as it has a name. While the name value does not have to be the same as the filename itself, it will make your life a lot easier if they are the same. Of course, that would not be a very exciting theme because nothing would change if you applied it to a Power BI report file.

The second item-value pair is much more exciting because it defines the color palette of the new theme. In this case, the values are an array and must therefore appear within square brackets with each value separate from the others with a comma. The item “dataColors” defines the colors used by each data series in a line chart or each column series in a column chart or the wedge colors of a pie chart. It also updates the color palette that appears when you open the dropdown to format the color of an individual data item among other places.


Why Most Themes have 8 Colors and Should Yours?

You should note that a color palette is typically composed of a series of 10 colors with the first two always being white and black. In fact, by default, if you are creating a report visualization that has more than 8 data series, Power BI merely repeats the 8 colors using a slightly different tint or hue from the default palette to differentiate the colors. However, slightly changing the tint or hue of a color may not be obvious to everyone. Obviously, this can make charts very confusing. Perhaps this is the best reason why many report designers tell you that having more than 8 data series or 8 pie wedges should be avoided. Perhaps also for this reason, many themes have “dataColor” arrays with only eight distinct colors. If you remember from the Customize the Current Theme tool discussed Part 3, that dialog allows you to change/set only eight colors.

What if you really needed more than 8 distinct colors but use a template with only 8 unique colors? Well, in that case the first 8 colors will come from your theme, but the rest of the colors will be from the default palette again using different tints and hues just like before you applied your custom palette. Rather than mix themes, one solution to this problem would be to export your theme, edit the JSON file to add enough colors for all the data series or pie chart wedges, and then import the updated theme. For example, the Civic.json file shown above overwrites the first 12 colors and then revert back to the default palette if more colors are needed. But by editing the JSON file directly, you can ‘fix’ that shortcoming. That is what I meant about using tools for the foundation but knowing how to customize that foundation if necessary.

You can also define a color scheme that only changes a single color, perhaps because that color has a low contrast ratio with other colors in the report. For example, you could define a color palette with only a single color such as the following JSON file which changes the first palette color to red. What happens to the rest of colors? The original color palette is used for the next 7 colors and if colors must be repeated with different hues, the original first color in the palette is used, not the red color from the new theme.

{

“name”: “RedOne”,

“dataColors”: [“#aa0000”]

}

That’s great if you want to change only the first color, but what if you wanted to change the fourth color? Is there a way to skip the first 3 colors, perhaps by using a placeholder? Unfortunately, not that I’ve found. However, I could export the JSON for the theme I wanted, change only the fourth color to the one I want like red and upload the modified JSON file (now named Civic2.json) for the report. Again, we use the tool first, and then customize the code second. For example, the following code takes the Civic theme and changes the fourth color to red.

{

“name”: “Civic2”,

“dataColors”: [“#D16349”, “#CCB400”, “#8CADAE”, “#FF0000”,

“#8FB08C”, “#D19049”, “#D1A649”, “#81CC00”, “#8C9DAE”,

“#8C8870”, “#8CB09A”, “#CFD149”],

“background”: “#FFFFFF”,

“foreground”: “#8CADAE”,

“tableAccent”: “#D16349”

}

Revert to Default

Finally, I want to expand on something I talked about last time. The Revert to Default option only applies to manually set colors. It will not reset a data series or data point back to the default color theme if the colors were changed by using a theme. If fact, if you apply a color theme and then manually change the color of say the first color in the color pallet as defined by that theme, the Revert to Default option only reverts that color element to the last applied theme color, not the system default. On the other hand, one theme easily overwrites the colors from a prior color theme. So how can you remember this? It is a hierarchy. Think of CSS code.

  1. The Power BI color theme establishes the foundation set of colors.
  2. Applying a new theme only overwrites the default color theme or another color theme.
  3. Manual color changes overwrite default colors and colors from applied themes
  4. Revert to Default only removes a manual color change returning either the default color or the color of the last applied theme.

Of course, knowing how to change the colors in the dataColors array requires that you can get the RGB value of any color you want to use. While there are many tools that can do this, do you remember the Colour Contrast Analyzer I discussed in Part 2 and the Color Picker in Part 1.

I will continue from this point in my next post.

C’ya.