Jump to content

Issues when using Treemap as Color Legend


Marcos Telles

Recommended Posts

 

 

So I've been using the treemap for something it is not designed for, but I really like it having the function of legend. By that I mean I make a Treemap with Size by (none), Columns and Hierarchy I use the same column that I use to color all other visualizations. When I manually set the same Color Scheme in everything, it works perfectly. This allows me to save a lot of screen space by hiding the individual legends of each visualization, and I can still can highlight individual colors by clicking on the Treemap.

Trouble started when I tried to make a Dropdown menu, associeted with a property to change the column I'm using to Color By and the Color Scheme. Obviously everything I want is to have the same colors linked to the same values everywhere. I've tried writting several different IronPython scrips but I always end up with the same issue: it works for all visualizations, except for the Treemap, that is always showing different colors than the rest.

I also had one issue, the Treemap was automatically changing to Color Mode Gradient whenver the column selected by the dropdown was not a String. No script I made could change this behaviour, the only solution I found was changing all columns to String. But this is also not a perfect solution, I would like to be able to use both Color Mode Gradient or Categorical.

Are there any limitations or bugs in the Treemap that are preventing this from working? I feel like no Script can access the Treemap and apply a Color Scheme nor change it's Color Mode.

Example of scripts I've tried:

1:

# Define function to set color by property for each visualization type
def set_color_by(visual, column_name):
    visual.ColorAxis.Expression = "[{}]".format(column_name)

# Define function to set color mode for Treemap
def set_treemap_properties(treemap, column_name):
    treemap.ColorAxis.Expression = "[{}]".format(column_name)
    treemap.ColorAxis.Coloring = "UniqueValues"

# Get the active page in the analysis
active_page = Application.Document.ActivePageReference

# Get the color by property
color_by = Document.Properties["ColorBy"]

# Get the selected column name
selected_column_name = color_by

# Loop through all visualizations on the active page
for visual in active_page.Visuals:
    # Check if the visualization supports color axis
    if hasattr(visual, "ColorAxis"):
        # Set color by property for Scatter Plot and Bar Chart
        set_color_by(visual, selected_column_name)
        # Set properties for Treemap
        if visual.TypeId.Name == "TreeMap":
            set_treemap_properties(visual, selected_column_name)

2.

# Define function to set color by property for each visualization type
def set_color_by(visual, column_name):
    visual.ColorAxis.Expression = "[{}]".format(column_name)

# Define function to set color mode for Treemap
def set_treemap_properties(treemap, column_name):
    treemap.ColorAxis.Expression = "[{}]".format(column_name)
    treemap.ColorAxis.Coloring = "UniqueValues"

# Get the active page in the analysis
active_page = Application.Document.ActivePageReference

# Get the color by property
color_by = Document.Properties["ColorBy"]

# Get the selected column name
selected_column_name = color_by

# Loop through all visualizations on the active page
for visual in active_page.Visuals:
    # Check if the visualization supports color axis
    if hasattr(visual, "ColorAxis"):
        # Set color by property for Scatter Plot and Bar Chart
        set_color_by(visual, selected_column_name)
        # Set properties for Treemap
        if visual.TypeId.Name == "TreeMap":
            set_treemap_properties(visual, selected_column_name)

3.

# Define function to set color by property for each visualization type
def set_color_by(visual, column_name):
    visual.ColorAxis.Expression = "[{}]".format(column_name)

# Define function to set color mode for Treemap based on data type
def set_treemap_properties(treemap, column_name, data_type):
    treemap.ColorAxis.Expression = "[{}]".format(column_name)
    if data_type == "Integer" or data_type == "Real":
        treemap.ColorAxis.Coloring = "Gradient"
    else:
        treemap.ColorAxis.Coloring = "UniqueValues"

# Get the active page in the analysis
active_page = Application.Document.ActivePageReference

# Get the color by property and its data type
color_by = Document.Properties["ColorBy"]
color_by_column = None
color_by_data_type = None

for visual in active_page.Visuals:
    if hasattr(visual, "Data"):
        for table in visual.Data.DataTableReference:
            if color_by in table.Columns:
                color_by_column = table.Columns[color_by]
                color_by_data_type = color_by_column.DataType.ToString()
                break
    if color_by_column is not None:
        break

