Using a different list to add information to bar chart with hovertool

hello!! I am working on a memory utilization bar graph. The graph itself shows memory utilization in percentage. When I hover over each bar, I would like it if it shows the total memory and used memory number - both are used to derive the percentage but not part of data for the graph itself. From my online searches, I cant find an example I can understand (beginner here). could someone give me a hint?

What I have below was what I could figure out from online resources - when I hover, it displays the label and the percent of memory used. I need to get it to display other info like mem_total and mem_available (I can put them into a list) instead.

# figure 8
mem_percent_label = ['MemUsed', 'SwapUsed', 'CMAUsed']
mem_percent = [0,0,0]
mem_total = get_mem('MemTotal')
swap_total = get_mem('SwapTotal')
cma_total = get_mem('CmaTotal')
mem_plot_hbar = figure(y_range=mem_percent_label, plot_width=800, plot_height=300, title='Memory Usage')
mem_plot_hbar.xaxis.axis_label = '%Used'
mem_plot_hbar.yaxis.axis_label = 'Memory type'
mem_percent_ds = (mem_plot_hbar.hbar(y=mem_percent_label, right=mem_percent,
                                     tags = mem_percent_label,
                                     height=.5, fill_color='steelblue',
                                     hatch_pattern='vertical_line', hatch_weight=2, line_width=0)).data_source
hover = HoverTool(tooltips=[("Total:", "@y"), ("Used:", "@right")])

def update(step):
    mem_total = get_mem('MemTotal')
    mem_available = get_mem('MemAvailable')
    swap_total = get_mem('SwapTotal')
    swap_free = get_mem('SwapFree')
    cma_total = get_mem('CmaTotal')
    cma_free = get_mem('CmaFree')
    global mem_percent
    mem_percent[0] = 100 - mem_available * 100 / max(mem_total, 1)
    mem_percent[1] = 100 - swap_free * 100 / max(swap_total, 1)
    mem_percent[2] = 100 - cma_free * 100 / max(cma_total, 1)
    mem_percent_ds.trigger('data', mem_percent_label, mem_percent)

curdoc().add_periodic_callback(update, 1000)

this is the current output:

Hey @Juie,

It’s a little bit tricky envisioning your data source etc. without a full runnable example, but I think your problem might be made much easier by organizing your data into a ColumnDataSource (or at least a pandas DataFrame) first. If you had a CDS or DataFrame set up like this:

CMA_Total CMA_Used Swap_Total Swap_Used Mem_Total Mem_Used
10 3.386 x y a b

Then the HoverTool might be set up similar to the way you have it:

cma_hover = HoverTool(tooltips=[("Total:", "@CMA_Total"), ("Used:", "@CMA_Used")])

And you can specify which glyph the Hovertool will apply to.

I hope that’s a nudge in the right direction, but if it won’t work with your data, try creating and posting a Minimal Reproducible Example (with data as required) so that someone could copy and paste and test it locally.

1 Like

@carolyn Thank you so much!! that is indeed the nudge in the right direction, I converted my data to ColumnDataSource and used that as source to my figure, and now I can refer to the data that was not plotted in hovertool. Thank you so much!!!

1 Like