pub struct Plot { /* private fields */ }
Expand description

A 2D plot, e.g. a graph of a function.

Plot supports multiple lines and points.

use egui::plot::{Line, Plot, Value, Values};
let sin = (0..1000).map(|i| {
    let x = i as f64 * 0.01;
    Value::new(x, x.sin())
});
let line = Line::new(Values::from_values_iter(sin));
Plot::new("my_plot").view_aspect(2.0).show(ui, |plot_ui| plot_ui.line(line));

Implementations

Give a unique id for each plot within the same Ui.

width / height ratio of the data. For instance, it can be useful to set this to 1.0 for when the two axes show the same unit. By default the plot window’s aspect ratio is used.

width / height ratio of the plot region. By default no fixed aspect ratio is set (and width/height will fill the ui it is in).

Width of plot. By default a plot will fill the ui it is in. If you set Self::view_aspect, the width can be calculated from the height.

Height of plot. By default a plot will fill the ui it is in. If you set Self::view_aspect, the height can be calculated from the width.

Minimum size of the plot view.

Show the x-value (e.g. when hovering). Default: true.

Show the y-value (e.g. when hovering). Default: true.

Always keep the x-axis centered. Default: false.

Always keep the y-axis centered. Default: false.

Whether to allow zooming in the plot. Default: true.

Whether to allow scrolling in the plot. Default: true.

Set the side margin as a fraction of the plot size.

For instance, a value of 0.1 will add 10% space on both sides.

Whether to allow zooming in the plot by dragging out a box with the secondary mouse button.

Default: true.

Config the button pointer to use for boxed zooming. Default: Secondary

Whether to allow dragging in the plot to move the bounds. Default: true.

Provide a function to customize the on-hovel label for the x and y axis

use egui::plot::{Line, Plot, Value, Values};
let sin = (0..1000).map(|i| {
    let x = i as f64 * 0.01;
    Value::new(x, x.sin())
});
let line = Line::new(Values::from_values_iter(sin));
Plot::new("my_plot").view_aspect(2.0)
.label_formatter(|name, value| {
    if !name.is_empty() {
        format!("{}: {:.*}%", name, 1, value.y)
    } else {
        "".to_string()
    }
})
.show(ui, |plot_ui| plot_ui.line(line));

Show the pointer coordinates in the plot.

Provide a function to customize the labels for the X axis based on the current visible value range.

This is useful for custom input domains, e.g. date/time.

If axis labels should not appear for certain values or beyond a certain zoom/resolution, the formatter function can return empty strings. This is also useful if your domain is discrete (e.g. only full days in a calendar).

Provide a function to customize the labels for the Y axis based on the current value range.

This is useful for custom value representation, e.g. percentage or units.

If axis labels should not appear for certain values or beyond a certain zoom/resolution, the formatter function can return empty strings. This is also useful if your Y values are discrete (e.g. only integers).

Configure how the grid in the background is spaced apart along the X axis.

Default is a log-10 grid, i.e. every plot unit is divided into 10 other units.

The function has this signature:

fn get_step_sizes(input: GridInput) -> Vec<GridMark>;

This function should return all marks along the visible range of the X axis. step_size also determines how thick/faint each line is drawn. For example, if x = 80..=230 is visible and you want big marks at steps of 100 and small ones at 25, you can return:

vec![
   // 100s
   GridMark { value: 100.0, step_size: 100.0 },
   GridMark { value: 200.0, step_size: 100.0 },

   // 25s
   GridMark { value: 125.0, step_size: 25.0 },
   GridMark { value: 150.0, step_size: 25.0 },
   GridMark { value: 175.0, step_size: 25.0 },
   GridMark { value: 225.0, step_size: 25.0 },
];

There are helpers for common cases, see log_grid_spacer and uniform_grid_spacer.

Default is a log-10 grid, i.e. every plot unit is divided into 10 other units.

See Self::x_grid_spacer for explanation.

Expand bounds to include the given x value. For instance, to always show the y axis, call plot.include_x(0.0).

Expand bounds to include the given y value. For instance, to always show the x axis, call plot.include_y(0.0).

Show a legend including all named items.

Whether or not to show the background Rect. Can be useful to disable if the plot is overlaid over existing content. Default: true.

Show the axes. Can be useful to disable if the plot is overlaid over an existing grid or content. Default: [true; 2].

Add a LinkedAxisGroup so that this plot will share the bounds with other plots that have this group assigned. A plot cannot belong to more than one group.

Interact with and add items to the plot and finally draw it.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more