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

Plot borrowed data #3849

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7856dfd
Move PlotUi
GunnarMorrigan Jan 23, 2024
292ef19
Add GenericPlotPoints trait and impl for iterator
GunnarMorrigan Jan 23, 2024
8cffd45
Add PlotUiBuilder
GunnarMorrigan Jan 23, 2024
9baf13f
Add lifetime to plotui for boxed PlotItem
GunnarMorrigan Jan 23, 2024
914ff82
Make Points generic
GunnarMorrigan Jan 23, 2024
8a44bd6
Add generic constructor for Points
GunnarMorrigan Jan 23, 2024
e82d832
Add functions to mutate Points in place
GunnarMorrigan Jan 23, 2024
d9c4611
Add PlotItem impl for generic Points and PlotGeometry::GenericPoints
GunnarMorrigan Jan 23, 2024
5e9b705
Remove enum instance Borrowed from PlotPoints
GunnarMorrigan Jan 23, 2024
8428663
Make legend take an iterator
GunnarMorrigan Jan 23, 2024
7ea8a50
Add module plot_ui and import PlotUi and PlotUiBuilder
GunnarMorrigan Jan 23, 2024
4d58b4a
Adjust new show function to have appropriate type
GunnarMorrigan Jan 23, 2024
e9cc2b6
Adjust show body to construct PlotUiBuilder for build_fn
GunnarMorrigan Jan 23, 2024
cfc4ab0
Adjust legend call to updated legend signature of Iterator
GunnarMorrigan Jan 23, 2024
e1cbec1
Adjust PlotUiPrepared to take lifetime
GunnarMorrigan Jan 23, 2024
e57ef93
Adjust doc example to match new build_fn in show function
GunnarMorrigan Jan 23, 2024
f450c6b
Adjust PlotUi builder style and add borrowed points function
GunnarMorrigan Jan 23, 2024
9fbdcb2
Make PlotUi and PlotUiBuilder public
GunnarMorrigan Jan 23, 2024
655ab7c
Fix demo and examples
GunnarMorrigan Jan 23, 2024
525b4f1
Cargo fmt
GunnarMorrigan Jan 23, 2024
552103d
Merge branch 'master' into plotBorrowedData
GunnarMorrigan Jan 24, 2024
48cf815
Add lifetime via PhantomData to Plot struct
GunnarMorrigan Jan 25, 2024
1ab8ff6
Fix duplicate mod decls
GunnarMorrigan Jan 25, 2024
4758345
Fix examples and demo
GunnarMorrigan Jan 25, 2024
ff245bb
Fix doc tests
GunnarMorrigan Jan 25, 2024
5206472
Added scatter plot demo iter example
GunnarMorrigan Jan 25, 2024
231e0b3
Removed unnecessary lifetime
GunnarMorrigan Jan 25, 2024
e4cfe4a
Fix doc Plot path
GunnarMorrigan Jan 25, 2024
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
4 changes: 3 additions & 1 deletion crates/egui_demo_lib/src/demo/context_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ impl ContextMenus {
.width(self.width)
.height(self.height)
.data_aspect(1.0)
.show(ui, |plot_ui| plot_ui.line(line))
.show(ui, |plot_ui| {
plot_ui.line(line);
})
.response
}

Expand Down
182 changes: 143 additions & 39 deletions crates/egui_demo_lib/src/demo/plot_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum Panel {
Interaction,
CustomAxes,
LinkedAxes,
ScatterPlot,
}

impl Default for Panel {
Expand All @@ -41,6 +42,7 @@ pub struct PlotDemo {
interaction_demo: InteractionDemo,
custom_axes_demo: CustomAxesDemo,
linked_axes_demo: LinkedAxesDemo,
scatter_plot: ScatterPlot,
open_panel: Panel,
}

Expand Down Expand Up @@ -87,6 +89,7 @@ impl super::View for PlotDemo {
ui.selectable_value(&mut self.open_panel, Panel::Interaction, "Interaction");
ui.selectable_value(&mut self.open_panel, Panel::CustomAxes, "Custom Axes");
ui.selectable_value(&mut self.open_panel, Panel::LinkedAxes, "Linked Axes");
ui.selectable_value(&mut self.open_panel, Panel::ScatterPlot, "Scatter Plot");
});
ui.separator();

Expand Down Expand Up @@ -115,6 +118,9 @@ impl super::View for PlotDemo {
Panel::LinkedAxes => {
self.linked_axes_demo.ui(ui);
}
Panel::ScatterPlot => {
self.scatter_plot.ui(ui);
}
}
}
}
Expand Down Expand Up @@ -290,9 +296,10 @@ impl LineDemo {
plot = plot.coordinates_formatter(Corner::LeftBottom, CoordinatesFormatter::default());
}
plot.show(ui, |plot_ui| {
plot_ui.line(self.circle());
plot_ui.line(self.sin());
plot_ui.line(self.thingy());
plot_ui
.line(self.circle())
.line(self.sin())
.line(self.thingy());
})
.response
}
Expand Down Expand Up @@ -320,7 +327,7 @@ impl Default for MarkerDemo {
}

