当选择新的下拉值时,如何清除回调中添加到我的散景图中的字形?

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

我创建了链接在一起的并排图形。单击第一个图形上的数据点时,第二个图形上包含该值的条将突出显示。因为散景不具有仅突出显示一个选定条的功能,所以我使用的解决方案是创建另一种颜色的四字形,然后将其添加到第二张图形中。示例如下所示。Datapoint clicked on bsps graph to create highlighted glyph->New "Questionnaire" dropdown value clicked, highlighted glyph remains

def callback(attr, old, new):
    highlight_data = []
    # changes month dropdown values based on questionnaire value
    select_month.options = qm[q_select.value]
    #changes patient dropdown values based on questionnaire chosen
    p_select.options = opts[q_select.value]
    # changes visualizations 1 and 2 data
    sample_source.data = ColumnDataSource.from_df(scores.loc[(scores.record_id == p_select.value) & (scores.questionnaire == q_select.value)])
    sample_missing_source.data=ColumnDataSource.from_df(miss.loc[(miss.record_id == p_select.value) & (miss.questionnaire == q_select.value)]) #set this ID equal to initial value of selector

    if '.csv' in new:
        highlight_data = []
        select_month.value = str(0)
        source_t.data = ColumnDataSource.from_df(total_df.loc[(total_df.questionnaire == q_select.value) & (total_df.month == 0)])
    else:
        source_t.data = ColumnDataSource.from_df(total_df.loc[(total_df.questionnaire == q_select.value) & (total_df.month == int(select_month.value))])
    # plot visualization 1 based on new data
    p.title.text='Patient: '+p_select.value+' Diagnosis: '+diagnosis[diagnosis['record_id']==p_select.value]['baseline_dsmiv'].to_string(index=False) + ' Questionnaire: '+q_select.value
    legend_it=[]

    score_vars=[var for var in np.unique(sample_source.data['variable'])]
    missing_vars=[var for var in  np.unique(sample_missing_source.data['variable'])]

def tap_callback(event):
    highlight_data = []
    selected = sample_missing_source.selected.indices
    idx = selected[0]
    row = {c: v[idx] for c, v in sample_missing_source.data.items()}
    select_month.value = str(row['month'])
    source_t.data = ColumnDataSource.from_df(total_df.loc[(total_df.questionnaire == row['questionnaire']) & (total_df.month == int(row['month']))])
    p_t.title.text='Overall Patient Data for Questionnaire: '+ source_t.data['questionnaire'][0] + ", Month: " + str(source_t.data['month'][0])
    p_t.x_range.end = int(source_t.data['total'][0]) + 1
    highlight_data = ColumnDataSource.from_df(total_df.loc[(total_df.questionnaire == row['questionnaire']) & (total_df.month == int(row['month'])) & (total_df.number_questions_answered == int(row['completed']))])
    try:
        p_t.quad(top=int(highlight_data['participant_count']), bottom=0, left=float(highlight_data['number_questions_answered'])-0.5, right=float(highlight_data['number_questions_answered'])+0.5, color="#B3DE69")
    except:
        pass


q_select.on_change('value', callback)
p_select.on_change('value', callback)
select_month.on_change('value',callback)
p.on_event(Tap, tap_callback)
layout1 = column(p_select, q_select, p, digitized_copy)
layout2 = column(select_month, p_t)
curdoc().add_root(row(layout1,layout2))

这将突出显示第二张图上单击第一张图的数据点所在的条。但是,当我从下拉列表中选择另一个“问卷”值时,四字形保留在第二个图形上。我曾尝试设置highlight_data = []来重置四字形的数据源,但这不起作用。在选择另一个“问卷”下拉值时如何清除四字形的其他想法?

python graph bokeh interactive
1个回答
0
投票

不要在回调中调用p_t.quad()。在顶层使用空数据源调用一次,然后仅更新数据源本身。这样,如果清除数据源,则四边形将消失。

更好的是,仅重复使用具有所有四边形的完整数据源,并使用过滤器:https://docs.bokeh.org/en/latest/docs/user_guide/data.html#filtering-data

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