[英]How to format a bokeh DataTable cell based on another column value?
[英]Bokeh DataTable color cells based on different range for each cell
我有一個具有這種格式的散景數據表,顯示了 3 個不同 QC 樣品(低濃度、中濃度和高濃度)測量的濃度。
Unnamed: 0 run 9Be 45Sc 51V 52Cr 55Mn........
QC Low Mean 11/14/19 1.16 0.845 1.2 2.5 9.6
QC Med Mean 11/14/19 2.37 0.865 2.0 5.6 10.0
QC Hi Mean 11/14/19 15.28 0.894 12.7 32.6 23.9
這些值中的每一個都有一個可接受的范圍。 范圍取決於樣品(低、中、高)和元素。 如果它們超出該范圍,我想將單元格着色為紅色。
例如:
if QC Low 9Be was < 1.0 or >1.4 the cell would be colored red.
if QC Med 9Be was <2.2 or >2.7 the cell would be colored red
if QC Hi 9Be was <14.5 or >16.9 the cell would be red
if QC Low 51V was <0.9 or >1.4 the cell would be red
etc
我將所有這些范圍作為單獨的列存儲在 columndata 源中(例如 Min9Be 和 Max9Be 等)
我知道你可以設置一個模板並使用 htmlformatter 來格式化與此類似的單元格
template="""
<div style="background:<%=
(function colorfromint(){
if(some logic ){
return("red")}
}()) %>;
color: black">
<%= value %>
</div>
"""
formatter = HTMLTemplateFormatter(template=template)
但據我所見,這僅在比較整個列時才有效? 還是只對整列應用一個規則?
是否可以逐個單元進行? 有沒有辦法實現我想要的?
這是可能的。 AFAIK 有兩種方法可以在 Underscore.js 模板中實現這一點:
from bokeh.io import show
from bokeh.models import DataTable, TableColumn, ColumnDataSource, HTMLTemplateFormatter
ds = ColumnDataSource(dict(x=list(range(10))))
template_it = """\
<div style="background: <%
if (value % 2) {
%>red<%
} else {
%>green<%
} %>; color: black;">
<%- value %>
</div>
"""
template_js = """\
<%
var bc;
if (value < 3) bc = 'red';
else if (value < 6) bc = 'green';
else bc = 'blue';
%>
<div style="background: <%- bc %>; color: black;">
<%- value %>
</div>
"""
dt = DataTable(columns=[TableColumn(field='x', name='X with inline template',
formatter=HTMLTemplateFormatter(template=template_it)),
TableColumn(field='x', name='X with a JS block',
formatter=HTMLTemplateFormatter(template=template_js))],
source=ds)
show(dt)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.