如何为散景数据表中的行和/或单元格着色? [英] How to color rows and/or cells in a Bokeh DataTable?
本文介绍了如何为散景数据表中的行和/或单元格着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从Initial table
开始,我需要突出显示(color)元素,如表示例Ex. 1, Ex. 2, Ex. 3
之一所示.
Starting from Initial table
, I need to highlight(color) elements as shown in either one of the table examples Ex. 1, Ex. 2, Ex. 3
.
有什么主意吗?
推荐答案
如果其他人可能遇到同样的需求,这里有一些我想到的变体. (感谢Bokeh团队的提示!)
In case someone else might bump into the same need, here are some variants that I came up with. (Thanks to Bokeh team for hints!)
变量1:突出显示 A列> B列
Variant 1: Highlight the cell where column A > column B
代码:
from bokeh.io import output_notebook, show
output_notebook()
from random import randint
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn, HTMLTemplateFormatter
output_file("data_table.html")
data = dict(
cola=[randint(0, 100) for i in range(10)],
colb=[randint(0, 100) for i in range(10)],
)
source = ColumnDataSource(data)
template="""
<div style="background:<%=
(function colorfromint(){
if(cola > colb ){
return("green")}
}()) %>;
color: black">
<%= value %>
</div>
"""
formatter = HTMLTemplateFormatter(template=template)
columns = [TableColumn(field="cola", title="CL1", width = 100),
TableColumn(field='colb', title='CL2', formatter=formatter, width = 100)]
data_table = DataTable(source=source,
columns=columns,
fit_columns=True,
selectable = True,
sortable = True,
width=400,height=400)
show(widgetbox(data_table))
输出:
变量2:突出显示 A列> B列所在的单元格,并为文本着色
Variant 2: Highlight the cell where column A > column B and color the text
from bokeh.io import output_notebook, show
output_notebook()
from random import randint
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn, HTMLTemplateFormatter
output_file("data_table.html")
data = dict(
cola=[randint(0, 100) for i in range(10)],
colb=[randint(0, 100) for i in range(10)],
)
source = ColumnDataSource(data)
template="""
<div style="background:<%=
(function colorfromint(){
if(cola > colb ){
return("green")}
}()) %>;
color: <%=
(function colorfromint(){
if(cola > colb){return('yellow')}
}()) %>;">
<%= value %>
</font>
</div>
"""
formatter = HTMLTemplateFormatter(template=template)
columns = [TableColumn(field="cola", title="CL1", width = 100),
TableColumn(field='colb', title='CL2', formatter=formatter, width = 100)]
data_table = DataTable(source=source,
columns=columns,
fit_columns=True,
selectable = True,
sortable = True,
width=400,height=400)
show(widgetbox(data_table))
输出:
变体3:根据A列和B列之间的多种条件为文本着色
from bokeh.io import output_notebook, show
output_notebook()
from random import randint
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn, HTMLTemplateFormatter
output_file("data_table.html")
data = dict(
cola=[5, 6, 7, 20, 30, 40, 50, 60, 70, 80],
colb=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
)
source = ColumnDataSource(data)
template="""
<p style="color:<%=
(function colorfromint(){
if (1 < Math.abs(cola - colb) && Math.abs(cola - colb) < 10)
{return('green')}
else if (10 < Math.abs(cola - colb) && Math.abs(cola - colb) < 40)
{return('blue')}
else
{return('red')}
}()) %>;">
<%= value %>
</p>
"""
formatter = HTMLTemplateFormatter(template=template)
columns = [TableColumn(field="cola", title="CL1", width = 100),
TableColumn(field='colb', title='CL2', formatter=formatter, width = 100)
]
data_table = DataTable(source=source,
columns=columns,
fit_columns=True,
selectable = True,
sortable = True,
width=400,height=400)
show(widgetbox(data_table))
输出:
变式4:根据A列和B列之间的多个条件为文本着色.添加CL3列以突出显示条件
from bokeh.io import output_notebook, show
output_notebook()
from random import randint
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn, HTMLTemplateFormatter
output_file("data_table.html")
data = dict(
cola=[randint(0, 100) for i in range(10)],
colb=[randint(0, 100) for i in range(10)],
colc=['█' for i in range(10)]
)
source = ColumnDataSource(data)
template="""
<p style="color:<%=
(function colorfromint(){
if (1 < Math.abs(cola - colb) && Math.abs(cola - colb) < 10)
{return('green')}
else if (10 < Math.abs(cola - colb) && Math.abs(cola - colb) < 40)
{return('blue')}
else
{return('red')}
}()) %>;">
<%= value %>
</p>
"""
formatter = HTMLTemplateFormatter(template=template)
columns = [TableColumn(field="cola", title="CL1", width = 100),
TableColumn(field='colb', title='CL2', formatter=formatter, width = 100),
TableColumn(field='colc', title='CL3', formatter=formatter, width = 5)
]
data_table = DataTable(source=source,
columns=columns,
fit_columns=True,
selectable = True,
sortable = True,
width=400,height=400)
show(widgetbox(data_table))
输出:
这篇关于如何为散景数据表中的行和/或单元格着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文