如何为画布外的散景图生成动态描述文本?

问题描述 投票:0回答:1

我有一个散景图,其中包含多个字形和一个javascript回调函数,该函数根据鼠标在图形中的停留位置融合到其他字形中。

现在,我要根据鼠标在左侧图中的徘徊(或单击)的位置,在该图的右侧添加带有其他信息的描述。

如何为画布外的散景图生成动态变化的描述文本?

[我的想法是编写一个Java脚本回调函数,该函数可在绘图外更改div的文本,但我不确定这是否可行或如何实现。

可以使用悬停工具,但是我的描述过长,无法显示为覆盖框。

这里是一些我想动态更改的div示例代码:

from bokeh.models import ColumnDataSource, CustomJS, HoverTool, Div, Spacer
from bokeh.plotting import figure, output_file, show
from bokeh.layouts import row

output_file("hover_callback.html")

# define some points and a little graph between them
x = [2, 3, 5, 6, 8, 7]
y = [6, 4, 3, 8, 7, 5]
links = {
    0: [1, 2],
    1: [0, 3, 4],
    2: [0, 5],
    3: [1, 4],
    4: [1, 3],
    5: [2, 3, 4]
}

p = figure(plot_width=400, plot_height=400, tools="", toolbar_location=None, title='Hover over points')

source = ColumnDataSource({'x0': [], 'y0': [], 'x1': [], 'y1': []})
sr = p.segment(x0='x0', y0='y0', x1='x1', y1='y1', color='olive', alpha=0.6, line_width=3, source=source, )
cr = p.circle(x, y, color='olive', size=30, alpha=0.4, hover_color='olive', hover_alpha=1.0)

# Add a hover tool, that sets the link data for a hovered circle
code = """
const links = %s
const data = {'x0': [], 'y0': [], 'x1': [], 'y1': []}
const indices = cb_data.index.indices

console.log(cb_data.index.indices)

for (var i = 0; i < indices.length; i++) {
    const start = indices[i]
    for (var j = 0; j < links[start].length; j++) {
        const end = links[start][j]
        data['x0'].push(circle.data.x[start])
        data['y0'].push(circle.data.y[start])
        data['x1'].push(circle.data.x[end])
        data['y1'].push(circle.data.y[end])
    }
}
segment.data = data
""" % links

callback = CustomJS(args={'circle': cr.data_source, 'segment': sr.data_source}, code=code)
p.add_tools(HoverTool(tooltips=None, callback=callback, renderers=[cr]))


div = Div(text="""<br>
Here is were I want to display some additional information about the point that is currently hovered over.""",
width=200, height=100)

show(row(p,Spacer(width=20), div))
javascript python html bokeh interactive
1个回答
0
投票

查看此示例,它确实满足您的要求:https://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html?highlight=event#customjs-for-user-interaction-events

简单来说,您可以使用Div模型并将其text属性更改为所需的任何内容。

© www.soinside.com 2019 - 2024. All rights reserved.