Struct plotters_druid::Plot

source ·
pub struct Plot<T: Data> { /* private fields */ }
Expand description

The type of a plot widget.

See Plot::new for information on how to construct this.

This implements druid::Widget so it can be used like any other widget type.

fn build_plot_widget() -> impl Widget<()> {
    // ... construct and return widget using Plot::new()
}

let main_window = WindowDesc::new(build_plot_widget());

Implementations§

Create a plot widget

This takes a function that should draw the plot using the normal plotters API. The function has access to the width and height of the plotting area, to the Data of the rust widget, and to a plotters DrawingArea.

Plot::new(|(width, height), data: &AppState, root| {
    root.fill(&WHITE).unwrap();
    let mut chart = ChartBuilder::on(&root)
        .build_cartesian_2d(-1f32..1f32, -0.1f32..1f32)
        .unwrap();

    // see the plotters documentation on how to use `chart`
});
Examples found in repository?
examples/simple.rs (lines 6-35)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
fn build_plot_widget() -> impl Widget<()> {
    Plot::new(|_size, _data, root| {
        // Code taken from the plotters example: https://github.com/38/plotters#quick-start
        root.fill(&WHITE).unwrap();
        let mut chart = ChartBuilder::on(&root)
            .caption("y=x^2", ("sans-serif", 50).into_font())
            .margin(5)
            .margin_right(15)
            .x_label_area_size(30)
            .y_label_area_size(30)
            .build_cartesian_2d(-1f32..1f32, -0.1f32..1f32)
            .unwrap();

        chart.configure_mesh().draw().unwrap();

        chart
            .draw_series(LineSeries::new(
                (-50..=50).map(|x| x as f32 / 50.0).map(|x| (x, x * x)),
                &RED,
            ))
            .unwrap()
            .label("y = x^2")
            .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &RED));

        chart
            .configure_series_labels()
            .background_style(&WHITE.mix(0.8))
            .border_style(&BLACK)
            .draw()
            .unwrap();
    })
}
More examples
Hide additional examples
examples/interactive.rs (lines 14-63)
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
fn build_plot_widget() -> impl Widget<State> {
    Plot::new(|_size, data: &State, root| {
        let μ = data.μ as f32;

        let res = 400;
        let font = FontDesc::new(FontFamily::SansSerif, 16., FontStyle::Normal);

        let mut chart = ChartBuilder::on(&root)
            .x_label_area_size(30)
            .y_label_area_size(30)
            .margin_right(10)
            .build_cartesian_2d(0.0..1_f32, 0.0..6_f32)
            .unwrap();

        chart
            .configure_mesh()
            .axis_style(&RGBColor(28, 28, 28))
            .x_label_style(font.clone().with_color(&WHITE))
            .y_label_style(font.clone().with_color(&WHITE))
            .draw()
            .unwrap();

        for (σ, idx) in [0.32_f32, 0.56, 1., 1.78, 3.16].into_iter().zip(0..) {
            let fac = 1. / (σ * std::f32::consts::TAU.sqrt());
            let color = Palette99::pick(idx);

            let data = (0..res).map(|x| x as f32 / res as f32).map(|x| {
                let y = fac * (-(logit(x) - μ).powi(2) / (2. * σ.powi(2))).exp() / (x * (1. - x));
                (x, y)
            });

            chart
                .draw_series(LineSeries::new(data, &color))
                .unwrap()
                .label(format!("σ = {σ}"))
                .legend(move |(x, y)| {
                    PathElement::new(
                        vec![(x, y), (x + 20, y)],
                        ShapeStyle::from(&color).stroke_width(2),
                    )
                });
        }
        chart
            .configure_series_labels()
            .position(SeriesLabelPosition::UpperRight)
            .background_style(&RGBColor(41, 41, 41))
            .border_style(&RGBColor(28, 28, 28))
            .label_font(font.with_color(&WHITE))
            .draw()
            .unwrap();
    })
}

Trait Implementations§

Handle an event. Read more
Handle a life cycle notification. Read more
Update the widget’s appearance in response to a change in the app’s Data or Env. Read more
Compute layout. Read more
Paint the widget appearance. Read more
Computes max intrinsic/preferred dimension of a widget on the provided axis. Read more

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.

Performs the conversion.
Performs the conversion.
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.
Wrap this widget in a Padding widget with the given [Insets]. Read more
Wrap this widget in an Align widget, configured to center it.
Wrap this widget in an Align widget, configured to align left.
Wrap this widget in an Align widget, configured to align right.
Wrap this widget in an Align widget, configured to align vertically.
Wrap this widget in an Align widget, configured to align horizontally.
Wrap this widget in a SizedBox with an explicit width.
Wrap this widget in a SizedBox with an explicit height.
Wrap this widget in an SizedBox with an explicit width and height
Wrap this widget in a SizedBox with an infinite width and height. Read more
Wrap this widget in a SizedBox with an infinite width. Read more
Wrap this widget in a SizedBox with an infinite width. Read more
Wrap this widget in a Container with the provided background. Read more
Wrap this widget in a Container with the given border. Read more
Wrap this widget in a EnvScope widget, modifying the parent Env with the provided closure.
Wrap this widget with the provided Controller.
Provide a closure that will be called when this widget is added to the widget tree. Read more
Control the events of this widget with a Click widget. The closure provided will be called when the widget is clicked with the left mouse button. Read more
Draw the layout Rects of this widget and its children.
Display the WidgetIds for this widget and its children, when hot. Read more
Draw a color-changing rectangle over this widget, allowing you to see the invalidation regions.
Set the DEBUG_WIDGET env variable for this widget (and its descendants). Read more
Wrap this widget in a LensWrap widget for the provided Lens.
👎Deprecated since 0.7.0: Use TextBox::with_formatter instead
Parse a Widget<String>’s contents
Assign the widget a specific WidgetId. Read more
Wrap this widget in a Box.
Wrap this widget in a Scroll widget.
Wrap this widget in a DisabledIf widget. Read more
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