From bc990c87c98e7fc19d866161481a036367635366 Mon Sep 17 00:00:00 2001 From: bolphen Date: Fri, 29 Sep 2023 13:02:31 +0000 Subject: [PATCH 1/3] Increase WPM y-label range to at least 5 --- src/ui.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ui.rs b/src/ui.rs index 3d809f2..c738be6 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -307,6 +307,9 @@ impl ThemedWidget for &results::Results { .style(theme.results_chart) .data(&wpm_sma)]; + let y_label_min = wpm_sma_min as u16; + let y_label_max = (wpm_sma_max as u16).max(y_label_min + 6); + let wpm_chart = Chart::new(wpm_datasets) .block(Block::default().title(vec![Span::styled("Chart", theme.title)])) .x_axis( @@ -322,7 +325,7 @@ impl ThemedWidget for &results::Results { )) .bounds([wpm_sma_min, wpm_sma_max]) .labels( - (wpm_sma_min as u16..wpm_sma_max as u16) + (y_label_min..y_label_max) .step_by(5) .map(|n| Span::raw(format!("{}", n))) .collect(), From c25b98cac3d980298271bc1913a6d319b445ce0d Mon Sep 17 00:00:00 2001 From: bolphen Date: Fri, 29 Sep 2023 14:04:31 +0000 Subject: [PATCH 2/3] Ignore the case where the WPM vector is empty --- src/ui.rs | 84 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/src/ui.rs b/src/ui.rs index c738be6..9c6c599 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -291,47 +291,49 @@ impl ThemedWidget for &results::Results { }) .collect(); - let wpm_sma_min = wpm_sma - .iter() - .map(|(_, x)| x) - .fold(f64::INFINITY, |a, &b| a.min(b)); - let wpm_sma_max = wpm_sma - .iter() - .map(|(_, x)| x) - .fold(f64::NEG_INFINITY, |a, &b| a.max(b)); - - let wpm_datasets = vec![Dataset::default() - .name("WPM") - .marker(Marker::Braille) - .graph_type(GraphType::Line) - .style(theme.results_chart) - .data(&wpm_sma)]; - - let y_label_min = wpm_sma_min as u16; - let y_label_max = (wpm_sma_max as u16).max(y_label_min + 6); - - let wpm_chart = Chart::new(wpm_datasets) - .block(Block::default().title(vec![Span::styled("Chart", theme.title)])) - .x_axis( - Axis::default() - .title(Span::styled("Keypresses", theme.results_chart_x)) - .bounds([0.0, self.timing.per_event.len() as f64]), - ) - .y_axis( - Axis::default() - .title(Span::styled( - "WPM (10-keypress rolling average)", - theme.results_chart_y, - )) - .bounds([wpm_sma_min, wpm_sma_max]) - .labels( - (y_label_min..y_label_max) - .step_by(5) - .map(|n| Span::raw(format!("{}", n))) - .collect(), - ), - ); - wpm_chart.render(res_chunks[1], buf); + if !wpm_sma.is_empty() { // In case there's a chart to render + let wpm_sma_min = wpm_sma + .iter() + .map(|(_, x)| x) + .fold(f64::INFINITY, |a, &b| a.min(b)); + let wpm_sma_max = wpm_sma + .iter() + .map(|(_, x)| x) + .fold(f64::NEG_INFINITY, |a, &b| a.max(b)); + + let wpm_datasets = vec![Dataset::default() + .name("WPM") + .marker(Marker::Braille) + .graph_type(GraphType::Line) + .style(theme.results_chart) + .data(&wpm_sma)]; + + let y_label_min = wpm_sma_min as u16; + let y_label_max = (wpm_sma_max as u16).max(y_label_min + 6); + + let wpm_chart = Chart::new(wpm_datasets) + .block(Block::default().title(vec![Span::styled("Chart", theme.title)])) + .x_axis( + Axis::default() + .title(Span::styled("Keypresses", theme.results_chart_x)) + .bounds([0.0, self.timing.per_event.len() as f64]), + ) + .y_axis( + Axis::default() + .title(Span::styled( + "WPM (10-keypress rolling average)", + theme.results_chart_y, + )) + .bounds([wpm_sma_min, wpm_sma_max]) + .labels( + (y_label_min..y_label_max) + .step_by(5) + .map(|n| Span::raw(format!("{}", n))) + .collect(), + ), + ); + wpm_chart.render(res_chunks[1], buf); + } } } From 2fee3639effdd26744d78abf0179bf8565064cb9 Mon Sep 17 00:00:00 2001 From: Max Niederman Date: Sat, 30 Sep 2023 12:32:45 -0700 Subject: [PATCH 3/3] style(ui): change comment on chart rendering code --- src/ui.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ui.rs b/src/ui.rs index 9c6c599..9b736f3 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -291,7 +291,8 @@ impl ThemedWidget for &results::Results { }) .collect(); - if !wpm_sma.is_empty() { // In case there's a chart to render + // Render the chart if possible + if !wpm_sma.is_empty() { let wpm_sma_min = wpm_sma .iter() .map(|(_, x)| x)