simple/
simple.rs

1use druid::{AppLauncher, Widget, WindowDesc};
2use plotters::prelude::*;
3use plotters_druid::Plot;
4
5fn build_plot_widget() -> impl Widget<()> {
6    Plot::new(|_size, _data, root| {
7        // Code taken from the plotters example: https://github.com/38/plotters#quick-start
8        root.fill(&WHITE).unwrap();
9        let mut chart = ChartBuilder::on(&root)
10            .caption("y=x^2", ("sans-serif", 50).into_font())
11            .margin(5)
12            .margin_right(15)
13            .x_label_area_size(30)
14            .y_label_area_size(30)
15            .build_cartesian_2d(-1f32..1f32, -0.1f32..1f32)
16            .unwrap();
17
18        chart.configure_mesh().draw().unwrap();
19
20        chart
21            .draw_series(LineSeries::new(
22                (-50..=50).map(|x| x as f32 / 50.0).map(|x| (x, x * x)),
23                &RED,
24            ))
25            .unwrap()
26            .label("y = x^2")
27            .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &RED));
28
29        chart
30            .configure_series_labels()
31            .background_style(&WHITE.mix(0.8))
32            .border_style(&BLACK)
33            .draw()
34            .unwrap();
35    })
36}
37
38fn main() {
39    let main_window = WindowDesc::new(build_plot_widget())
40        .title("Hello Plot!")
41        .window_size((400.0, 400.0));
42
43    AppLauncher::with_window(main_window)
44        .launch(())
45        .expect("Failed to launch application");
46}