emilk / emilk/egui_plot

default_x_bounds does not set "reset" bounds

Open
#123 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
468
Forks
108
Avg merge
15m
Merged PRs (30d)
3

Description

egui_plot 0.32.1

I'm trying to accomplish the following behavior:

- Set my own X/Y bounds for a plot. Ignore contained data for bounds.
- Allow user to move/zoom/... the plot
- On double-click, make the plot go back to my own X/Y bounds

This seems like a reasonable behavior to me.

Judging by the comment, `Plot.default_x_bounds` seem to do exactly that, as the comment states that it `/// Overwrite the starting and reset bounds used for the x axis`. However, in practice,

## 1. If I do a simple

```rust
Plot::new("circle_plot")
.default_x_bounds(-0.5, 0.5)
.default_y_bounds(-0.3, 0.3)
.show(ui, |plot_ui| {
plot_ui.line(Line::new("circle", PlotPoints::Owned(points)));
});
```

the user can't control the plot, the bounds always come back to the defaults

## 2. If I set bounds only once on start

it works better, but double-clicking does NOT reset the plot to *my* bounds (as suggested by the comment "Overwrite the starting and *reset* bounds used for the x axis"), but in such a way as to contain all the data.

```rust

let plot = Plot::new("circle_plot");

let plot = if self.first_time {
plot.default_x_bounds(-0.5, 0.5).default_y_bounds(-0.3, 0.3)
} else {
plot
};

plot.show(ui, |plot_ui| {
plot_ui.line(Line::new("circle", PlotPoints::Owned(points)).width(3.));
});
```

I want double-click to reset to my bounds. How can I achieve my objective?

The whole example:

```rust
use eframe::egui;
use egui_plot::{Line, Plot, PlotPoint, PlotPoints};

struct MyApp {
first_time: bool,
}

impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
let points: Vec = (0..100).map(|i| {
let angle = (i as f64) * std::f64::consts::TAU / 100.0;
PlotPoint { x: angle.cos(), y: angle.sin() }
}).collect();

let plot = Plot::new("circle_plot");

let plot = if self.first_time {
plot.default_x_bounds(-0.5, 0.5).default_y_bounds(-0.3, 0.3)
} else {
plot
};

plot.show(ui, |plot_ui| {
plot_ui.line(Line::new("circle", PlotPoints::Owned(points)).width(3.));
});
});

self.first_time = false;
}
}

fn main() {
let native_options = eframe::NativeOptions::default();
let _ = eframe::run_native(
"My XY Circle Plot",
native_options,
Box::new(|_cc| Ok(Box::new(MyApp { first_time: true }))),
);
}

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.