Polygons in Tableau are geometric shapes created by connecting a series of coordinate points. These shapes can be used to represent various objects or areas in a visualisation, allowing for precise mapping and analysis of data within those defined shapes. While Tableau offers the flexibility to plot intricate shapes, crafting custom polygons can be a meticulous and time consuming process. You would have to specify all the X and Y coordinates of each point in the shape and define the order in which they are to be connected. There are tools available to streamline this process, but it still requires a lot of fine-tuning and attention to detail.

Fortunately, there is an image file format that already contains all the necessary information to draw polygons in Tableau. SVG stands for Scalable Vector Graphics, and it uses path instructions instead of pixels to render images. By parsing these path instructions, we can extract the coordinates of the shape’s points and the corresponding drawing paths. Essentially, all the components needed to create complex images are packaged within an XML file.

Just as a brief aside, SVG paths come in two main flavours: absolute and relative. Absolute paths use fixed coordinates, specifying exact positions to draw each segment of the shape. In contrast, relative paths use relative distance to the previous point, and extra calculations will be required to determine the exact coordinate of each subsequent point. More information can be found here. For this demonstration, I will be using an SVG image with absolute paths, as it is more straightforward to work with.

Let’s create our own Porygon in Tableau

.

You can download the SVG file from this link.

Inside the hood: De-mystifying path instructions

While SVG files typically open as images, you can also open them in a text editor to explore the underlying code, as SVG is an XML-based file format. What we can see below are the dimensions of the image, groups of path instructions for various shapes in the image, pairs of X and Y coordinates, and the fill colour hexcode for each of the elements.

The information we are most interested in are the following:

  1.  The width and height of the image. This defines the boundaries of the image, and ensures that the image will be scaled proportionally in our plot. I will also be using this for some coordinate adjustment later on.
  2. Path elements. Path elements contain the instructions for drawing the shapes in the images. Multiple path elements come together to create a complex image, thus preserving the grouping is essential.
  3. Coordinates. As our SVG file is using absolute paths, the numbers following “d=” represent pairs of X and Y coordinates. You’ve probably noticed various letters preceding some pairs of coordinates. M stands for “Move to”, i.e. the starting point of the element. Other letters such as C, Q and L describe the shape of the line.
    Lucky for us, Tableau only draws in straight lines, so we can safely ignore these letters when we move on to prepare the data.
  4. Fill colour. If you would like to recreate the colours used in the image, the hexcodes can be conveniently extracted to create a custom colour palette in Tableau. More on that later.

 

Data Preparation

I will be preparing my data in Alteryx, but the process should be replicable on any data preparation platform of your choice.

Connecting to the Data

Alteryx does not come with SVG support by default. To load SVG files as XML in Alteryx, we can override the default file type detection and force the software to recognize SVG files as XML.

Getting the X and Y coordinates

  1. Extracting X and Y coordinates:
    • Utilize appropriate string operations, such as Text to Columns or Regex, to extract the X and Y values from the coordinate pairs (d).
    • Split the coordinate pairs into separate X (d1) and Y (d2) values.

 

Adjusting the Y coordinates

This next step serves to flip the image the right side up. Due to the manner the path instructions are coded, using the Y coordinates as it will result in a flipped image, as illustrated below.

To correct the image, simply take the height of the image (extracted from Step 1 in the previous section) and deduct all the current Y axis values from it. Alternatively, you can simply reverse the y axis in Tableau.

Creating Path Order and Path Grouping

The penultimate step in our data preparation process is to group the path instructions and arrange them in the prescribed order as defined in the SVG path instructions. These are essential to enable Tableau to convert the points into distinct polygons, ensuring accurate rendering of the desired shapes.

Generating the Colour Palette

Lastly, I generated a separate extract specifically for the fill colors used in the image and attached the <color> tag using a formula tool. This simplifies the process of copying and pasting the color information into Tableau’s Preferences.tps. It is worth noting that even though there may be duplicated fill colors, it is advisable to retain them as is to avoid the manual matching of colors across over 50 individual polygons.

 

Output

These are the fields you’ll need for drawing polygons in Tableau.

  1. X and Y coordinates in separate columns
  2. Path sequence specifying the order in which the coordinates should be connected
  3. Groups that define individual polygons

To make colouring the Porygon much easier, I will also be generating a list of the fill colours used in the SVG. Order the colours by group, then paste it in that order into the Preferences.tps document in your Tableau Repository folder.

And that concludes the data preparation phase! In my next post, I will guide you through the process of plotting the polygons in Tableau.

The Data School
Author: The Data School