ironpython - 如何使用 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
解决方案
我认为解决方案是创建一个数据透视表,将选定的列作为行标识符。这将为您提供独特的价值。
使用这些作为起点:
https://community.tibco.com/questions/remove-duplicate-rows-table-visualization
试试这个脚本(根据您的假设,有问题的表格是默认表格,表格视觉对象的默认标题是表格名称):
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])
推荐阅读
- python-3.x - 这个 python-click 行为正确吗?
- c++ - 如何集成sqlite3 C++接口
- django - Django 日志记录无法在所有级别使用有效配置?
- python - 如何为每个词性标签创建一个 pandas 数据框列?
- mysql - 如何将 SQL 查询结果转换/复制为 JSON?
- python - 如何在 url 上传递当前登录用户的用户名?python django
- npm - npm:未处理的拒绝错误:EISDIR:对目录的非法操作,打开'/Users/me/.npm/_cacache/content-v2/sha512/b3/da'
- java - Java 8:如何从包含为映射值的列表中获取值?
- android - 如何将自定义视图添加到 android 键盘
- reactjs - 对于扩展 React-dev 工具,如何水平对齐 chrome-dev-menu 中的选项卡,而不是垂直对齐?