Jump to content

I want to apply the same shape scheme of a scatter plot to several other scatter plots using an IronPython script.


msctomat

Recommended Posts

Hi

I want to apply the same shape scheme of a scatter plot to several other scatter plots using an IronPython script.

I plan to implement this by creating a button in a text area and running the script.

I've applied the color scheme to another chart, but I don't know how to apply the shape scheme.

Please help me.

 

===================================================

from Spotfire.Dxp.Application.Visuals import *

ref_chart = 'ref'

for v in Application.Document.ActivePageReference.Visuals:

    if v.Title == ref_chart:

        vis = v.As[VisualContent]()

        color_by = vis.ColorAxis.Expression

        color_sc = vis.ColorAxis.Coloring

        sh = vis.ShapeAxis.Expression

        break

 

for v in Application.Document.ActivePageReference.Visuals:

    vis = v.As[VisualContent]()

    if vis is not None and vis .Title != ref_chart:

        vis.ColorAxis.Expression = color_by

        vis.ColorAxis.Coloring.Apply(color_sc)

        vis.DrawingOrderAxis.Expression = color_by

        vis.ShapeAxis.Expression = sh

Link to comment
Share on other sites

Hi,

I don't know if you have a lot of scatterplot visualizations, but if it is 'oversee-able' you could try this code:
 

from Spotfire.Dxp.Application.Visuals import *
#add script parameter splot referring to the scatterplot

splot1 = splot1.As[ScatterPlot]()
splot2 = splot2.As[ScatterPlot]()

splot1.ShapeAxis.DefaultShape = MarkerShape(MarkerType.TriangleUp)
splot2.ShapeAxis.DefaultShape = MarkerShape(MarkerType.Cross)

Each scatterplot is declared and then targeted for a markershape. In the example above, I used different shapes, but in your case, the shape would be the same.

The reason I used different shapes, is to show that the script works (and what script parameters you have to set):
image.thumb.png.5733aa9daf1604db44353937952a4712.png
 

In your use case, all the shapes in the code would be the same, meaning that these lines in the code is repeated for each scatterplot:
splot1.ShapeAxis.DefaultShape = MarkerShape(MarkerType.TriangleUp)
splot2.ShapeAxis.DefaultShape = MarkerShape(MarkerType.TriangleUp)

Kind regards,

David

 

Link to comment
Share on other sites

Thank you for your response.

The number of other charts to which I want to apply the shape scheme of the reference chart is about 7 to 10.

The shape expression is set as a categorical column and can be as many as 10.

 

 

image.thumb.png.3f5cdd53c8576c1e71d86e8c7bd43492.png

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

image.thumb.png.0bf4f181e8cbe500940a7576d568a33d.png

 

 

I think it might work to use an API function that applies the shape scheme of the reference chart, stored as a variable, to the shape schemes of other charts.

Is there such a thing?

image.png.b28d2dbe5ecdddec0697c37034f9f2a2.png

 

 

 

 

Link to comment
Share on other sites

Hi msctomat,

Please find the script below that sets all scatterplot visualizations to a pre-defined shape scheme.
You need to define the shape scheme once in the iron python code (this is located in the MAP SHAPE TO VALUES part at the end of the script), and then it can be applied to all the scatterplots with the click of a button.

from Spotfire.Dxp.Application.Visuals import *
from Spotfire.Dxp.Data import IndexSet,DataValueCursor,DataType

#####################################################################
# Save all visuals of type Scatterplot in all_scatterplots
all_scatterplots = []
for page in Document.Pages:    
	for vis in page.Visuals:
		if str(vis.TypeId) == "TypeIdentifier:Spotfire.ScatterPlot":
			vis = vis.As[ScatterPlot]()
			all_scatterplots.append(vis)

#####################################################################
# Find all possible values of the 'category' variable
table_name = 'iris'
category_name = 'category'
table = Document.Data.Tables[table_name] 
category_column =  table.Columns[category_name]
category_cursor = DataValueCursor.CreateFormatted(table.Columns[category_name])
# All rows
rowsToInclude = IndexSet (table.RowCount,True)
# Loop through all values
category_values=[]
for row in table.GetRows(rowsToInclude,category_cursor):   
  	category_values.append(category_cursor.CurrentValue)
#Find only unique values
category_values = list(set(category_values))
# In this case, category is an integer, but the values are always returned as strings.
# Ignore if the category is a string.
if category_column.DataType== DataType.Integer:
	category_values = [int(x) for x in category_values]

category_values.sort()

#####################################################################
# MAP SHAPES TO VALUES
#####################################################################
number_of_categories = len(category_values)
print ('Number of category values:',number_of_categories)
# See MarkerType Enumeration in the Spotfire API for a list of all supported shapes
shape_types = ['Circle','Square','Diamond','TriangleRight','TriangleLeft']

# For all values and shapes, associate them to the category column in all scatterplots
for value, shape in zip(category_values,shape_types):
	#print (value, shape)
	shape_value = getattr(MarkerType,shape)
	for vis in all_scatterplots:
		vis.ShapeAxis.ShapeMap[value] = MarkerShape(shape_value)
	

I have also attached an example dxp for your convenience.

Kind regards,

David

test.dxp

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...