import holoviews as hv
from holoviews import opts
from holoviews.plotting.links import DataLink
hv.extension('bokeh')
This example demonstrates how to use a DataLink to join two elements displaying the same data, a choropleth of the Texas unemployment rate alongside a Table of the same data. By linking the two selecting a polygon will highlight it in the table and vice versa.
from bokeh.sampledata.us_counties import data as counties
from bokeh.sampledata.unemployment import data as unemployment
# Generate dummy data
data2 = {}
data3 = {}
for ind, keys in enumerate(list(unemployment.keys())):
data2[keys] = unemployment[keys]+ind/10
data3[keys] = unemployment[keys]-ind/10
print(list(data2.items())[0:3])
[((1, 1), 9.7), ((1, 3), 9.2), ((1, 5), 13.6)]
counties = [dict(county, Unemployment=unemployment[cid], Data2=data2[cid], Data3=data3[cid])
for cid, county in counties.items()
if county["state"] == "tx"]
def map_and_table(counties,var='Unemployment'):
county_data = [(county['detailed name'], county[var]) for county in counties]
choropleth = hv.Polygons(counties, ['lons', 'lats'], [('detailed name', 'County'), var])#, label='Texas ' + var)
table = hv.Table(county_data, [('detailed name', 'County'), var])
# Link the choropleth and the table
DataLink(choropleth, table)
layout = choropleth + table
layout.opts(
opts.Table(height=328),
opts.Polygons(width=400, height=400, tools=['hover', 'tap'], xaxis=None,
yaxis=None, color_index=var))
return layout
layout=map_and_table(counties,var='Data2')
layout
statistics = ('Unemployment','Data2','Data3')
# get a map object
layout_dict = {stat: map_and_table(counties,var=stat)[0] for stat in statistics}
kdims = [hv.Dimension(('statistics', 'Statistics'))]
holomaps = hv.HoloMap(layout_dict, kdims=kdims)
# get a table object
layout_dict = {stat: map_and_table(counties,var=stat)[1] for stat in statistics}
kdims = [hv.Dimension(('statistics', 'Statistics'))]
holotables = hv.HoloMap(layout_dict, kdims=kdims)
# Link both object
DataLink(holomaps, holotables)
holomapwithtable = holomaps + holotables
holomapwithtable
from bokeh.resources import INLINE
hv.save(holomapwithtable,'test2.html', resources=INLINE)
from bokeh.io import show
from bokeh.models import CustomJS, RadioButtonGroup
LABELS = ["Option 1", "Option 2", "Option 3"]
radio_button_group = RadioButtonGroup(labels=LABELS, active=0)
radio_button_group.js_on_click(CustomJS(code="""
console.log('radio_button_group: active=' + this.active, this.toString())
"""))
show(radio_button_group)