Comments (7)
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, CustomJS, HoverTool
src = ColumnDataSource(
data = {
'x': [1,2,3,4],
'y1': [1,2,3,4],
'y2': [2,3,4,5],
'y3': [3,4,5,6]
}
)
style = {
'y1': {'marker': 'circle', 'color': 'orange'},
'y2': {'marker': 'square', 'color': 'blue'},
'y3': {'marker': 'triangle', 'color': 'red'},
}
p = figure(width=400, height=400, tools = '')
for i in style:
r = p.scatter(
x = 'x', y = i,
marker = style[i]['marker'],
color = style[i]['color'],
size = 12, alpha = 0.5,
source = src,
name = i,
legend_label = i
)
hover = HoverTool(
tooltips = [
(i, f'@{i}'),
('index', '$index'),
],
renderers = [r]
)
hover.callback = CustomJS(
args = {'src': src},
code='''
const idx = src.inspected.indices;
if (idx.length > 0) {
console.log(src.data.name);
console.log(idx);
}
''')
p.add_tools(hover)
p.legend[0].location = 'bottom_right'
show(p)
With this adjustment, you generate an individual HoverTool and CustomJS callback for every renderer. The callback code accurately displays the name of the renderer and the index of the hovered data points for each series (y1, y2, y3) upon hovering over them.
from bokeh.
@Lilneo786 Thanks for taking the time looking into this and proposing a solution. However, I do not observe anything that solves my issue; not sure if we are using the same version of Bokeh or if something else is different. Using your code proposal the CustomJS
callback is still only triggered when hovering data points of the last series, y3
when I look in the browser console. Also, what you propose in the CustomJS
callback code, src.data.name
yields undefined
in my system (not sure why I would change my code where I got rend.name
since this is ok for me)
from bokeh.
This should ensure that the correct glyph name is logged when hovering over the points of each series.
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, CustomJS, HoverTool
src = ColumnDataSource(
data={
'x': [1, 2, 3, 4],
'y1': [1, 2, 3, 4],
'y2': [2, 3, 4, 5],
'y3': [3, 4, 5, 6]
}
)
style = {
'y1': {'marker': 'circle', 'color': 'orange'},
'y2': {'marker': 'square', 'color': 'blue'},
'y3': {'marker': 'triangle', 'color': 'red'},
}
p = figure(width=400, height=400, tools='')
for i in style:
r = p.scatter(
x='x', y=i,
marker=style[i]['marker'],
color=style[i]['color'],
size=12, alpha=0.5,
source=src,
name=i,
legend_label=i
)
p.add_tools(HoverTool(
tooltips=[
(i, f'@{i}'),
('index', '$index'),
],
renderers=[r],
callback=CustomJS(
args={'src': src},
code='''
const index = cb_data.index.indices[0];
const glyph_name = %s;
console.log(glyph_name);
console.log(index);
''' % repr(i) # Pass the glyph name as a string
)
))
p.legend[0].location = 'bottom_right'
show(p)
from bokeh.
@Lilneo786 OK, so cb_data
and not source
, do you know why this difference?
from bokeh.
cb_data provides information about the callback event itself, including indices of selected data points.
source refers to the data used for plotting.
You use cb_data to access event-related info, like which data point was selected, while source is for managing the plot's data.
from bokeh.
I am still puzzled why source.inspected.indices
does not work
Maybe related to #7397 ?
from bokeh.
Maybe related to #7397?
Yes, this is probably a duplicate of that issue.
from bokeh.
Related Issues (20)
- Useless error formatting can be sometimes costly in on_change setup HOT 3
- [BUG] Most toolbar functions non-functional after update HOT 8
- [BUG] Blurry plots with WebGL HOT 6
- [BUG] HoverTool can't adress source with `ΓΆ` HOT 1
- Missing links in docs in "Third-party integrations" page
- [BUG] Toolbar above plot with many hovers shifted to the right
- [BUG] Outdated link in warning for standalone Python callback
- [BUG] Slight change in axes when select using match_aspect HOT 1
- [FEATURE] Support log axes for "pre-transformed" data HOT 6
- [BUG] max_zoom+1 is used for WMTSTileSource/MercatorTileSource
- Errors related to MIN_PREFERRED_MAX_WIDTH while building the docs HOT 1
- Broken page in the docs
- Explanation is part of the code block in the latex_axis_labels_titles_labels example HOT 1
- [BUG] Enforce `max_interval` / `min_interval` if `{x,y}_range` is not set HOT 1
- [BUG] Javascript Error Using AjaxDataSource With Image Data HOT 4
- [FEATURE] Create option for wheel zoom tool to apply only to subplot nearest to the cursor position HOT 1
- [BUG] Documentation Missing space character in the command to enable jupyter server extension in jupyterLab docs.
- Problems initializing Models using BokehJS without Python HOT 3
- Typo in the documentation related to writing tests in the Contribute section HOT 2
- App README links are all old / bad
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bokeh.