我使用 django 制作了一个散景条形图和一个来自 pandas 数据框的数据表,它们在网页上彼此相邻显示。我想单击条形图并根据条形图的标题更改显示的数据表行。
我在下面举了一个例子。根据示例,我想单击条形图上的条并检索条的名称(水果标签之一)。然后,如果条形图名称与列中的字符串相同,我想过滤数据框(仅在示例中列出)。例如:如果我单击名为“apple”的条形图栏,则应显示“word”列值等于 apple 的数据框行。
我一直在尝试使用 js_on_event 和 js_on_change 执行此操作,但无法读取任何内容。有没有办法做到这一点?或者甚至只是为了在点击时阅读列的名称?
def indexPage(request):
fruits = ["apple", "banana","carrot","daikon","eggplant","fig","grape"]
count = [1,2,3,4,5,6,7]
data = {'fruits' : fruits,
'count' : count}
source = ColumnDataSource(data=data)
p = figure(y_range=fruits, sizing_mode='scale_width',toolbar_location='right',tools=['tap', 'reset'])
p.hbar(y='fruits', right="count",height=0.9,selection_color='deepskyblue', nonselection_color='lightgray',nonselection_alpha=0.3,source=source)
hover = HoverTool(tooltips=[("word","@fruits"),("count","@count")])
p.add_tools(hover)
script, div = components(p)
########################################################################################################
text = ["apples r red", "banana is yelo","carrt is loong","daikon is long","eggplants r","figs are the food","grape is red"]
word = ["apple", "banana","carrot","daikon","eggplant","fig","grape"]
data1 = {'text' : text}
source = ColumnDataSource(data=data1)
dt = DataTable(columns=[TableColumn(field='text', title='text')],source=source)
script1, div1 = components(dt)
return render(request, "index.html", {'script':script, 'div':div, "script1":script1,"div1":div1})