interactive/
interactive.rs

1use druid::{
2    widget::{Flex, Label, Slider},
3    AppLauncher, Data, Lens, Widget, WidgetExt, WindowDesc,
4};
5use plotters::prelude::*;
6use plotters_druid::Plot;
7
8#[derive(Clone, Data, Lens)]
9struct State {
10    μ: f64,
11}
12
13fn build_plot_widget() -> impl Widget<State> {
14    Plot::new(|_size, data: &State, root| {
15        let μ = data.μ as f32;
16
17        let res = 400;
18        let font = FontDesc::new(FontFamily::SansSerif, 16., FontStyle::Normal);
19
20        let mut chart = ChartBuilder::on(&root)
21            .x_label_area_size(30)
22            .y_label_area_size(30)
23            .margin_right(10)
24            .build_cartesian_2d(0.0..1_f32, 0.0..6_f32)
25            .unwrap();
26
27        chart
28            .configure_mesh()
29            .axis_style(&RGBColor(28, 28, 28))
30            .x_label_style(font.clone().with_color(&WHITE))
31            .y_label_style(font.clone().with_color(&WHITE))
32            .draw()
33            .unwrap();
34
35        for (σ, idx) in [0.32_f32, 0.56, 1., 1.78, 3.16].into_iter().zip(0..) {
36            let fac = 1. / (σ * std::f32::consts::TAU.sqrt());
37            let color = Palette99::pick(idx);
38
39            let data = (0..res).map(|x| x as f32 / res as f32).map(|x| {
40                let y = fac * (-(logit(x) - μ).powi(2) / (2. * σ.powi(2))).exp() / (x * (1. - x));
41                (x, y)
42            });
43
44            chart
45                .draw_series(LineSeries::new(data, &color))
46                .unwrap()
47                .label(format!("σ = {σ}"))
48                .legend(move |(x, y)| {
49                    PathElement::new(
50                        vec![(x, y), (x + 20, y)],
51                        ShapeStyle::from(&color).stroke_width(2),
52                    )
53                });
54        }
55        chart
56            .configure_series_labels()
57            .position(SeriesLabelPosition::UpperRight)
58            .background_style(&RGBColor(41, 41, 41))
59            .border_style(&RGBColor(28, 28, 28))
60            .label_font(font.with_color(&WHITE))
61            .draw()
62            .unwrap();
63    })
64}
65
66fn build_slider_widget(name: String, min: f64, max: f64) -> impl Widget<f64> {
67    Flex::row()
68        .with_child(Label::new(name))
69        .with_flex_child(
70            Slider::new().with_range(min, max).env_scope(|env, _| {
71                // remove the width limit in [`Slider`]
72                env.set(druid::theme::WIDE_WIDGET_WIDTH, std::f64::INFINITY)
73            }),
74            1.,
75        )
76        .must_fill_main_axis(true)
77        .fix_height(35.)
78}
79
80fn build_root_widget() -> impl Widget<State> {
81    Flex::column()
82        .with_flex_child(build_plot_widget(), 1.)
83        .with_spacer(5.)
84        .with_child(build_slider_widget("μ".to_string(), -3., 3.).lens(State::μ))
85        .padding(10.)
86}
87
88fn main() {
89    let main_window = WindowDesc::new(build_root_widget())
90        .title("Logit-Normal Distribution")
91        .window_size((700.0, 450.0));
92
93    AppLauncher::with_window(main_window)
94        .launch(State { μ: 0.8 })
95        .expect("Failed to launch application");
96}
97
98fn logit(p: f32) -> f32 {
99    (p / (1. - p)).ln()
100}