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§
source§impl<T: Data> Plot<T>
impl<T: Data> Plot<T>
sourcepub fn new(
f: impl Fn((u32, u32), &T, &DrawingArea<PietBackend<'_, '_>, Shift>) + 'static
) -> Plot<T>
pub fn new(
f: impl Fn((u32, u32), &T, &DrawingArea<PietBackend<'_, '_>, Shift>) + 'static
) -> Plot<T>
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
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§
source§impl<T> Widget<T> for Plot<T>where
T: Data,
impl<T> Widget<T> for Plot<T>where
T: Data,
source§fn event(&mut self, _: &mut EventCtx<'_, '_>, _: &Event, _: &mut T, _: &Env)
fn event(&mut self, _: &mut EventCtx<'_, '_>, _: &Event, _: &mut T, _: &Env)
Handle an event. Read more
source§fn lifecycle(&mut self, _: &mut LifeCycleCtx<'_, '_>, _: &LifeCycle, _: &T, _: &Env)
fn lifecycle(&mut self, _: &mut LifeCycleCtx<'_, '_>, _: &LifeCycle, _: &T, _: &Env)
Handle a life cycle notification. Read more
source§fn layout(
&mut self,
_: &mut LayoutCtx<'_, '_>,
bc: &BoxConstraints,
_: &T,
_: &Env
) -> Size
fn layout(
&mut self,
_: &mut LayoutCtx<'_, '_>,
bc: &BoxConstraints,
_: &T,
_: &Env
) -> Size
Compute layout. Read more
source§fn paint(&mut self, ctx: &mut PaintCtx<'_, '_, '_>, data: &T, _: &Env)
fn paint(&mut self, ctx: &mut PaintCtx<'_, '_, '_>, data: &T, _: &Env)
Paint the widget appearance. Read more
source§fn compute_max_intrinsic(
&mut self,
axis: Axis,
ctx: &mut LayoutCtx<'_, '_>,
bc: &BoxConstraints,
data: &T,
env: &Env
) -> f64
fn compute_max_intrinsic(
&mut self,
axis: Axis,
ctx: &mut LayoutCtx<'_, '_>,
bc: &BoxConstraints,
data: &T,
env: &Env
) -> f64
Computes max intrinsic/preferred dimension of a widget on the provided axis. Read more
Auto Trait Implementations§
impl<T> !RefUnwindSafe for Plot<T>
impl<T> !Send for Plot<T>
impl<T> !Sync for Plot<T>
impl<T> Unpin for Plot<T>
impl<T> !UnwindSafe for Plot<T>
Blanket Implementations§
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> RoundFrom<T> for T
impl<T> RoundFrom<T> for T
§fn round_from(x: T) -> T
fn round_from(x: T) -> T
Performs the conversion.
§impl<T, U> RoundInto<U> for Twhere
U: RoundFrom<T>,
impl<T, U> RoundInto<U> for Twhere
U: RoundFrom<T>,
§fn round_into(self) -> U
fn round_into(self) -> U
Performs the conversion.
source§impl<T, W> TestWidgetExt<T> for Wwhere
T: Data,
W: Widget<T> + 'static,
impl<T, W> TestWidgetExt<T> for Wwhere
T: Data,
W: Widget<T> + 'static,
source§impl<T, W> WidgetExt<T> for Wwhere
T: Data,
W: Widget<T> + 'static,
impl<T, W> WidgetExt<T> for Wwhere
T: Data,
W: Widget<T> + 'static,
source§fn align_left(self) -> Align<T>
fn align_left(self) -> Align<T>
Wrap this widget in an
Align
widget, configured to align left.source§fn align_right(self) -> Align<T>
fn align_right(self) -> Align<T>
Wrap this widget in an
Align
widget, configured to align right.source§fn align_vertical(self, align: UnitPoint) -> Align<T>
fn align_vertical(self, align: UnitPoint) -> Align<T>
Wrap this widget in an
Align
widget, configured to align vertically.source§fn align_horizontal(self, align: UnitPoint) -> Align<T>
fn align_horizontal(self, align: UnitPoint) -> Align<T>
Wrap this widget in an
Align
widget, configured to align horizontally.source§fn fix_width(self, width: impl Into<KeyOrValue<f64>>) -> SizedBox<T>
fn fix_width(self, width: impl Into<KeyOrValue<f64>>) -> SizedBox<T>
Wrap this widget in a
SizedBox
with an explicit width.source§fn fix_height(self, height: impl Into<KeyOrValue<f64>>) -> SizedBox<T>
fn fix_height(self, height: impl Into<KeyOrValue<f64>>) -> SizedBox<T>
Wrap this widget in a
SizedBox
with an explicit height.source§fn fix_size(
self,
width: impl Into<KeyOrValue<f64>>,
height: impl Into<KeyOrValue<f64>>
) -> SizedBox<T>
fn fix_size(
self,
width: impl Into<KeyOrValue<f64>>,
height: impl Into<KeyOrValue<f64>>
) -> SizedBox<T>
Wrap this widget in an
SizedBox
with an explicit width and heightsource§fn expand_width(self) -> SizedBox<T>
fn expand_width(self) -> SizedBox<T>
source§fn expand_height(self) -> SizedBox<T>
fn expand_height(self) -> SizedBox<T>
source§fn background(self, brush: impl Into<BackgroundBrush<T>>) -> Container<T>
fn background(self, brush: impl Into<BackgroundBrush<T>>) -> Container<T>
source§fn border(
self,
color: impl Into<KeyOrValue<Color>>,
width: impl Into<KeyOrValue<f64>>
) -> Container<T>
fn border(
self,
color: impl Into<KeyOrValue<Color>>,
width: impl Into<KeyOrValue<f64>>
) -> Container<T>
source§fn controller<C>(self, controller: C) -> ControllerHost<Self, C>where
C: Controller<T, Self>,
fn controller<C>(self, controller: C) -> ControllerHost<Self, C>where
C: Controller<T, Self>,
Wrap this widget with the provided
Controller
.source§fn on_added(
self,
f: impl Fn(&mut Self, &mut LifeCycleCtx<'_, '_>, &T, &Env) + 'static
) -> ControllerHost<Self, Added<T, Self>>
fn on_added(
self,
f: impl Fn(&mut Self, &mut LifeCycleCtx<'_, '_>, &T, &Env) + 'static
) -> ControllerHost<Self, Added<T, Self>>
Provide a closure that will be called when this widget is added to the widget tree. Read more
source§fn on_click(
self,
f: impl Fn(&mut EventCtx<'_, '_>, &mut T, &Env) + 'static
) -> ControllerHost<Self, Click<T>>
fn on_click(
self,
f: impl Fn(&mut EventCtx<'_, '_>, &mut T, &Env) + 'static
) -> ControllerHost<Self, Click<T>>
source§fn debug_paint_layout(self) -> EnvScope<T, Self>
fn debug_paint_layout(self) -> EnvScope<T, Self>
Draw the
layout
Rect
s of this widget and its children.source§fn debug_widget_id(self) -> EnvScope<T, Self>
fn debug_widget_id(self) -> EnvScope<T, Self>
Display the
WidgetId
s for this widget and its children, when hot. Read moresource§fn debug_invalidation(self) -> DebugInvalidation<T, Self>
fn debug_invalidation(self) -> DebugInvalidation<T, Self>
Draw a color-changing rectangle over this widget, allowing you to see the
invalidation regions.
source§fn debug_widget(self) -> EnvScope<T, Self>
fn debug_widget(self) -> EnvScope<T, Self>
Set the
DEBUG_WIDGET
env variable for this widget (and its descendants). Read moresource§fn parse(self) -> Parse<Self>where
Self: Widget<String>,
fn parse(self) -> Parse<Self>where
Self: Widget<String>,
👎Deprecated since 0.7.0: Use TextBox::with_formatter instead
Parse a
Widget<String>
’s contentssource§fn with_id(self, id: WidgetId) -> IdentityWrapper<Self>
fn with_id(self, id: WidgetId) -> IdentityWrapper<Self>
source§fn disabled_if(
self,
disabled_if: impl Fn(&T, &Env) -> bool + 'static
) -> DisabledIf<T, Self>
fn disabled_if(
self,
disabled_if: impl Fn(&T, &Env) -> bool + 'static
) -> DisabledIf<T, Self>
Wrap this widget in a
DisabledIf
widget. Read more