我创建了一个基于散景的条形图 . 然后,如果单击堆叠条的特定部分,我会尝试捕获鼠标onclick事件 . 有人可以帮我吗?
非常感谢 .
def plot_bar_stacked():
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]
colors = ["#c9d9d3", "#718dbf", "#e84d60"]
js_resources = INLINE.render_js()
css_resources = INLINE.render_css()
data = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 4, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}
source = ColumnDataSource(data=data)
callback = CustomJS(code="""
// the event that triggered the callback is cb_obj:
// The event type determines the relevant attributes
console.log('Tap event occured at x-position: ' + cb_obj.x)
""")
source.js_on_event('tap', callback)
p = figure(x_range=fruits, plot_height=350, title="Fruit Counts by Year",
toolbar_location=None, tools="")
renderers = p.vbar_stack(years, x='fruits', width=0.9, color=colors, source=source,
legend=[value(x) for x in years], name=years)
for r in renderers:
year = r.name
hover = HoverTool(tooltips=[
("%s total" % year, "@%s" % year),
("index", "$index")
], renderers=[r])
p.add_tools(hover)
...