# Get the selected column name
selected_column_name = color_by

# Loop through all visualizations on the active page
for visual in active_page.Visuals:
    # Check if the visualization supports color axis
    if hasattr(visual, "ColorAxis"):
        # Set color by property for Scatter Plot and Bar Chart
        set_color_by(visual, selected_column_name)
        # Set properties for Treemap based on data type
        if visual.TypeId.Name == "TreeMap":
            set_treemap_properties(visual, selected_column_name, color_by_data_type)

4.

# Define function to set color by property for each visualization type
def set_color_by(visual, column_name):
    visual.ColorAxis.Expression = "[{}]".format(column_name)

# Define function to set color mode for Treemap and other visualizations
def set_visual_properties(visual, column_name, data_type):
    if visual.TypeId.Name == "TreeMap":
        visual.ColorAxis.Coloring = "UniqueValues"
    else:
        visual.ColorAxis.Expression = "[{}]".format(column_name)

# Get the active page in the analysis
active_page = Application.Document.ActivePageReference

# Get the color by property and its data type
color_by = Document.Properties["ColorBy"]

# Loop through all visualizations on the active page
for visual in active_page.Visuals:
    # Check if the visualization supports color axis
    if hasattr(visual, "ColorAxis"):
        # Set color properties for each visualization
        set_visual_properties(visual, color_by, None)

5.

# Define function to set color by property for each visualization type
def set_color_by(visual, column_name):
    visual.ColorAxis.Expression = "[{}]".format(column_name)

# Define function to set color mode for Treemap and other visualizations
def set_visual_properties(visual, column_name, data_type):
    if visual.TypeId.Name == "TreeMap":
        visual.Properties["ColorAxis.Coloring"] = "UniqueValues"
        visual.ColorAxis.Expression = "[{}]".format(column_name)
    else:
        visual.ColorAxis.Expression = "[{}]".format(column_name)

# Get the active page in the analysis
active_page = Application.Document.ActivePageReference

# Get the color by property and its data type
color_by = Document.Properties["ColorBy"]

# Loop through all visualizations on the active page
for visual in active_page.Visuals:
    # Check if the visualization supports color axis
    if hasattr(visual, "ColorAxis"):
        # Set color properties for each visualization
        set_visual_properties(visual, color_by, None)

6.

import time

# Define function to set color by property for each visualization type
def set_color_by(visual, column_name):
    visual.ColorAxis.Expression = "[{}]".format(column_name)

# Get the active page in the analysis
active_page = Application.Document.ActivePageReference

# Get the color by property and its data type
color_by = Document.Properties["ColorBy"]

# Loop through all visualizations on the active page
for visual in active_page.Visuals:
    # Check if the visualization supports color axis
    if hasattr(visual, "ColorAxis"):
        # Set color by property for each visualization
        set_color_by(visual, color_by)

        # If the visualization is a Treemap, periodically force the color mode to "Unique Values"
        if visual.TypeId.Name == "TreeMap":
            while True:
                try:
                    visual.Properties["ColorAxis.Coloring"] = "UniqueValues"
                    break
                except:
                    # If setting the color mode fails, retry after a short delay
                    time.sleep(1)

7.

# Define function to set color by property for each visualization type
def set_color_by(visual, column_name):
    visual.ColorAxis.Expression = "[{}]".format(column_name)

# Define function to set color mode for Treemap and other visualizations
def set_visual_properties(visual, column_name, data_type):
    if visual.TypeId.Name == "TreeMap":
        visual.ColorAxis.Expression = "[{}]".format(column_name)
        visual.Data.PropertyControls["ColorAxis.Coloring"].SetPropertyValue("Coloring", "UniqueValues")
    else:
        visual.ColorAxis.Expression = "[{}]".format(column_name)

# Get the active page in the analysis
active_page = Application.Document.ActivePageReference

# Get the color by property and its data type
color_by = Document.Properties["ColorBy"]

# Loop through all visualizations on the active page
for visual in active_page.Visuals:
    # Check if the visualization supports color axis
    if hasattr(visual, "ColorAxis"):
        # Set color properties for each visualization
        set_visual_properties(visual, color_by, None)

