在图选择上过滤Bokeh DataTable

时间:2019-08-19 07:02:32

标签: python bokeh

我正在尝试根据图选择按dataTable进行过滤。我陷入合并cb_obj

我生成一个散点图,我想在该点上选择任何一点。选定后,我想用选定的y(或x)值大于第二个数据源(即DataTable的数据源)中的值的值填充表。

from random import random

from bokeh.layouts import row
from bokeh.models import CustomJS, ColumnDataSource, TapTool,BoxZoomTool, Column
from bokeh.plotting import figure, output_file, show
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn

output_file("callback.html")

x = [random()*10 for x in range(10)]
y = [random()*10 for y in range(10)]

s1 = ColumnDataSource(data=dict(x=x, y=y))
p1 = figure(plot_width=400, plot_height=400, title="Select Here")
p1.add_tools(TapTool(), BoxZoomTool())
p1.circle('x', 'y', source=s1, alpha=10)

x1=range(1, 10)
y1=range(1, 10)
s2 = ColumnDataSource(data=dict(x=x1, y=y1))

C1 = [
        TableColumn(field="x", title="x"),
        TableColumn(field="y", title="y"),
    ]

f_data_table = DataTable(columns=C1,source=s2)

s3 = ColumnDataSource(data=dict(x=[],y=[]))

C2 = [
        TableColumn(field="x", title="x"),
        TableColumn(field="y", title="y"),
    ]

data_table = DataTable(columns=C2,source=s3)

s1.selected.js_on_change('indices', CustomJS(args=dict(s1=s1, s2=s2,s3=s3), code="""
var data = s1.data;
var f = cb_obj.indices;
var d2 = s2.data;
var d3 = s3.data;
d3['x']=[]
d3['y']=[]


for(i = 0; i < d2['y'].length;i++){

if(d2['y'][i]>f['y']){

    d3['x'].push(d2['x'][i])
    d3['y'].push(d2['y'][i])
}

}

s3.change.emit()
// trigger change on datatable
data_table.change.emit()

""")
)

layout = Column(p1, data_table, f_data_table )

show(layout)

编辑:

让它正常工作

from random import random

from bokeh.layouts import row
from bokeh.models import CustomJS, ColumnDataSource, TapTool,BoxZoomTool, Column
from bokeh.plotting import figure, output_file, show
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn

output_file("callback.html")

x = [random()*10 for x in range(10)]
y = [random()*10 for y in range(10)]

s1 = ColumnDataSource(data=dict(x=x, y=y))
p1 = figure(plot_width=400, plot_height=400, title="Select Here")
p1.add_tools(TapTool(), BoxZoomTool())
p1.circle('x', 'y', source=s1, alpha=10)

x1=range(1, 10)
y1=range(1, 10)
s2 = ColumnDataSource(data=dict(x=x1, y=y1))

C1 = [
        TableColumn(field="x", title="x"),
        TableColumn(field="y", title="y"),
    ]

f_data_table = DataTable(columns=C1,source=s2)

s3 = ColumnDataSource(data=dict(x=[],y=[]))

C2 = [
        TableColumn(field="x", title="x"),
        TableColumn(field="y", title="y"),
    ]

data_table = DataTable(columns=C2,source=s3)

s1.selected.js_on_change('indices', CustomJS(args=dict(s1=s1, s2=s2,s3=s3), code="""
var data = s1.data;
var f = cb_obj.indices;
var d2 = s2.data;
var d3 = s3.data;
d3['x']=[]
d3['y']=[]

for (var j = 0; j < f.length; j++) {

for(i = 0; i < d2['y'].length;i++){

if(d2['y'][i]>data['y'][f] && d2['x'][i]>data['x'][f]){

    d3['x'].push(d2['y'][i])
    d3['y'].push(d2['y'][i])
}
}
}

s3.change.emit()
// trigger change on datatable
data_table.change.emit()

""")
)

layout = Column(p1, data_table, f_data_table )

show(layout)

0 个答案:

没有答案