Skip to content

Plot bounds broken when all y values are the same #2307

@nknotts

Description

@nknotts

Describe the bug
When all y values are identically the same, the calculated y bounds will be considered invalid and the default y-range of [0, 1] will be used instead. The same bug applies to x as well.

To Reproduce
I've created an example application here:

https://github.com/nknotts/egui/blob/added-plot-bounds-example/examples/plot_bounds/src/main.rs

The gist is

            let x_range = 0..1000;
            let single2: PlotPoints = x_range
                .clone()
                .map(|i| {
                    let x = i as f64 * 0.01;
                    [x, 2.0]
                })
                .collect();

            ui.label("All values == 2, completely missing");
            Plot::new("plot3")
                .allow_drag(false)
                .allow_zoom(false)
                .allow_scroll(false)
                .height(row_height)
                .show(ui, |plot_ui| plot_ui.line(Line::new(single2)));

Where determining the bounds, there is

let auto_x = !bounds_modified.x && (!min_auto_bounds.is_valid_x() || auto_bounds.x);

        let auto_x = !bounds_modified.x && (!min_auto_bounds.is_valid_x() || auto_bounds.x);
        let auto_y = !bounds_modified.y && (!min_auto_bounds.is_valid_y() || auto_bounds.y);

Ultimately there is a call to

self.is_finite() && self.width() > 0.0 && self.height() > 0.0

    pub fn is_valid(&self) -> bool {
        self.is_finite() && self.width() > 0.0 && self.height() > 0.0
    }

where, in this case, the height will be == 0.0 since all values are the same. Since the y bounds are not valid, the default bounds of [0, 1] are used.

Expected behavior
An appropriate bound to be calculated for this case.

Screenshots
Screen Shot 2022-11-14 at 1 33 45 PM

Desktop (please complete the following information):

  • OS: macOS
  • Version 12.6

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething is brokenegui_plotRelated to egui_plotgood first issueGood for newcomershelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions