You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Python脚本程序化修改Spotfire交叉表的颜色规则

Modify Column-Based Color Rules in Spotfire via Python Script

Got it, let's walk through how to update your "contains" color rule for the "construction" keyword to red using Python in Spotfire. Here's a practical, step-by-step solution:

Step 1: Target Your Cross Table Visualization

First, you need to reference the specific cross table where your color rule is applied. Replace YourCrossTableName with the actual name of your visualization:

from Spotfire.Dxp.Application.Visuals import CrossTablePlot
from Spotfire.Dxp.Application.Visuals import ColorRule

# Get the cross table by its name
cross_table = Application.Document.ActivePageReference.Visuals.TryGetValue("YourCrossTableName")
if cross_table is None:
    raise ValueError("Cross table not found! Double-check the visualization name in your Spotfire document.")

Step 2: Locate the Target Color Rule

Next, iterate through the color rules attached to your target column, and find the one that checks for the "construction" keyword using the "Contains" operator:

# Access color rules for your target column (replace `YourColumnName` with the column's name)
color_rules = cross_table.ColorAxis.Rules

target_rule = None
for rule in color_rules:
    # Filter for categorical "Contains" rules targeting "construction"
    if isinstance(rule, ColorRule) and rule.Filter.Operator == Spotfire.Dxp.Data.Filtering.ComparisonOperator.Contains:
        if rule.Filter.Value.ToString() == "construction":
            target_rule = rule
            break

if target_rule is None:
    raise ValueError("Couldn't locate the color rule for 'construction'! Verify the rule's operator and target value match your setup.")

Step 3: Update the Rule's Color

Once you've found the target rule, set its color to red. You can use either a named color or custom RGB values:

# Option 1: Use a standard named color (Spotfire recognizes most common color names)
target_rule.Color = System.Drawing.Color.Red

# Option 2: Use custom RGB values (example: deep red)
# target_rule.Color = System.Drawing.Color.FromArgb(220, 20, 60)

Step 4: Apply the Changes

Finalize the update by refreshing the visualization to reflect the new color rule:

cross_table.ColorAxis.UpdateRules(color_rules)

Quick Notes:

  • Double-check YourCrossTableName and YourColumnName match exactly what's in your Spotfire document—typos will break the script.
  • If your color rule has nested conditions or a different structure, adjust the rule-checking logic to match your specific configuration.
  • Test the script in Spotfire's Python console first to catch any naming or rule-matching issues before deploying it widely.

内容的提问来源于stack exchange,提问作者frq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 14:08:12