透過給定的 RGB 值設定行顏色您好,我正在嘗試透過列中給定的 RGB 值來設定行顏色(不是單元格顏色)。但是,我無法獲得正確的循環來分配我想要的自訂顏色。有人可以幫忙嗎?
附代碼和數據表。謝謝!
// Get the current data table
dt = Current Data Table();
// Initialize the color lists
RedList = Column(dt, "Red") << Get Values;
GreenList = Column(dt, "Green") << Get Values;
BlueList = Column(dt, "Blue") << Get Values;
// Ensure we do not exceed the number of rows or the number of items in any color list
minLength = Min( N Rows(dt), N Items(RedList), N Items(GreenList), N Items(BlueList) );
// Loop to set the row colors
For( i = 1, i <= minLength, i++,
// Clear any previous selections
dt << Clear Select;
// Select the current row
dt << Select Where( Row() == i );
// Set the row color
dt << Set Row Colors( RedList[i], GreenList[i], BlueList[i] );
);
自動化和腳本編寫回覆:透過給定的 RGB 值設定行顏色由於所有選擇都會發生,這會有點慢
Names Default To Here(1);
dt = Current Data Table();
dt << show window(0);
For Each Row(dt,
dt << Clear Select;
dt << Select Rows(Row());
dt << Colors(RGB Color(Eval List({:Red, :Green, :Blue}) / 255));
);
dt << show window(1);
如果需要更快的速度,可以透過選擇多行(同時具有特定顏色的所有行)或設定行狀態來加快速度。
Names Default To Here(1);
dt = Current Data Table();
Summarize(dt, uniq = By(:Color), r = Min(:Red), g = Min(:Green), b = Min(:Blue));
For Each({cur_color, idx}, uniq,
dt << Select Where(:Color == cur_color);
dt << Colors(RGB Color(Eval List({r[idx], g[idx], b[idx]}) / 255));
dt << Clear Select;
);

漂亮的圖片!
編輯:
我突然想到您還可以使用匯總表來設定顏色
Names Default To Here(1);
dt = Current Data Table();
dt_summary = dt << Summary(
Group(:Color),
Mean(:Red),
Mean(:Green),
Mean(:Blue),
Freq("None"),
Weight("None"),
statistics column name format("column")
);
For Each Row(dt_summary,
dt_summary << Select Rows(Row());
dt_summary << Colors(RGB Color(Eval List({:Red, :Green, :Blue}) / 255));
dt_summary << Clear Select;
);
Close(dt_summary, no save);
Edit2:還有一個選項循環整個表格但沒有選擇
Names Default To Here(1);
dt = Current Data Table();
For Each Row(dt,
Row State(Row()) = Color State(Eval List({:Red, :Green, :Blue} / 255));
);
我不知道如何將顏色狀態(例如顏色狀態(-15527920))轉換為數字(應該是512.925536155701),所以我可以建立行狀態矩陣並使用<<設定行狀態。
回覆:透過給定的 RGB 值設定行顏色謝謝!這些解決方案確實有效且有效率!
回覆:透過給定的 RGB 值設定行顏色2 件事
- 設定行狀態顏色的語法不正確。
- 使用 SelectWhere() 會使迴圈變得非常慢。
這是我的修改
// Get the current data table
dt = Current Data Table();
// Initialize the color lists
RedList = Column(dt, "Red") << Get Values;
GreenList = Column(dt, "Green") << Get Values;
BlueList = Column(dt, "Blue") << Get Values;
// Ensure we do not exceed the number of rows or the number of items in any color list
minLength = Min( N Rows(dt), N Items(RedList), N Items(GreenList), N Items(BlueList) );
// Loop to set the row colors
For( i = 1, i <= minLength, i++,
// Clear any previous selections
dt << Clear Select;
// Select the current row
dt << Select rows( i );
// Set the row color
dt << Colors( rgb color(redlist[i],greenlist[i],bluelist[i]) );
);回覆:根據給定的 RGB 值設定行顏色抱歉翻出這麼老的帖子,但我剛才費了一番功夫才弄清楚。我之前也嘗試過把顏色狀態轉換成數字,但沒成功。後來我找到了一種更快的方法,因為它避免了循環。雖然感覺有點作弊,但創建一個公式列,然後讓 JMP 使用內建函數複製這些行狀態,速度比@jthi之前用 Edit2 實現的快得多。
(dt << new column ("Color", formula (color state (RGB color (:R, :G, :B))))) << copy to row states;