impl MarkerDemo {
fn markers(&self) -> Vec<Points> {
fn markers(&self) -> Vec<Points<PlotPoints>> {
MarkerShape::all()
.enumerate()
.map(|(i, marker)| {
Expand Down Expand Up @@ -368,7 +375,7 @@ impl MarkerDemo {
markers_plot
.show(ui, |plot_ui| {
for marker in self.markers() {
plot_ui.points(marker);
plot_ui.owned_points(marker);
Copy link
Owner

Choose a reason for hiding this comment

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

This is a slightly annoying name change, but maybe fine

Copy link
Author

@GunnarMorrigan GunnarMorrigan Jan 26, 2024

Choose a reason for hiding this comment

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

I can replace it with a single function but would need to expose PlotItem as it would go into a public interface.

pub fn points<T: 'a>(&mut self, mut points: Points<T>) -> &mut Self
    where
        Points<T>: PlotItem,
    {
        // Give the points an automatic color if no color has been assigned.
        if points.color == Color32::TRANSPARENT {
            points.color = self.auto_color();
        }
        self.items.push(Box::new(points));

        self
    }

This is something I am not sure about. If it is exposed maybe it should be sealed?

}
})
.response
Expand Down Expand Up @@ -442,11 +449,12 @@ impl LegendDemo {
.data_aspect(1.0);
legend_plot
.show(ui, |plot_ui| {
plot_ui.line(Self::line_with_slope(0.5).name("lines"));
plot_ui.line(Self::line_with_slope(1.0).name("lines"));
plot_ui.line(Self::line_with_slope(2.0).name("lines"));
plot_ui.line(Self::sin().name("sin(x)"));
plot_ui.line(Self::cos().name("cos(x)"));
plot_ui
.line(Self::line_with_slope(0.5).name("lines"))
.line(Self::line_with_slope(1.0).name("lines"))
.line(Self::line_with_slope(2.0).name("lines"))
.line(Self::sin().name("sin(x)"))
.line(Self::cos().name("cos(x)"));
})
.response
}
Expand Down Expand Up @@ -636,12 +644,13 @@ impl LinkedAxesDemo {
))
}

fn configure_plot(plot_ui: &mut egui_plot::PlotUi) {
plot_ui.line(Self::line_with_slope(0.5));
plot_ui.line(Self::line_with_slope(1.0));
plot_ui.line(Self::line_with_slope(2.0));
plot_ui.line(Self::sin());
plot_ui.line(Self::cos());
fn configure_plot(plot_ui: &mut egui_plot::PlotUi<'_>) {
plot_ui
.line(Self::line_with_slope(0.5))
.line(Self::line_with_slope(1.0))
.line(Self::line_with_slope(2.0))
.line(Self::sin())
.line(Self::cos());
}

fn ui(&mut self, ui: &mut Ui) -> Response {
Expand Down Expand Up @@ -690,6 +699,97 @@ impl LinkedAxesDemo {

// ----------------------------------------------------------------------------

struct ScatterPlot {
points: Vec<PlotPoint>,
step: usize,
point_radius: f32,
fill_points: bool,
}

impl PartialEq for ScatterPlot {
fn eq(&self, other: &Self) -> bool {
self.point_radius == other.point_radius && self.fill_points == other.fill_points
}
}

impl Default for ScatterPlot {
fn default() -> Self {
Self::new()
}
}

impl ScatterPlot {
fn new() -> Self {
Self {
points: Self::calculate_points(),
step: 1,
point_radius: 1.5,
fill_points: true,
}
}

fn calculate_points() -> Vec<PlotPoint> {
(0..50_000)
.zip((-10..10).cycle())
.filter_map(|(x, offset)| {
if offset != 0 {
let x = x as f64 / 1000.0;
let y = x * 1.5 + offset as f64;
Some(PlotPoint::new(x, y))
} else {
None
}
})
.collect()
}

fn ui(&mut self, ui: &mut Ui) -> Response {
ui.label("Plot iterators!");
ui.add(
egui::DragValue::new(&mut self.step)
.speed(1)
.clamp_range(1..=100)
.prefix("Filter point step: "),
);
ui.horizontal(|ui| {
ui.checkbox(&mut self.fill_points, "Fill");
ui.add(
egui::DragValue::new(&mut self.point_radius)
.speed(0.1)
.clamp_range(0.0..=f64::INFINITY)
.prefix("Radius: "),
);
});

Plot::new("scatter_plot")
.legend(Legend::default().position(Corner::LeftTop))
.y_axis_width(4)
.show_axes(true)
.show_grid(true)
// .view_aspect(1.0)
// .data_aspect(1.0)
.show(ui, |plot_ui| {
let points = Points::new_generic(self.points.iter().step_by(self.step))
.name("Points")
.filled(self.fill_points)
.radius(self.point_radius);

plot_ui.borrowed_points(points).line(
Line::new(PlotPoints::from_explicit_callback(
|x| x * 1.5,
0.0..=50.0,
100,
))
.name("Line")
.width(self.point_radius * 1.5),
);
})
.response
}
}

// ----------------------------------------------------------------------------

#[derive(PartialEq, Default)]
struct ItemsDemo {
texture: Option<egui::TextureHandle>,
Expand Down Expand Up @@ -743,19 +843,20 @@ impl ItemsDemo {
.show_y(false)
.data_aspect(1.0);
plot.show(ui, |plot_ui| {
plot_ui.hline(HLine::new(9.0).name("Lines horizontal"));
plot_ui.hline(HLine::new(-9.0).name("Lines horizontal"));
plot_ui.vline(VLine::new(9.0).name("Lines vertical"));
plot_ui.vline(VLine::new(-9.0).name("Lines vertical"));
plot_ui.line(line.name("Line with fill"));
plot_ui.polygon(polygon.name("Convex polygon"));
plot_ui.points(points.name("Points with stems"));
plot_ui.text(Text::new(PlotPoint::new(-3.0, -3.0), "wow").name("Text"));
plot_ui.text(Text::new(PlotPoint::new(-2.0, 2.5), "so graph").name("Text"));
plot_ui.text(Text::new(PlotPoint::new(3.0, 3.0), "much color").name("Text"));
plot_ui.text(Text::new(PlotPoint::new(2.5, -2.0), "such plot").name("Text"));
plot_ui.image(image.name("Image"));
plot_ui.arrows(arrows.name("Arrows"));
plot_ui
.hline(HLine::new(9.0).name("Lines horizontal"))
.hline(HLine::new(-9.0).name("Lines horizontal"))
.vline(VLine::new(9.0).name("Lines vertical"))
.vline(VLine::new(-9.0).name("Lines vertical"))
.line(line.name("Line with fill"))
.polygon(polygon.name("Convex polygon"))
.owned_points(points.name("Points with stems"))
.text(Text::new(PlotPoint::new(-3.0, -3.0), "wow").name("Text"))
.text(Text::new(PlotPoint::new(-2.0, 2.5), "so graph").name("Text"))
.text(Text::new(PlotPoint::new(3.0, 3.0), "much color").name("Text"))
.text(Text::new(PlotPoint::new(2.5, -2.0), "such plot").name("Text"))
.image(image.name("Image"))
.arrows(arrows.name("Arrows"));
})
.response
}
Expand Down Expand Up @@ -789,13 +890,15 @@ impl InteractionDemo {
inner: (screen_pos, pointer_coordinate, pointer_coordinate_drag_delta, bounds, hovered),
..
} = plot.show(ui, |plot_ui| {
(
let return_value = (
plot_ui.screen_from_plot(PlotPoint::new(0.0, 0.0)),
plot_ui.pointer_coordinate(),
plot_ui.pointer_coordinate_drag_delta(),
plot_ui.plot_bounds(),
plot_ui.response().hovered(),
)
);

return_value
GunnarMorrigan marked this conversation as resolved.
Show resolved Hide resolved
});

ui.label(format!(
Expand Down Expand Up @@ -925,7 +1028,9 @@ impl ChartsDemo {
.y_axis_width(3)
.allow_zoom(self.allow_zoom)
.allow_drag(self.allow_drag)
.show(ui, |plot_ui| plot_ui.bar_chart(chart))
.show(ui, |plot_ui| {
plot_ui.bar_chart(chart);
})
.response
}

Expand Down Expand Up @@ -985,10 +1090,11 @@ impl ChartsDemo {
.data_aspect(1.0)
.allow_drag(self.allow_drag)
.show(ui, |plot_ui| {
plot_ui.bar_chart(chart1);
plot_ui.bar_chart(chart2);
plot_ui.bar_chart(chart3);
plot_ui.bar_chart(chart4);
plot_ui
.bar_chart(chart1)
.bar_chart(chart2)
.bar_chart(chart3)
.bar_chart(chart4);
})
.response
}
Expand Down Expand Up @@ -1030,9 +1136,7 @@ impl ChartsDemo {
.allow_zoom(self.allow_zoom)
.allow_drag(self.allow_drag)
.show(ui, |plot_ui| {
plot_ui.box_plot(box1);
plot_ui.box_plot(box2);
plot_ui.box_plot(box3);
plot_ui.box_plot(box1).box_plot(box2).box_plot(box3);
})
.response
}
Expand Down
4 changes: 3 additions & 1 deletion crates/egui_demo_lib/src/demo/widget_gallery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ fn example_plot(ui: &mut egui::Ui) -> egui::Response {
.height(32.0)
.show_axes(false)
.data_aspect(1.0)
.show(ui, |plot_ui| plot_ui.line(line))
.show(ui, |plot_ui| {
plot_ui.line(line);
})
.response
}

Expand Down
Loading
Loading