8.

# Define function to set color by property and color scheme for each visualization type
def set_visual_properties(visual, column_name, color_scheme):
    visual.ColorAxis.Expression = "[{}]".format(column_name)
    visual.ColorAxis.Coloring = "ByColumn"
    visual.ColorAxis.ScaleType = "CategoricalColor"
    visual.ColorAxis.PaletteName = color_scheme

# Get the active page in the analysis
active_page = Application.Document.ActivePageReference

# Get the color by property
color_by = Document.Properties["ColorBy"]

# Define the color scheme (e.g., "Spotfire categorical classic")
color_scheme = "Spotfire categorical classic"

# Loop through all visualizations on the active page
for visual in active_page.Visuals:
    # Check if the visualization supports color axis
    if hasattr(visual, "ColorAxis"):
        # Set color properties for each visualization
        set_visual_properties(visual, color_by, color_scheme)

        # Special handling for Treemap to force it to use the specified color scheme
        if visual.TypeId.Name == "TreeMap":
            visual.ColorAxis.PaletteName = color_scheme

9.

# Define function to set color by property and color scheme for each visualization type
def set_visual_properties(visual, column_name, color_scheme):
    visual.ColorAxis.Expression = "[{}]".format(column_name)
    visual.ColorAxis.Coloring = "ByColumn"
    visual.ColorAxis.ScaleType = "CategoricalColor"
    visual.ColorAxis.PaletteName = color_scheme

# Get the active page in the analysis
active_page = Application.Document.ActivePageReference

# Get the color by property
color_by = Document.Properties["ColorBy"]

# Define the color scheme (e.g., "Spotfire categorical")
color_scheme = "Spotfire categorical"

# Loop through all visualizations on the active page
for visual in active_page.Visuals:
    # Check if the visualization supports color axis
    if hasattr(visual, "ColorAxis"):
        # Set color properties for each visualization
        set_visual_properties(visual, color_by, color_scheme)

 

Link to comment
Share on other sites

Can you show some screenshots of where it goes wrong, and show the property data type/values?
When I try to set the colour by in a TreeMap, even for a string, it defaults to UniqueCount. The way to fix it was to:

1. add First() around the property name, as it wants an aggregation

2. set the colour mode to Unique Values.

When you say that you want to colour non-string values in a tree map, unless these are integers with few values, I am not sure you can do it, because the tree map wants an aggregation. Can you provide an example of that?

Link to comment
Share on other sites

Hello Gaia,

Thank you very much for the reply!

I think I've partially solved the issue, there must be a bug on the Treemap color. What I did was, under Tools-Options-Visualization, I've selected a "Default categorical color scheme" and a "Default continuous color scheme". After doing that, the Treemap stopped going back to the Color Scheme "Spotfire categorical" by itself when changing my dropdown - I was trying to use "Spotfire categorical classic". But only when all my columns are String. 

As the data I'm working on is confidential, I've made another workbook as sample for you.

These are the columns I made:

image.png.134193439efd0f529dbc863a5d76037d.png

So when I change my Dropdown from Manufacturer to Serial Number, this is what happens, the Treemap goes to "Color mode: Gradient".

image.thumb.png.f6856628a323c665f613554f7b30b85e.png

And when I change it manually to "Color mode: Unique Values", it uses the wrong Color scheme.

image.thumb.png.aa92ba155fdf98566206d464f66eccd2.png

If I switch back my dropdown to "Manufacturer" everything is fine, when I switch again to "Serial Number" then Color Mode goes back to Gradient as the first screenshot. The only way to prevent this is to change all columns Data Type to String, which I would not like to do. 

So I would like a script that ensures all visualizations are using the same Color Mode and the same Color Scheme matching colors and values, independently of which is the Data Type of the Column I'm choosing. Also because I will need to use Color Schemes saved as Document Color Scheme too.

Link to comment
Share on other sites

On 3/4/2024 at 11:47 AM, Gaia Paolini said:

Can you show some screenshots of where it goes wrong, and show the property data type/values?
When I try to set the colour by in a TreeMap, even for a string, it defaults to UniqueCount. The way to fix it was to:

