Skip to content

Conversation

@bombless
Copy link

  • Closes #143
  • [ ✓ ] I have followed the instructions in the PR template

@bombless
Copy link
Author

With the patch in this PR, issue #143 will be solved as

struct App;

impl eframe::App for App {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            let plot = Plot::new("mouseover test")
                .allow_scroll(true)
                .allow_zoom(true)
                .allow_boxed_zoom(true)
                .allow_drag(true)
                .include_x(0.0)
                .include_x(0.1);

            plot.show(ui, |plot_ui| {
                let bounds = plot_ui.plot_bounds();
                let val = bounds.min()[0];

                plot_ui.text(PlotText::new(
                    "",
/* ============ no need to clamp now since we can call `.overlay(true)` */
                    PlotPoint { x: val, y: 0.0 },
                    val.to_string(),
                ).overlay(true));
            });
        });
    }
}

and to those two cases in its comments:

struct App;

impl App {


    fn draw_plot(ui: &mut egui::Ui) {
        let plot = Plot::new("dominant_freq_plot")
            .allow_scroll(false)
            .allow_zoom(true)
            .allow_boxed_zoom(true)
            .allow_drag(true)
            .default_x_bounds(0.0, 60.0)
            .default_y_bounds(-100.0, 100.0)
            .auto_bounds(Vec2b::new(true, true));

        plot.show(ui, |plot_ui| {
            // ...


            
            if let Some(pointer) = plot_ui.pointer_coordinate() {
                let txt = format!("{pointer:?}");
                plot_ui.text(
/* ============ no need to clamp now since we can call `.overlay(true)` */
                    PlotText::new("鼠标坐标提示", PlotPoint {x: pointer.x, y: pointer.y}, txt)
                        .overlay(true)
                        .anchor(Align2([Align::Min, Align::Max]))
                );
            }
        });
    }
}
struct App;

// Constants for time and frequency axis bounds
const TIME_BOUNDS: (f64, f64) = (0.0, 10.0);
const FREQ_BOUNDS: (f64, f64) = (0.0, 10.0);

impl App {
    fn draw_plot(&self, ui: &mut egui::Ui) {
        // Create a plot with specified interaction settings
        let plot = Plot::new("dominant_freq_plot")
            .allow_scroll(false)
            .allow_zoom(true)
            .allow_boxed_zoom(true)
            .allow_drag(true)
            .default_x_bounds(TIME_BOUNDS.0, TIME_BOUNDS.1)
            .default_y_bounds(FREQ_BOUNDS.0, FREQ_BOUNDS.1)
            .auto_bounds(Vec2b::new(true, true));

        plot.show(ui, |plot_ui| {
            let beat_duration = 1f64;
            let bounds = plot_ui.plot_bounds();
            let y_span = bounds.max()[1] - bounds.min()[1];

            // Rectangle position: at the top of the plot
            let rect_y_center = bounds.max()[1] - 0.05 * y_span;
            let rect_height = 0.08 * y_span;
            let rect_width = beat_duration * 0.8;

            // Rectangle corners
            let rect_x_min = 0.0f64;
            let rect_x_max = 0.0 + rect_width;
            let rect_y_max = rect_y_center + rect_height / 2.0;

/* ============ no need to clamp now since we can call `.overlay(true)` */

            // let rect_x_min = rect_x_min.clamp(TIME_BOUNDS.0, TIME_BOUNDS.1);
            // let rect_x_max = rect_x_max.clamp(TIME_BOUNDS.0, TIME_BOUNDS.1);
            // let rect_y_max = rect_y_max.clamp(FREQ_BOUNDS.0, FREQ_BOUNDS.1);

            println!("{:?}", vec![
                [rect_x_min, rect_y_max],
                [rect_x_max, rect_y_max],
            ]);
            // Draw top edge of the rectangle
            plot_ui.line(
                Line::new(
                    "PlotPoints",
                    PlotPoints::from_iter(vec![
                        [rect_x_min, rect_y_max],
                        [rect_x_max, rect_y_max],
                    ]),
                )
                    .overlay(true)
                    .width(10.0),
            );
        });
    }
}

Copy link
Contributor

@bircni bircni left a comment

Choose a reason for hiding this comment

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

sorry but whatever you did here - the code is a mess f.e. the comment for a fn you added is in 414 and the code is in 634 - like what?!??!

@kitizz
Copy link

kitizz commented Dec 10, 2025

As @bircni pointed out, this PR should probably be labeled as draft. But more than that, a solution for #143 might exist that doesn't require theses change to egui_plot. Discussion should continue there for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mouseover make the plot to zoom in improperly

3 participants