Bokeh/Python:如何更改CustomJS回调中的数据列

Bokeh/Python:如何更改CustomJS回调中的数据列,python,callback,bokeh,Python,Callback,Bokeh,假设源有4列,分别命名为time\u s,time\u min,time\u h 和y source = ColumnDataSource(...) 然后从这些列中选择最初的time\u s和y p = figure(tools=tools, toolbar_location="above") p.line(x='time_s', y='y', source=source) 在CustomJS回调代码块中,我想将x列从time\u s切换到maybetime\u min,并相应地重新渲染绘

假设源有4列,分别命名为
time\u s
time\u min
time\u h
y

source = ColumnDataSource(...) 
然后从这些列中选择最初的
time\u s
y

p = figure(tools=tools, toolbar_location="above")
p.line(x='time_s', y='y', source=source)
在CustomJS回调代码块中,我想将x列从
time\u s
切换到maybe
time\u min
,并相应地重新渲染绘图。我知道如何通过在DevTools控制台中分析来浏览/访问一些数据和对象,但我无法从
p.line
对象中找到
x='time'
,也无法访问它

js = """
// Explore Objects in DevTools Console
console.log(data)
console.log(source)
console.log(plot) 

data = source.data
data['time_s'] = do something to column here ...

if (widget.value === 'xyz') {
    // Looking for something like:
    plot.line.x = data['time_min'] // not working
}

// After data change -> rerender
source.change.emit()

"""
cb = CustomJS(args=dict(src=source, plot=p, widget=mywidget), code=js)
mywidget.js_on_change('value', cb)

那么如何做到这一点呢

我可以想象这样做有多种方式。假设您可以容忍数据源中有一列被复制,我建议如下:

import numpy as np

from bokeh.io import show
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, CustomJS, Select
from bokeh.plotting import figure

x = np.linspace(0, 10, 100)
foo = x**2
bar = np.sin(x)

source = ColumnDataSource(data=dict(x=x, y=foo, foo=foo, bar=bar))

plot = figure(plot_height=350)
plot.line(x='x', y='y', source=source)

select = Select(value='foo', options=['foo', 'bar'])
select.js_on_change('value', CustomJS(args=dict(source=source, select=select), code="""
    // make a shallow copy of the current data dict
    const new_data = Object.assign({}, source.data)

    // update the y column in the new data dict from the appropriate other column
    new_data.y = source.data[select.value]

    // set the new data on source, BokehJS will pick this up automatically
    source.data = new_data
"""))

show(column(plot, select))
如果不允许将所选列复制到“y”列,则可以更新图示符,以更改其指向的列。看起来是这样的:

r = p.line(x='x', y='foo' source=source)

cb = CustomJS(args=dict(r=r, select=select), code="""
    // tell the glyph which field of the source y should refer to
    r.glyph.y.field = select.value

    // manually trigger change event to re-render
    r.glyph.change.emit()
""")

我知道我可以用数学方法简单地重新计算从's'到'min'和'h'的列,但这是一个更一般的问题,thx:)谢谢@bigreddot,第二个例子正是我想要的。