1. add First() around the property name, as it wants an aggregation

2. set the colour mode to Unique Values.

When you say that you want to colour non-string values in a tree map, unless these are integers with few values, I am not sure you can do it, because the tree map wants an aggregation. Can you provide an example of that?

 

Link to comment
Share on other sites

Hi Marcos,

Looking at this question, the suggestions that you received and the follow-up questions that you ask covering a large number of scripts and visualizations, this is beyond what we would like to handle as community questions. You will likely benefit from working with a Spotfire partner to get some development support. Please check out https://www.spotfire.com/partners/partner-locator for the directory of Spotfire partners. Or you can reach out to your account team and they will be happy to recommend a partner or make an introduction. We'll be very happy to answer your future questions in the community, it will be helpful if you try to make them a bit shorter so that contributors to the community (Spotfire employees, Spotfire partners, Spotfire users) can answer them within a reasonable amount of time. Here are some tips for asking questions. Dr Spotfire also monitors the community in case some topics can benefit from being covered in a Dr Spotfire session. Keep an eye out on the events page for Dr Spotfire topics that may be of use for you (longer webinars, free training)

 

Kind regards,

Heleen Snelting

Link to comment
Share on other sites

On 3/12/2024 at 2:21 PM, Heleen Snelting said:

Hi Marcos,

Looking at this question, the suggestions that you received and the follow-up questions that you ask covering a large number of scripts and visualizations, this is beyond what we would like to handle as community questions. You will likely benefit from working with a Spotfire partner to get some development support. Please check out https://www.spotfire.com/partners/partner-locator for the directory of Spotfire partners. Or you can reach out to your account team and they will be happy to recommend a partner or make an introduction. We'll be very happy to answer your future questions in the community, it will be helpful if you try to make them a bit shorter so that contributors to the community (Spotfire employees, Spotfire partners, Spotfire users) can answer them within a reasonable amount of time. Here are some tips for asking questions. Dr Spotfire also monitors the community in case some topics can benefit from being covered in a Dr Spotfire session. Keep an eye out on the events page for Dr Spotfire topics that may be of use for you (longer webinars, free training)

 

Kind regards,

Heleen Snelting

Ok, thank you. 

In my opnion having consistent colors through a document should not be so complicated, to the point of requiring scripts and support from Spotfire partners. 

Apparently the Treemap has an inconsistent behaviour comparing to all other visualizations, maybe a bug.

Link to comment
Share on other sites

Hi Marcos,

If you think there is a bug, we do want to investigate this and the way to achieve this is to log a support ticket. That process requires you to submit some details which enable us to replicate the issue and investigate fully. Would you be so kind to submit this here: https://support.tibco.com/s/ ? 

Additionally, as promised, our Dr Spotfire team is looking at your question and may have some additional questions. 

Thank you!

Heleen

Link to comment
Share on other sites

Hello  Marcos, 

Are you trying to set the same color scheme for all visuals based on  column selection from a dropdown property?

You could do this without any scripting by just having the color by driven by the selector. Please note that the Treemap visualization handles the color by a bit different because the hierarchy, so you will need to use a unique concatenate or other type of aggregation for your purpose for both, the hierarchy and the color by.

colorBy.thumb.gif.73564e947354a99cf597121682ed9349.gif


Just right click on the Color by selector for each visualization and select "set from property.." option. 

If you want to change the default color scheme, you can create your own or select one from the color scheme properties. For example, you can select the Green Categorical and save it as a document color scheme, for example "greens" and then go to your Colum Properties and set the default color scheme to be "greens"

image.thumb.png.f0a16787f0d42a57db48516d09f5a0d4.png

image.png.50257f842f2975b2060e3e43cca8f10d.png
 

 

colorByGreens.thumb.gif.06db73c5d75d6b8fb5a23d0895960c32.gif

Iron Python is often used to automate thing, so if you can do it by hand, you can do it programmatically in most cases. By the way, I noticed however, that the  if hasattr(visual, "ColorAxis"): part of your code always returns false, so the rest of your code is never executed. I don't know if you still need to use script to perform this out of the box functionality.

Cheers, 

Jose

Edited by Jose Leviaguirre
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...