Convert Mouse coordinates to plot coordinates

Hi,
Is there any way to convert mouse coordinates to a plot X/Y value? Basically like the tap tool, except I need something a lot more powerful, so having to detect the event myself using JS, and need some way to convert that to the XY values.

In more detail - I want shift click and shift right click to set the plot X min/max and alt click/alt right click to set the plot Y min/max, and the tap tool can neither pass through modifiers, or the right click, and JS events can pick them up trivially. I think that I can get it working by using a hovertool that continuously copies the current XY coordinates into a JS variable, and then the click to read those out, but was wondering if there was a more elegant way to do it.

Many thanks,
Sam

As a starting point, there’s CoordinateTransform.map_from_screen. But I’ve never used it myself.

Thanks for the suggestion! The only mention I can find of that is a pull request for version 2.2 (https://github.com/bokeh/bokeh/pull/10202/files), so I’m guessing it’s not in the release branch yet. Taking that code, and trying to run graph.x_scale.v_invert manually, it gives an error message “this.source_range is null”. I’ve tried doing graph.x_scale.source_range = graph.x_range, (no idea if that’s right!) but then it gives the error message this.target_range is null… So not sure how I should be using that!

Ah, indeed.

Hmm, that’s strange - those attributes should’ve been set for you. If you provide a minimal reproducible example, I could look into it.

Thanks! If you take the example from here:
https://docs.bokeh.org/en/latest/docs/user_guide/plotting.html#single-lines

With the minor modification to give the graph a name, then run in the JS console:

Bokeh.documents[0].get_model_by_name(‘line’).x_scale.v_invert(123)

Mmm, right. Seems like the attributes are set, but only within CartesianFrame. An instance of which is available in PlotView, but the view itself is not a very accessible structure.

By just playing around, I can’t find any way to get hold of the instance of the view in the JS console - I’ve not touched views before, so don’t know how I should use them (if I even should!). I can go through the renderer to get a view, but I think that’s the data source view, rather than the plot view, and I can’t work out a way to go ‘up’. This feels like I’m going into stuff that isn’t designed for users to use and might be changed on newer versions, so maybe better to stick with my hack.

This feels like I’m going into stuff that isn’t designed for users to use and might be changed on newer versions

That is absolutely the case since none of this stuff is documented.