diff --git a/CHANGELOG.md b/CHANGELOG.md index a107024..03cb8ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Use term RPE instead of intensity - Improve display of charts when all values are zero - Background color of sections on routine page and training session page +- Omit charts that contain no data ### Fixed diff --git a/frontend/src/common.rs b/frontend/src/common.rs index 5370168..adf326f 100644 --- a/frontend/src/common.rs +++ b/frontend/src/common.rs @@ -589,40 +589,51 @@ pub fn view_calendar(entries: Vec<(NaiveDate, usize, f64)>, interval: &Inter pub fn view_chart( labels: &[(&str, usize)], - chart: Result>, + chart: Result, Box>, + no_data_label: bool, ) -> Node { - div![ - C!["container"], - C!["has-text-centered"], - h1![ - C!["is-size-6"], - C!["has-text-weight-bold"], - labels - .iter() - .map(|(label, color_idx)| { - span![ - C!["icon-text"], - C!["mx-1"], - span![ - C!["icon"], - style![ - St::Color => { - let (r, g, b) = Palette99::pick(*color_idx).mix(0.9).rgb(); - format!("#{r:02x}{g:02x}{b:02x}") - } - ], - i![C!["fas fa-square"]] - ], - span![label], - ] - }) - .collect::>(), - ], - raw![&chart.unwrap_or_else(|err| { - error!("failed to plot chart:", err); - String::new() - })], - ] + match chart { + Ok(result) => match result { + None => if no_data_label { + div![ + C!["is-size-7"], + C!["block"], + C!["has-text-centered"], + C!["mb-4"], + "No data.".to_string(), + ] } else { empty![] }, + Some(value) => div![ + C!["container"], + C!["has-text-centered"], + h1![ + C!["is-size-6"], + C!["has-text-weight-bold"], + labels + .iter() + .map(|(label, color_idx)| { + span![ + C!["icon-text"], + C!["mx-1"], + span![ + C!["icon"], + style![ + St::Color => { + let (r, g, b) = Palette99::pick(*color_idx).mix(0.9).rgb(); + format!("#{r:02x}{g:02x}{b:02x}") + } + ], + i![C!["fas fa-square"]] + ], + span![label], + ] + }) + .collect::>(), + ], + raw![&value], + ], + }, + Err(err) => div![raw![&format!("failed to plot chart: {err}")]], + } } pub fn plot_line_chart( @@ -632,7 +643,11 @@ pub fn plot_line_chart( y_min_opt: Option, y_max_opt: Option, theme: &data::Theme, -) -> Result> { +) -> Result, Box> { + if all_zeros(data) { + return Ok(None); + } + let (y_min, y_max, y_margin) = determine_y_bounds( data.iter() .flat_map(|(s, _)| s.iter().map(|(_, y)| *y)) @@ -691,7 +706,7 @@ pub fn plot_line_chart( root.present()?; } - Ok(result) + Ok(Some(result)) } pub fn plot_dual_line_chart( @@ -700,7 +715,11 @@ pub fn plot_dual_line_chart( x_min: NaiveDate, x_max: NaiveDate, theme: &data::Theme, -) -> Result> { +) -> Result, Box> { + if all_zeros(data) && all_zeros(secondary_data) { + return Ok(None); + } + let (y1_min, y1_max, y1_margin) = determine_y_bounds( data.iter() .flat_map(|(s, _)| s.iter().map(|(_, y)| *y)) @@ -787,7 +806,7 @@ pub fn plot_dual_line_chart( root.present()?; } - Ok(result) + Ok(Some(result)) } pub fn plot_bar_chart( @@ -798,7 +817,11 @@ pub fn plot_bar_chart( y_min_opt: Option, y_max_opt: Option, theme: &data::Theme, -) -> Result> { +) -> Result, Box> { + if all_zeros(data) && all_zeros(secondary_data) { + return Ok(None); + } + let (y1_min, y1_max, _) = determine_y_bounds( data.iter() .flat_map(|(s, _)| s.iter().map(|(_, y)| *y)) @@ -884,7 +907,11 @@ pub fn plot_bar_chart( root.present()?; } - Ok(result) + Ok(Some(result)) +} + +fn all_zeros(data: &[(Vec<(NaiveDate, f32)>, usize)]) -> bool { + return data.iter().all(|p| p.0.iter().all(|s| s.1 == 0.0)); } fn colors(theme: &data::Theme) -> (RGBColor, RGBColor) { diff --git a/frontend/src/page/body_fat.rs b/frontend/src/page/body_fat.rs index 3865e64..873e75e 100644 --- a/frontend/src/page/body_fat.rs +++ b/frontend/src/page/body_fat.rs @@ -771,6 +771,7 @@ fn view_chart(model: &Model, data_model: &data::Model) -> Node { model.interval.last, data_model.theme(), ), + true, ) } diff --git a/frontend/src/page/body_weight.rs b/frontend/src/page/body_weight.rs index 4dcabcf..c739b32 100644 --- a/frontend/src/page/body_weight.rs +++ b/frontend/src/page/body_weight.rs @@ -393,6 +393,7 @@ fn view_chart(model: &Model, data_model: &data::Model) -> Node { None, data_model.theme(), ), + true, ) } diff --git a/frontend/src/page/exercise.rs b/frontend/src/page/exercise.rs index aa60382..53b8271 100644 --- a/frontend/src/page/exercise.rs +++ b/frontend/src/page/exercise.rs @@ -499,7 +499,8 @@ pub fn view_charts( Some(0.), Some(10.), theme, - ) + ), + false, ), common::view_chart( &[("Volume load", common::COLOR_VOLUME_LOAD)], @@ -513,7 +514,8 @@ pub fn view_charts( Some(0.), Some(10.), theme, - ) + ), + false, ), common::view_chart( &[("Time under tension (s)", common::COLOR_TUT)], @@ -524,7 +526,8 @@ pub fn view_charts( Some(0.), Some(10.), theme, - ) + ), + false, ), common::view_chart( &[ @@ -568,7 +571,8 @@ pub fn view_charts( Some(0.), Some(10.), theme, - ) + ), + false, ), common::view_chart( &[("Weight (kg)", common::COLOR_WEIGHT)], @@ -588,7 +592,8 @@ pub fn view_charts( Some(0.), Some(10.), theme, - ) + ), + false, ), common::view_chart( &[("Time (s)", common::COLOR_TIME)], @@ -607,7 +612,8 @@ pub fn view_charts( Some(0.), Some(10.), theme, - ) + ), + false, ), ] } diff --git a/frontend/src/page/menstrual_cycle.rs b/frontend/src/page/menstrual_cycle.rs index 1118c58..3c1c99f 100644 --- a/frontend/src/page/menstrual_cycle.rs +++ b/frontend/src/page/menstrual_cycle.rs @@ -361,6 +361,7 @@ fn view_chart(model: &Model, data_model: &data::Model) -> Node { Some(4.), data_model.theme(), ), + true, ) } diff --git a/frontend/src/page/muscles.rs b/frontend/src/page/muscles.rs index 26354f4..94b36e1 100644 --- a/frontend/src/page/muscles.rs +++ b/frontend/src/page/muscles.rs @@ -103,7 +103,8 @@ pub fn view(model: &Model, data_model: &data::Model) -> Node { Some(0.), Some(10.), data_model.theme() - ) + ), + true, ) ] }) diff --git a/frontend/src/page/routine.rs b/frontend/src/page/routine.rs index 149035a..830f839 100644 --- a/frontend/src/page/routine.rs +++ b/frontend/src/page/routine.rs @@ -1345,7 +1345,8 @@ pub fn view_charts( Some(0.), Some(10.), theme, - ) + ), + false, ), common::view_chart( &[("Set volume", common::COLOR_SET_VOLUME)], @@ -1359,7 +1360,8 @@ pub fn view_charts( Some(0.), Some(10.), theme, - ) + ), + false, ), common::view_chart( &[("RPE", common::COLOR_RPE)], @@ -1385,7 +1387,8 @@ pub fn view_charts( Some(5.), Some(10.), theme, - ) + ), + false, ), ] } diff --git a/frontend/src/page/training.rs b/frontend/src/page/training.rs index 4c803cb..50637ec 100644 --- a/frontend/src/page/training.rs +++ b/frontend/src/page/training.rs @@ -537,7 +537,8 @@ pub fn view_charts( Some(0.), Some(10.), theme, - ) + ), + false, ), common::view_chart( &[("Set volume (weekly total)", common::COLOR_SET_VOLUME)], @@ -548,7 +549,8 @@ pub fn view_charts( Some(0.), Some(10.), theme, - ) + ), + false, ), common::view_chart( &[("RPE (weekly average)", common::COLOR_RPE)], @@ -559,7 +561,8 @@ pub fn view_charts( Some(5.), Some(10.), theme, - ) + ), + false, ), ] }