javascript - 如何使用回调更改字形的颜色?

标签 javascript python callback bokeh

我尝试过使用 Bokeh,现在我想搜索一个词并更改其字形的颜色。我的代码看起来像这样:

import bokeh.plotting as bp
from bokeh.models import HoverTool, CustomJS
from bokeh.models.widgets import TextInput
from bokeh.io import vform

words = ["werner", "herbert", "klaus"]
x=[1,2,3]
y=[1,2,3]
color = ['green', 'blue', 'red']
word_input= TextInput(value="word", title="Point out a word")

source = bp.ColumnDataSource(data= dict(x=x,y=y,words=words, color='color'))
hover= HoverTool(tooltips=[("word", "@words")])

# output to static HTML file (with CDN resources)
bp.output_file("plot.html", mode="cdn")

# create a new plot with the tools above, and explicit ranges
p = bp.figure(plot_height = 600, plot_width = 800, title="word2vec", tools=[hover], logo =None)
# add a circle renderer with vectorized colors and sizes
p.circle('x','y', radius= 0.1, color = color, source=source, line_color=None)
callback= CustomJS(args=dict(source=source), code ="""
    var data = source.get('data');
    var glyph = cb_obj.get('value')
    words = data['words']
    colors=data['color']
    for (i=0; i< words.length;i++){
        if(glyph==words[i]){colors[i]='yellow'}        
    }
    source.trigger('change');
""")
layout = vform(word_input, p)
# show the results
bp.show(layout)

这段代码行不通,我不明白为什么不行。

我做错了什么?我已经发布了 an other question那天早些时候,这是解决它的第一步。

最佳答案

你有几个问题:

  • 您创建了 CallbackJS 但从未将其设置为 TextInput

    的回调属性
  • 您将数据字典的 color 键设置为字符串 "color" 而不是颜色列表

  • 您将实际的颜色列表作为 color 参数传递给 figure(它应该是您要使用的数据源列的字符串名称,例如 “颜色”)


这是一个有效的版本:

from bokeh.io import vform
from bokeh.models import HoverTool, CustomJS
from bokeh.models.widgets import TextInput
from bokeh.plotting import output_file, figure, show, ColumnDataSource

output_file("plot.html")

words = ["werner", "herbert", "klaus"]
x, y = [1,2,3], [1,2,3]
color = ['green', 'blue', 'red']

source = ColumnDataSource(data=dict(x=x, y=y, words=words, color=color))

hover = HoverTool(tooltips=[("word", "@words")])

p = figure(plot_height=600, plot_width=800, title="word2vec", tools=[hover])
p.circle('x','y', radius=0.1, fill_color='color', source=source, line_color=None)

callback = CustomJS(args=dict(source=source), code="""
    var data = source.get('data')
    var value = cb_obj.get('value')
    var words = data['words']
    for (i=0; i < words.length; i++) {
        if ( words[i]==value ) { data.color[i]='yellow' }
    }
    source.trigger('change')
""")

word_input = TextInput(value="word", title="Point out a word", callback=callback)

layout = vform(word_input, p)

show(layout)

关于javascript - 如何使用回调更改字形的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37059055/

相关文章:

python - 额外的 python 库无法在 macOS 上运行

Python 等效于 Curl HTTP post

python - 通过 matplotlib 线图绘制一组多项式趋势线

c++ - MFC C++ 在回调函数中编辑 Windows 控件

javascript - 为什么返回时会触发点击事件?

javascript - PlatformLocation 的位置依赖性错误

javascript - 将音频和视频轨道组合到新的 MediaStream 中

Javascript 音频对象 onload 事件

event-handling - Node.js 重复监听器问题

android - 设备解锁时的应用程序回调