GithubHelp home page GithubHelp logo

Comments (7)

Lilneo786 avatar Lilneo786 commented on May 23, 2024
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.

carve11 avatar carve11 commented on May 23, 2024

@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.

Lilneo786 avatar Lilneo786 commented on May 23, 2024

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.

carve11 avatar carve11 commented on May 23, 2024

@Lilneo786 OK, so cb_data and not source, do you know why this difference?

from bokeh.

Lilneo786 avatar Lilneo786 commented on May 23, 2024

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.

carve11 avatar carve11 commented on May 23, 2024

I am still puzzled why source.inspected.indices does not work
Maybe related to #7397 ?

from bokeh.

mattpap avatar mattpap commented on May 23, 2024

Maybe related to #7397?

Yes, this is probably a duplicate of that issue.

from bokeh.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.