From a5910ff99be496d02972e8c7f3cf2fa720492686 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sat, 21 Dec 2019 11:04:30 -0800 Subject: [PATCH] add back a legend to the plots --- ezgui/src/text.rs | 6 ++++++ ezgui/src/widgets/plot.rs | 34 ++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/ezgui/src/text.rs b/ezgui/src/text.rs index 6d11525c1d..5199cf04e4 100644 --- a/ezgui/src/text.rs +++ b/ezgui/src/text.rs @@ -98,6 +98,12 @@ impl Text { self } + pub fn bg(mut self, bg: Color) -> Text { + assert!(self.bg_color.is_none()); + self.bg_color = Some(bg); + self + } + pub fn add(&mut self, line: TextSpan) { self.lines.push((None, vec![line])); } diff --git a/ezgui/src/widgets/plot.rs b/ezgui/src/widgets/plot.rs index 8b9f2128ed..ff46c21561 100644 --- a/ezgui/src/widgets/plot.rs +++ b/ezgui/src/widgets/plot.rs @@ -21,17 +21,37 @@ pub struct Plot { impl> Plot { // TODO I want to store y_zero in the trait, but then we can't Box max_y. - // Returns (plot, X axis labels, Y axis labels) + // Returns (plot, legend, X axis labels, Y axis labels) fn new( series: Vec>, y_zero: T, ctx: &EventCtx, - ) -> (Plot, ManagedWidget, ManagedWidget) { + ) -> (Plot, ManagedWidget, ManagedWidget, ManagedWidget) { let mut batch = GeomBatch::new(); let width = 0.5 * ctx.canvas.window_width; let height = 0.4 * ctx.canvas.window_height; + let radius = 15.0; + let legend = ManagedWidget::col( + series + .iter() + .map(|s| { + ManagedWidget::row(vec![ + ManagedWidget::draw_batch( + ctx, + GeomBatch::from(vec![( + s.color, + Circle::new(Pt2D::new(radius, radius), Distance::meters(radius)) + .to_polygon(), + )]), + ), + ManagedWidget::draw_text(ctx, Text::from(Line(&s.label))), + ]) + }) + .collect(), + ); + // Assume min_x is Time::START_OF_DAY and min_y is 0 let max_x = series .iter() @@ -118,7 +138,7 @@ impl> Plot col.reverse(); let y_axis = ManagedWidget::col(col); - (plot, x_axis, y_axis) + (plot, legend, x_axis, y_axis) } pub fn draw(&self, g: &mut GfxCtx) { @@ -127,7 +147,7 @@ impl> Plot let cursor = g.canvas.get_cursor_in_screen_space(); if ScreenRectangle::top_left(self.top_left, self.dims).contains(cursor) { let radius = Distance::meters(5.0); - let mut txt = Text::new().with_bg(); + let mut txt = Text::new().bg(Color::grey(0.6)); for (label, pt, _) in self.closest.all_close_pts( Pt2D::new(cursor.x - self.top_left.x, cursor.y - self.top_left.y), radius, @@ -155,8 +175,9 @@ impl> Plot impl Plot { pub fn new_usize(series: Vec>, ctx: &EventCtx) -> ManagedWidget { - let (plot, x_axis, y_axis) = Plot::new(series, 0, ctx); + let (plot, legend, x_axis, y_axis) = Plot::new(series, 0, ctx); ManagedWidget::col(vec![ + legend, ManagedWidget::row(vec![ y_axis.evenly_spaced(), ManagedWidget::usize_plot(plot), @@ -168,8 +189,9 @@ impl Plot { impl Plot { pub fn new_duration(series: Vec>, ctx: &EventCtx) -> ManagedWidget { - let (plot, x_axis, y_axis) = Plot::new(series, Duration::ZERO, ctx); + let (plot, legend, x_axis, y_axis) = Plot::new(series, Duration::ZERO, ctx); ManagedWidget::col(vec![ + legend, ManagedWidget::row(vec![ y_axis.evenly_spaced(), ManagedWidget::duration_plot(plot),