Skip to content

Commit

Permalink
Omit chart if all values are zero
Browse files Browse the repository at this point in the history
  • Loading branch information
senier committed Sep 3, 2024
1 parent b5bbf70 commit fed192b
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 52 deletions.
110 changes: 71 additions & 39 deletions frontend/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,40 +589,53 @@ pub fn view_calendar<Ms>(entries: Vec<(NaiveDate, usize, f64)>, interval: &Inter

pub fn view_chart<Ms>(
labels: &[(&str, usize)],
chart: Result<String, Box<dyn std::error::Error>>,
chart: Result<Option<String>, Box<dyn std::error::Error>>,
no_data_label: bool,
) -> Node<Ms> {
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::<Vec<_>>(),
],
raw![&chart.unwrap_or_else(|err| {
error!("failed to plot chart:", err);
String::new()
})],
]
match chart {
Ok(result) => match result {
None => match no_data_label {
true => div![
C!["is-size-7"],
C!["block"],
C!["has-text-centered"],
C!["mb-4"],
"No data.".to_string(),
],
false => div![],
},
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::<Vec<_>>(),
],
raw![&value],
],
},
Err(err) => div![raw![&format!("failed to plot chart: {}", err.to_string())]],
}
}

pub fn plot_line_chart(
Expand All @@ -632,7 +645,11 @@ pub fn plot_line_chart(
y_min_opt: Option<f32>,
y_max_opt: Option<f32>,
theme: &data::Theme,
) -> Result<String, Box<dyn std::error::Error>> {
) -> Result<Option<String>, Box<dyn std::error::Error>> {
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))
Expand Down Expand Up @@ -691,7 +708,7 @@ pub fn plot_line_chart(
root.present()?;
}

Ok(result)
Ok(Some(result))
}

pub fn plot_dual_line_chart(
Expand All @@ -700,7 +717,11 @@ pub fn plot_dual_line_chart(
x_min: NaiveDate,
x_max: NaiveDate,
theme: &data::Theme,
) -> Result<String, Box<dyn std::error::Error>> {
) -> Result<Option<String>, Box<dyn std::error::Error>> {
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))
Expand Down Expand Up @@ -787,7 +808,7 @@ pub fn plot_dual_line_chart(
root.present()?;
}

Ok(result)
Ok(Some(result))
}

pub fn plot_bar_chart(
Expand All @@ -798,7 +819,11 @@ pub fn plot_bar_chart(
y_min_opt: Option<f32>,
y_max_opt: Option<f32>,
theme: &data::Theme,
) -> Result<String, Box<dyn std::error::Error>> {
) -> Result<Option<String>, Box<dyn std::error::Error>> {
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))
Expand Down Expand Up @@ -884,7 +909,14 @@ pub fn plot_bar_chart(
root.present()?;
}

Ok(result)
Ok(Some(result))
}

fn all_zeros(data: &[(Vec<(NaiveDate, f32)>, usize)]) -> bool {
return data
.iter()
.map(|p| p.0.iter().map(|s| s.1 == 0.0).fold(true, |r, v| r && v))
.fold(true, |r, v| r && v);
}

fn colors(theme: &data::Theme) -> (RGBColor, RGBColor) {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/page/body_fat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ fn view_chart(model: &Model, data_model: &data::Model) -> Node<Msg> {
model.interval.last,
data_model.theme(),
),
true,
)
}

Expand Down
1 change: 1 addition & 0 deletions frontend/src/page/body_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ fn view_chart(model: &Model, data_model: &data::Model) -> Node<Msg> {
None,
data_model.theme(),
),
true,
)
}

Expand Down
18 changes: 12 additions & 6 deletions frontend/src/page/exercise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ pub fn view_charts<Ms>(
Some(0.),
Some(10.),
theme,
)
),
false,
),
common::view_chart(
&[("Volume load", common::COLOR_VOLUME_LOAD)],
Expand All @@ -513,7 +514,8 @@ pub fn view_charts<Ms>(
Some(0.),
Some(10.),
theme,
)
),
false,
),
common::view_chart(
&[("Time under tension (s)", common::COLOR_TUT)],
Expand All @@ -524,7 +526,8 @@ pub fn view_charts<Ms>(
Some(0.),
Some(10.),
theme,
)
),
false,
),
common::view_chart(
&[
Expand Down Expand Up @@ -568,7 +571,8 @@ pub fn view_charts<Ms>(
Some(0.),
Some(10.),
theme,
)
),
false,
),
common::view_chart(
&[("Weight (kg)", common::COLOR_WEIGHT)],
Expand All @@ -588,7 +592,8 @@ pub fn view_charts<Ms>(
Some(0.),
Some(10.),
theme,
)
),
false,
),
common::view_chart(
&[("Time (s)", common::COLOR_TIME)],
Expand All @@ -607,7 +612,8 @@ pub fn view_charts<Ms>(
Some(0.),
Some(10.),
theme,
)
),
false,
),
]
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/page/menstrual_cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ fn view_chart(model: &Model, data_model: &data::Model) -> Node<Msg> {
Some(4.),
data_model.theme(),
),
true,
)
}

Expand Down
3 changes: 2 additions & 1 deletion frontend/src/page/muscles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ pub fn view(model: &Model, data_model: &data::Model) -> Node<Msg> {
Some(0.),
Some(10.),
data_model.theme()
)
),
true,
)
]
})
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/page/routine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,8 @@ pub fn view_charts<Ms>(
Some(0.),
Some(10.),
theme,
)
),
false,
),
common::view_chart(
&[("Set volume", common::COLOR_SET_VOLUME)],
Expand All @@ -1359,7 +1360,8 @@ pub fn view_charts<Ms>(
Some(0.),
Some(10.),
theme,
)
),
false,
),
common::view_chart(
&[("RPE", common::COLOR_RPE)],
Expand All @@ -1385,7 +1387,8 @@ pub fn view_charts<Ms>(
Some(5.),
Some(10.),
theme,
)
),
false,
),
]
}
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/page/training.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,8 @@ pub fn view_charts<Ms>(
Some(0.),
Some(10.),
theme,
)
),
false,
),
common::view_chart(
&[("Set volume (weekly total)", common::COLOR_SET_VOLUME)],
Expand All @@ -548,7 +549,8 @@ pub fn view_charts<Ms>(
Some(0.),
Some(10.),
theme,
)
),
false,
),
common::view_chart(
&[("RPE (weekly average)", common::COLOR_RPE)],
Expand All @@ -559,7 +561,8 @@ pub fn view_charts<Ms>(
Some(5.),
Some(10.),
theme,
)
),
false,
),
]
}
Expand Down

0 comments on commit fed192b

Please sign in to comment.