Assigning Colors via XML Modification

In Tableau, colors are assigned to a given dimension across the workbook (as opposed to just per sheet). This assignment actually happens in the XML, which means you can substitute in different color assignments if you are publishing from templates. Even better, in Tableau 10.0, if you assign colors from one of the included color palettes, new items will automatically be assigned a color from that palette.

If you don’t assign any particular colors, there won’t be any of these sections in the XML, so if you want to check this out, you’ll need to put a dimension on the Color section of the Marks card, and then assign a palette other than default. When you do that and save the file, you’ll find a style section after all of the column and column-instance tags.

Here’s an example from Tableau 10.0:

      <style>
        <style-rule element='mark'>
          <encoding attr='color' field='[none:Sub-Category:nk]' palette='green_orange_cyan_yellow_10_0' type='palette'>
            <map to='#26897e'>
              <bucket>&quot;Labels&quot;</bucket>
            </map>
            <map to='#3ca8bc'>
              <bucket>&quot;Bookcases&quot;</bucket>
            </map>
            <map to='#3ca8bc'>
              <bucket>&quot;Tables&quot;</bucket>
            </map>
            <map to='#4e9f50'>
              <bucket>&quot;Accessories&quot;</bucket>
            </map>
            <map to='#4e9f50'>
              <bucket>&quot;Paper&quot;</bucket>
            </map>
            <map to='#87d180'>
              <bucket>&quot;Appliances&quot;</bucket>
            </map>
            <map to='#87d180'>
              <bucket>&quot;Phones&quot;</bucket>
            </map>
            <map to='#8dbfa8'>
              <bucket>&quot;Machines&quot;</bucket>
            </map>
            <map to='#94a323'>
              <bucket>&quot;Copiers&quot;</bucket>
            </map>
            <map to='#98d9e4'>
              <bucket>&quot;Chairs&quot;</bucket>
            </map>
            <map to='#a08400'>
              <bucket>&quot;Fasteners&quot;</bucket>
            </map>
            <map to='#c3ce3d'>
              <bucket>&quot;Envelopes&quot;</bucket>
            </map>
            <map to='#ef8a0c'>
              <bucket>&quot;Art&quot;</bucket>
            </map>
            <map to='#ef8a0c'>
              <bucket>&quot;Storage&quot;</bucket>
            </map>
            <map to='#f7d42a'>
              <bucket>&quot;Furnishings&quot;</bucket>
            </map>
            <map to='#fcc66d'>
              <bucket>&quot;Binders&quot;</bucket>
            </map>
            <map to='#fcc66d'>
              <bucket>&quot;Supplies&quot;</bucket>
            </map>
          </encoding>
        </style-rule>
      </style>

The only difference in 9.3 is that there is no palette attribute in the encoding tag; because of this, 9.3 and before could not cycle through the palette when a new element entered. This is why it was always preferable to find a way to create a grouping with a fixed number of dimensions. The other solution would be to programmatically add in the new mapping for the Dimension into the XML; this may still be the preferred solution if you have a particular mapping you want for a given dimension value.

I think it’s pretty obvious how the mapping works with hexadecimal colors and mapping of the exact values of the dimensions. If the dimension is numeric, there won’t be the encoded quote values surrounding the values.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s