Skip to content

Allow x/y only zooming by click-dragging respective axis (#17) #102

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

Open
wants to merge 1 commit into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions demo/src/plot_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,7 @@ struct ChartsDemo {
vertical: bool,
allow_zoom: Vec2b,
allow_drag: Vec2b,
allow_axis_zoom_drag: Vec2b,
allow_scroll: Vec2b,
}

Expand All @@ -903,6 +904,7 @@ impl Default for ChartsDemo {
chart: Chart::default(),
allow_zoom: true.into(),
allow_drag: true.into(),
allow_axis_zoom_drag: true.into(),
allow_scroll: true.into(),
}
}
Expand Down Expand Up @@ -949,6 +951,12 @@ impl ChartsDemo {
ui.checkbox(&mut self.allow_drag.x, "X");
ui.checkbox(&mut self.allow_drag.y, "Y");
});
ui.horizontal(|ui| {
ui.label("Allow axis zoom drag:");
ui.checkbox(&mut self.allow_axis_zoom_drag.x, "X");
ui.checkbox(&mut self.allow_axis_zoom_drag.y, "Y");
});

ui.horizontal(|ui| {
ui.label("Allow scroll:");
ui.checkbox(&mut self.allow_scroll.x, "X");
Expand Down Expand Up @@ -987,6 +995,7 @@ impl ChartsDemo {
.clamp_grid(true)
.allow_zoom(self.allow_zoom)
.allow_drag(self.allow_drag)
.allow_axis_zoom_drag(self.allow_axis_zoom_drag)
.allow_scroll(self.allow_scroll)
.show(ui, |plot_ui| plot_ui.bar_chart(chart))
.response
Expand Down
82 changes: 82 additions & 0 deletions egui_plot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ pub struct Plot<'a> {
center_axis: Vec2b,
allow_zoom: Vec2b,
allow_drag: Vec2b,
allow_axis_zoom_drag: Vec2b,
allow_scroll: Vec2b,
allow_double_click_reset: bool,
allow_boxed_zoom: bool,
Expand Down Expand Up @@ -203,6 +204,7 @@ impl<'a> Plot<'a> {
center_axis: false.into(),
allow_zoom: true.into(),
allow_drag: true.into(),
allow_axis_zoom_drag: false.into(),
allow_scroll: true.into(),
allow_double_click_reset: true,
allow_boxed_zoom: true,
Expand Down Expand Up @@ -388,6 +390,16 @@ impl<'a> Plot<'a> {
self
}

/// Whether to allow dragging in the axis areas to automaticall zoom the plot. Default: `true`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we really have this true as default?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really mind. I'll change this to false.
Would you also like me to change the demo app to include options to toggle this behaviour on and off?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!
Repushed to my branch and updated this PR to e031001

#[inline]
pub fn allow_axis_zoom_drag<T>(mut self, on: T) -> Self
where
T: Into<Vec2b>,
{
self.allow_axis_zoom_drag = on.into();
self
}

/// Provide a function to customize the on-hover label for the x and y axis
///
/// ```
Expand Down Expand Up @@ -777,6 +789,7 @@ impl<'a> Plot<'a> {
center_axis,
allow_zoom,
allow_drag,
allow_axis_zoom_drag,
allow_scroll,
allow_double_click_reset,
allow_boxed_zoom,
Expand Down Expand Up @@ -863,6 +876,31 @@ impl<'a> Plot<'a> {
// Allocate the plot window.
let response = ui.allocate_rect(plot_rect, sense);

let x_axis_responses = x_axis_widgets
.iter()
.map(|widget| {
let axis_response = ui.allocate_rect(widget.rect, Sense::drag());
if allow_axis_zoom_drag.x {
axis_response.on_hover_cursor(CursorIcon::ResizeHorizontal)
} else {
axis_response
}
})
.collect::<Vec<_>>();

let y_axis_responses = y_axis_widgets
.iter()
.map(|widget| {
let axis_response = ui.allocate_rect(widget.rect, Sense::drag());

if allow_axis_zoom_drag.y {
axis_response.on_hover_cursor(CursorIcon::ResizeVertical)
} else {
axis_response
}
})
.collect::<Vec<_>>();

// Load or initialize the memory.
ui.ctx().check_for_id_clash(plot_id, plot_rect, "Plot");

Expand Down Expand Up @@ -1081,6 +1119,50 @@ impl<'a> Plot<'a> {
.translate_bounds((delta.x as f64, delta.y as f64));
mem.auto_bounds = mem.auto_bounds.and(!allow_drag);
}
// Axis zoom dragging
if allow_axis_zoom_drag.x
&& x_axis_responses
.iter()
.any(|r| r.dragged_by(PointerButton::Primary))
{
let axis_response = x_axis_responses
.iter()
.find(|r| r.dragged_by(PointerButton::Primary))
.expect("One x-axis response should be dragged");

if let Some(_hover_pos) = axis_response.hover_pos() {
let delta = axis_response.drag_delta();
let mut zoom = delta.clamp(Vec2::splat(-10.0), Vec2::splat(10.0)) / 10.0;
zoom.x += 1.0;
zoom.y = 1.0;
if zoom.x != 1.0 {
mem.transform.zoom(zoom, plot_rect.center());
mem.auto_bounds = false.into();
}
}
}

if allow_axis_zoom_drag.y
&& y_axis_responses
.iter()
.any(|r| r.dragged_by(PointerButton::Primary))
{
let axis_response = y_axis_responses
.iter()
.find(|r| r.dragged_by(PointerButton::Primary))
.expect("One y-axis response should be dragged");

if let Some(_hover_pos) = axis_response.hover_pos() {
let delta = axis_response.drag_delta();
let mut zoom = delta.clamp(Vec2::splat(-10.0), Vec2::splat(10.0)) / 10.0;
zoom.x = 1.0;
zoom.y += 1.0;
if zoom.y != 1.0 {
mem.transform.zoom(zoom, plot_rect.center());
mem.auto_bounds = false.into();
}
}
}

// Zooming
let mut boxed_zoom_rect = None;
Expand Down