Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Present custom menu on right click Plotly.js

Is it possible to present a custom sub-menu when a user right clicks a plotly chart, instead of the default one?

Default right click

like image 427
George Bikas Avatar asked Jul 30 '26 13:07

George Bikas


1 Answers

You can add an event to contextmenu plotly container.

Here is the reference to contextmenu docs - https://developer.mozilla.org/en/docs/Web/Events/contextmenu

You will have prevent the default behaviour of context menu so that the browsers default context menu doesn't overlap with yours.

You can use this

document.querySelector('#plotly-container').addEventListender('contextmenu', function(event) {
    event.preventDefault();

    // Your code here
});

In the callback function, you need to identify the mouse position with event.clientX and event.clientY and position a container absolutely at those co-ordinates.

You can refer to this question to understand how to create a div container next to your mouse position.

How do I position a div next to a mouse click using JQuery?

You can refer to this question to understand more on how to bind contextmenu event.

How can I capture the right-click event in JavaScript?

like image 179
atman Avatar answered Aug 01 '26 01:08

atman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!