Jump to content

Did anyone know how to reset or change the color in line and bar charts ?


Sorani Dejsook

Recommended Posts

Hi Spotfire expert,

I have the question on the Categorical color mode.

Currently, I'm facing 2 issue below.

  1. The color is too light. Then sometime it is not easy to see in the white theme.
  2. The color values which display on the chart are looking very similar. Example in the image belowspotfire_chart_color.png.6ba2d2e45471a6c58eddf3c597e081b5.png

I know that we can fix the color per value by IronPython. But that way won't be the good way to do when we have 50 unique values in the column.

Also, I tried to reset the color by using IronPython (Below code) but it's only working one time. If you try click the button to run this script again, color won't change.

from Spotfire.Dxp.Application.Visuals import Visualization,VisualTypeIdentifiersfor page in Document.Pages: for viz in page.Visuals: if viz.TypeId == VisualTypeIdentifiers.BarChart: viz=viz.As[Visualization]() viz.ColorAxis.Clear() if viz.TypeId == VisualTypeIdentifiers.LineChart: viz=viz.As[Visualization]() viz.ColorAxis.Clear()

So anybody know how to solve this issue ? Please advice.😊

Note : Spotfire version 12.0.4.48

Thank you,

Sorani D.

Link to comment
Share on other sites

Hi @Gaia Paolini​, thank you for your help. I tried to add the statement you're mentioned.

from Spotfire.Dxp.Application.Visuals import Visualization,VisualTypeIdentifiersfor page in Document.Pages: for viz in page.Visuals: if viz.TypeId == VisualTypeIdentifiers.BarChart: viz=viz.As[Visualization]() viz.ColorAxis.Coloring.Clear() if viz.TypeId == VisualTypeIdentifiers.LineChart: viz=viz.As[Visualization]() viz.ColorAxis.Coloring.Clear()

Seems this script change the Color Mode from Categorical to Fixed.

Do you have any idea how to change/ reset the color in Categorical Color Mode ?spotfire_chart_color_w_ironpython.thumb.png.9c462fa6d0532f80b9ce1e76cde44492.png 

Thank you,

Sorani D.

Link to comment
Share on other sites

Yes it resets it to the default, which is a fixed colour.

If you want to distinguish 50 categories by some slowly changing colour, I think you need to create your own colour scheme. There are websites that let you create a colour palette. Maybe some colours ranging from blue to green to red? Once done, you then save these colours in a simple text file. They will probably be hexadecimal values.

Then you need to create a new colour scheme in Spotfire. Spotfire has its own colour scheme format (you can see it when you export a colour scheme to file) and does not directly import colours from a text file. I tried to experiment with a workaround, which worked for me.

You have a first Iron Python script in which you import the colours from file, and you create a document colour scheme from them:

from System.Drawing import ColorTranslatorfrom System.IO import StreamReader  # Read list of colours from csv file as a stringcol_filename='C:/blahblah/mycols.csv'streamReader=StreamReader(col_filename)col_data = streamReader.ReadToEnd() # Split the string to create a listcol_data=col_data.split('rn') # Create Color objects from hexadecimal stringscol_data = [ColorTranslator.FromHtml(hex) for hex in col_data] ### Add a new color scheme to the document# First clear (if you want) all document colour schemes Document.ColoringTemplates.Clear() # Then add a new colour schemecolouring_scheme_name='MyColoursFromFile'coloring = Document.ColoringTemplates.AddNew(colouring_scheme_name)map = coloring.AddCategoricalColorRule()map.SetColors(col_data)

This script reads from a file called 'C:/blahblah/mycols.csv' (insert your own file path here). An example is attached.

This is read as a string which is then split into a list. Since Spotfire needs to translate from hexadecimal strings to colour objects, I apply the function ColorTranslator.FromHtml() to the colour list.

Then the list is saved as a document colour scheme named 'MyColoursFromFile' (change to whatever name you want).

Now you have a new colour scheme. In a second script you load it and apply it to your barcharts and linecharts as you prefer:

from Spotfire.Dxp.Application.Visuals import * coloring = Document.ColoringTemplates["MyColoursFromFile"] for page in Document.Pages: for viz in page.Visuals: if viz.TypeId == VisualTypeIdentifiers.BarChart: viz=viz.As[Visualization]() #viz.ColorAxis.Coloring.Clear() viz.ColorAxis.Coloring.Apply(coloring) if viz.TypeId == VisualTypeIdentifiers.LineChart: viz=viz.As[Visualization]() #viz.ColorAxis.Coloring.Clear() viz.ColorAxis.Coloring.Apply(coloring)
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...