Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capturing background clicks with coordinates #973

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions bqplot/marks.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def __init__(self, **kwargs):
self._legend_hover_handlers = CallbackDispatcher()
self._element_click_handlers = CallbackDispatcher()
self._bg_click_handlers = CallbackDispatcher()
self._bg_click_with_coordinates_handlers = CallbackDispatcher()
self.on_msg(self._handle_custom_msgs)

def on_hover(self, callback, remove=False):
Expand All @@ -266,6 +267,9 @@ def on_element_click(self, callback, remove=False):
def on_background_click(self, callback, remove=False):
self._bg_click_handlers.register_callback(callback, remove=remove)

def on_background_click_with_coordinates(self, callback, remove=False):
self._bg_click_with_coordinates_handlers.register_callback(callback, remove=remove)

def _handle_custom_msgs(self, _, content, buffers=None):
if content.get('event', '') == 'hover':
self._hover_handlers(self, content)
Expand All @@ -279,6 +283,8 @@ def _handle_custom_msgs(self, _, content, buffers=None):
self._element_click_handlers(self, content)
elif content.get('event', '') == 'background_click':
self._bg_click_handlers(self, content)
elif content.get('event', '') == 'background_click_with_coordinates':
self._bg_click_with_coordinates_handlers(self, content)


@register_mark('bqplot.Lines')
Expand Down
27 changes: 23 additions & 4 deletions js/src/ScatterBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ export abstract class ScatterBase extends Mark {
this.event_listeners.parent_clicked = this.reset_selection;
this.event_listeners.element_clicked = this.scatter_click_handler;
break;
default:
this.event_listeners.parent_clicked = this.send_pos;
this.event_listeners.element_clicked = () => {};
break;
}
}

Expand Down Expand Up @@ -650,21 +654,36 @@ export abstract class ScatterBase extends Mark {
return;
}

add_element() {
get_pos() {
const mouse_pos = d3.mouse(this.el);
const curr_pos = [mouse_pos[0], mouse_pos[1]];

const x_scale = this.scales.x, y_scale = this.scales.y;
return [
x_scale.scale.invert(curr_pos[0]),
y_scale.scale.invert(curr_pos[1])
]
}

send_pos() {
const pos = this.get_pos()
this.send({
event: "background_click_with_coordinates",
point: {x : pos[0], y: pos[1]}
});
}

add_element() {
//add the new point to data
const x = this.model.get('x');
const y = this.model.get('y');
// copy data and fill in the last value
const xn = new x.constructor(x.length+1)
const yn = new y.constructor(y.length+1)
const pos = this.get_pos()
xn.set(x)
yn.set(y)
xn[x.length] = x_scale.scale.invert(curr_pos[0]);
yn[y.length] = y_scale.scale.invert(curr_pos[1]);
xn[x.length] = pos[0];
yn[y.length] = pos[1];
this.model.set("x", xn);
this.model.set("y", yn);
this.touch();
Expand Down