首页 > 解决方案 > 如何使用 Iron python 在 Spotfire 中创建多选(动态选择列)仪表板

问题描述

我使用下面的代码创建了一个动态选择仪表板,但这给了我选择单个列的重复记录。有没有办法避免重复?

代码:

from Spotfire.Dxp.Application.Visuals import TablePlot, VisualContent
from Spotfire.Dxp.Data import DataPropertyClass

#multiselect doc prop control
multiSelectColumnsPropertyControl = "select" 

#reference to the data table
dataTable = Document.Data.Tables.DefaultTableReference   

# 1 Create a table if not there already
tablePlotVisual = None
updateTablePlot = False
defaultTableTitle=dataTable.Name

# 1.1 check if tablePlot already exist (based on name)
for v in Document.ActivePageReference.Visuals: 
   tablePlotVisual = v
   updateTablePlot = v.Title == defaultTableTitle
   if v.Title == defaultTableTitle: break

# 2.1 If no tablePlot found, create a new one
if not updateTablePlot:
    tablePlotVisual = Application.Document.ActivePageReference.Visuals.AddNew[TablePlot]()
    tablePlotVisual.Data.DataTableReference = dataTable
    tablePlotVisual.Title = "${DataTable.DisplayName}"
    tablePlotVisual.Legend.Visible= False
else:
    tablePlotVisual=tablePlotVisual.As[VisualContent]()

# 2 Adds columns to a Data Table based on a document property. 

# 2.1 Remove all columns
cols = tablePlotVisual.Data.DataTableReference.Columns
tablePlotVisual.TableColumns.Clear()

# 2.2 get document property
selection = Document.Data.Properties.GetProperty(DataPropertyClass.Document, multiSelectColumnsPropertyControl).Value

# 2.3 Parse columns from selection and add to tablePlotVisual
for property in selection:
   for col in property.split(","): tablePlotVisual.TableColumns.Add(cols[str(col)])

例如: 输入:

empid  empname emp_loc  dept
101    aaa     chennai  IT
102    bbb     Delhi    HR
103    ccc     Delhi    HR
104    ddd     Mumbai   IT

输出:如果选择了 emp_loc 和 dept,则代码给出

emp_loc dept
Chennai IT
Delhi   HR
Delhi   HR
Mumbai  IT

预期输出:但我需要,

emp_loc  dept
Chennai  IT
Delhi    HR
Mumbai   IT

标签: ironpythonspotfire

解决方案


我认为解决方案是创建一个数据透视表,将选定的列作为行标识符。这将为您提供独特的价值。

使用这些作为起点:

https://community.tibco.com/questions/remove-duplicate-rows-table-visualization

https://community.tibco.com/wiki/how-add-data-table-pivot-transformation-using-ironpython-tibco-spotfire

试试这个脚本(根据您的假设,有问题的表格是默认表格,表格视觉对象的默认标题是表格名称):

from Spotfire.Dxp.Application.Visuals import TablePlot, VisualContent
from Spotfire.Dxp.Data import DataPropertyClass
from Spotfire.Dxp.Data import DataFlowBuilder, DataColumnSignature, DataType, DataSourcePromptMode
from Spotfire.Dxp.Data.Transformations import PivotTransformation
from System.Collections.Generic import List
from Spotfire.Dxp.Data.Import import DataTableDataSource

#NEW function
#if table with name tablename exists, return it, otherwise return None
def findTable(tablename):
    try:
        return Document.Data.Tables[tablename]
    except:
        return None


#multiselect doc prop control
multiSelectColumnsPropertyControl = "select" 
#get document property
selection = Document.Data.Properties.GetProperty(DataPropertyClass.Document, multiSelectColumnsPropertyControl).Value


#reference to the data table
dataTable = Document.Data.Tables.DefaultTableReference  
defaultTableTitle=dataTable.Name
 
#NEW:pivot table name
pivotTableName ='newpivot'

#NEW: create or update a pivot table using as identifiers the selected columns 
ds = DataTableDataSource(dataTable)
ds.IsPromptingAllowed = False
ds.ReuseSettingsWithoutPrompting = True
dfb = DataFlowBuilder(ds, Application.ImportContext)

pivot = PivotTransformation()
list = List[DataColumnSignature]()
list.Clear()

#add selected columns as identifiers
for property in selection:  
    col = dataTable.Columns[property]
    list.Add(DataColumnSignature(col))
pivot.IdentityColumns = list

dfb.AddTransformation(pivot)
pivotContent = dfb.Build()

#if pivot table exists, replace its content, otherwise create a new one
pivotHandle = findTable(pivotTableName)
if (pivotHandle is not None):
    pivotHandle.ReplaceData(pivotContent) #update
else:
    Document.Data.Tables.Add(pivotTableName,pivotContent) #create
    pivotHandle = findTable(pivotTableName) #refresh handle
    

    
#create a table VISUAL if not there already
tablePlotVisual = None

#check if tablePlot already exist (based on name)
for v in Document.ActivePageReference.Visuals: 
   if v.Title == defaultTableTitle:
        tablePlotVisual = v
        break

#if no tablePlot found, create a new one
if tablePlotVisual is None:
    tablePlotVisual = Application.Document.ActivePageReference.Visuals.AddNew[TablePlot]()
    tablePlotVisual.Data.DataTableReference = pivotHandle  #content of pivot table
    tablePlotVisual.Title = defaultTableTitle #title of original table
    tablePlotVisual.Legend.Visible= False
else:
    tablePlotVisual=tablePlotVisual.As[VisualContent]()

#make sure to add all selected columns to the visual
tablePlotVisual.TableColumns.Clear()
for property in selection:
    tablePlotVisual.TableColumns.Add(pivotHandle.Columns.Item[property])

推荐阅读