From 4c6aa8d054449df91437b4fcd9658b39af13b55d Mon Sep 17 00:00:00 2001 From: AnonimAnonim2245 Date: Fri, 19 Jan 2024 12:36:04 +0200 Subject: [PATCH] avoid duplication for series --- widgetry_demo/src/lib.rs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/widgetry_demo/src/lib.rs b/widgetry_demo/src/lib.rs index 5979e77844..b7c2c8a90a 100644 --- a/widgetry_demo/src/lib.rs +++ b/widgetry_demo/src/lib.rs @@ -55,7 +55,18 @@ impl Demo { tabs, } } - + fn create_series(&self, label: &str, color: Color, func: F) -> Series + where + F: Fn(usize) -> usize, + { + Series { + label: label.to_string(), + color, + pts: (0..(self.elapsed.inner_seconds() as usize)) + .map(|s: usize| (Time::START_OF_DAY + Duration::seconds(s as f64), func(s))) + .collect(), + } + } fn make_timeseries_panel(&self, ctx: &mut EventCtx) -> Panel { // Make a table with 3 columns. let mut col1 = vec![Line("Time").into_widget(ctx)]; @@ -91,21 +102,8 @@ impl Demo { ctx, "timeseries", vec![ - Series { - label: "Linear".to_string(), - color: Color::GREEN, - // These points are (x axis = Time, y axis = usize) - pts: (0..(self.elapsed.inner_seconds() as usize)) - .map(|s| (Time::START_OF_DAY + Duration::seconds(s as f64), s)) - .collect(), - }, - Series { - label: "Quadratic".to_string(), - color: Color::BLUE, - pts: (0..(self.elapsed.inner_seconds() as usize)) - .map(|s| (Time::START_OF_DAY + Duration::seconds(s as f64), s.pow(2))) - .collect(), - }, + self.create_series("Linear", Color::GREEN, |s| s), + self.create_series("Quadratic", Color::BLUE, |s| s.pow(2)), ], PlotOptions { // Without this, the plot doesn't stretch to cover times in between whole