Skip to content

Commit 4e6c1ac

Browse files
committed
Omit chart if all values are zero
1 parent b5bbf70 commit 4e6c1ac

File tree

8 files changed

+98
-52
lines changed

8 files changed

+98
-52
lines changed

frontend/src/common.rs

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -589,40 +589,51 @@ pub fn view_calendar<Ms>(entries: Vec<(NaiveDate, usize, f64)>, interval: &Inter
589589

590590
pub fn view_chart<Ms>(
591591
labels: &[(&str, usize)],
592-
chart: Result<String, Box<dyn std::error::Error>>,
592+
chart: Result<Option<String>, Box<dyn std::error::Error>>,
593+
no_data_label: bool,
593594
) -> Node<Ms> {
594-
div![
595-
C!["container"],
596-
C!["has-text-centered"],
597-
h1![
598-
C!["is-size-6"],
599-
C!["has-text-weight-bold"],
600-
labels
601-
.iter()
602-
.map(|(label, color_idx)| {
603-
span![
604-
C!["icon-text"],
605-
C!["mx-1"],
606-
span![
607-
C!["icon"],
608-
style![
609-
St::Color => {
610-
let (r, g, b) = Palette99::pick(*color_idx).mix(0.9).rgb();
611-
format!("#{r:02x}{g:02x}{b:02x}")
612-
}
613-
],
614-
i![C!["fas fa-square"]]
615-
],
616-
span![label],
617-
]
618-
})
619-
.collect::<Vec<_>>(),
620-
],
621-
raw![&chart.unwrap_or_else(|err| {
622-
error!("failed to plot chart:", err);
623-
String::new()
624-
})],
625-
]
595+
match chart {
596+
Ok(result) => match result {
597+
None => if no_data_label {
598+
div![
599+
C!["is-size-7"],
600+
C!["block"],
601+
C!["has-text-centered"],
602+
C!["mb-4"],
603+
"No data.".to_string(),
604+
] } else { div![] },
605+
Some(value) => div![
606+
C!["container"],
607+
C!["has-text-centered"],
608+
h1![
609+
C!["is-size-6"],
610+
C!["has-text-weight-bold"],
611+
labels
612+
.iter()
613+
.map(|(label, color_idx)| {
614+
span![
615+
C!["icon-text"],
616+
C!["mx-1"],
617+
span![
618+
C!["icon"],
619+
style![
620+
St::Color => {
621+
let (r, g, b) = Palette99::pick(*color_idx).mix(0.9).rgb();
622+
format!("#{r:02x}{g:02x}{b:02x}")
623+
}
624+
],
625+
i![C!["fas fa-square"]]
626+
],
627+
span![label],
628+
]
629+
})
630+
.collect::<Vec<_>>(),
631+
],
632+
raw![&value],
633+
],
634+
},
635+
Err(err) => div![raw![&format!("failed to plot chart: {err}")]],
636+
}
626637
}
627638

628639
pub fn plot_line_chart(
@@ -632,7 +643,11 @@ pub fn plot_line_chart(
632643
y_min_opt: Option<f32>,
633644
y_max_opt: Option<f32>,
634645
theme: &data::Theme,
635-
) -> Result<String, Box<dyn std::error::Error>> {
646+
) -> Result<Option<String>, Box<dyn std::error::Error>> {
647+
if all_zeros(data) {
648+
return Ok(None);
649+
}
650+
636651
let (y_min, y_max, y_margin) = determine_y_bounds(
637652
data.iter()
638653
.flat_map(|(s, _)| s.iter().map(|(_, y)| *y))
@@ -691,7 +706,7 @@ pub fn plot_line_chart(
691706
root.present()?;
692707
}
693708

694-
Ok(result)
709+
Ok(Some(result))
695710
}
696711

697712
pub fn plot_dual_line_chart(
@@ -700,7 +715,11 @@ pub fn plot_dual_line_chart(
700715
x_min: NaiveDate,
701716
x_max: NaiveDate,
702717
theme: &data::Theme,
703-
) -> Result<String, Box<dyn std::error::Error>> {
718+
) -> Result<Option<String>, Box<dyn std::error::Error>> {
719+
if all_zeros(data) && all_zeros(secondary_data) {
720+
return Ok(None);
721+
}
722+
704723
let (y1_min, y1_max, y1_margin) = determine_y_bounds(
705724
data.iter()
706725
.flat_map(|(s, _)| s.iter().map(|(_, y)| *y))
@@ -787,7 +806,7 @@ pub fn plot_dual_line_chart(
787806
root.present()?;
788807
}
789808

790-
Ok(result)
809+
Ok(Some(result))
791810
}
792811

793812
pub fn plot_bar_chart(
@@ -798,7 +817,11 @@ pub fn plot_bar_chart(
798817
y_min_opt: Option<f32>,
799818
y_max_opt: Option<f32>,
800819
theme: &data::Theme,
801-
) -> Result<String, Box<dyn std::error::Error>> {
820+
) -> Result<Option<String>, Box<dyn std::error::Error>> {
821+
if all_zeros(data) && all_zeros(secondary_data) {
822+
return Ok(None);
823+
}
824+
802825
let (y1_min, y1_max, _) = determine_y_bounds(
803826
data.iter()
804827
.flat_map(|(s, _)| s.iter().map(|(_, y)| *y))
@@ -884,7 +907,14 @@ pub fn plot_bar_chart(
884907
root.present()?;
885908
}
886909

887-
Ok(result)
910+
Ok(Some(result))
911+
}
912+
913+
fn all_zeros(data: &[(Vec<(NaiveDate, f32)>, usize)]) -> bool {
914+
return data
915+
.iter()
916+
.map(|p| p.0.iter().map(|s| s.1 == 0.0).all(|e| e))
917+
.all(|e| e);
888918
}
889919

890920
fn colors(theme: &data::Theme) -> (RGBColor, RGBColor) {

frontend/src/page/body_fat.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,7 @@ fn view_chart(model: &Model, data_model: &data::Model) -> Node<Msg> {
771771
model.interval.last,
772772
data_model.theme(),
773773
),
774+
true,
774775
)
775776
}
776777

frontend/src/page/body_weight.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ fn view_chart(model: &Model, data_model: &data::Model) -> Node<Msg> {
393393
None,
394394
data_model.theme(),
395395
),
396+
true,
396397
)
397398
}
398399

frontend/src/page/exercise.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,8 @@ pub fn view_charts<Ms>(
499499
Some(0.),
500500
Some(10.),
501501
theme,
502-
)
502+
),
503+
false,
503504
),
504505
common::view_chart(
505506
&[("Volume load", common::COLOR_VOLUME_LOAD)],
@@ -513,7 +514,8 @@ pub fn view_charts<Ms>(
513514
Some(0.),
514515
Some(10.),
515516
theme,
516-
)
517+
),
518+
false,
517519
),
518520
common::view_chart(
519521
&[("Time under tension (s)", common::COLOR_TUT)],
@@ -524,7 +526,8 @@ pub fn view_charts<Ms>(
524526
Some(0.),
525527
Some(10.),
526528
theme,
527-
)
529+
),
530+
false,
528531
),
529532
common::view_chart(
530533
&[
@@ -568,7 +571,8 @@ pub fn view_charts<Ms>(
568571
Some(0.),
569572
Some(10.),
570573
theme,
571-
)
574+
),
575+
false,
572576
),
573577
common::view_chart(
574578
&[("Weight (kg)", common::COLOR_WEIGHT)],
@@ -588,7 +592,8 @@ pub fn view_charts<Ms>(
588592
Some(0.),
589593
Some(10.),
590594
theme,
591-
)
595+
),
596+
false,
592597
),
593598
common::view_chart(
594599
&[("Time (s)", common::COLOR_TIME)],
@@ -607,7 +612,8 @@ pub fn view_charts<Ms>(
607612
Some(0.),
608613
Some(10.),
609614
theme,
610-
)
615+
),
616+
false,
611617
),
612618
]
613619
}

frontend/src/page/menstrual_cycle.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ fn view_chart(model: &Model, data_model: &data::Model) -> Node<Msg> {
361361
Some(4.),
362362
data_model.theme(),
363363
),
364+
true,
364365
)
365366
}
366367

frontend/src/page/muscles.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ pub fn view(model: &Model, data_model: &data::Model) -> Node<Msg> {
103103
Some(0.),
104104
Some(10.),
105105
data_model.theme()
106-
)
106+
),
107+
true,
107108
)
108109
]
109110
})

frontend/src/page/routine.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,8 @@ pub fn view_charts<Ms>(
13451345
Some(0.),
13461346
Some(10.),
13471347
theme,
1348-
)
1348+
),
1349+
false,
13491350
),
13501351
common::view_chart(
13511352
&[("Set volume", common::COLOR_SET_VOLUME)],
@@ -1359,7 +1360,8 @@ pub fn view_charts<Ms>(
13591360
Some(0.),
13601361
Some(10.),
13611362
theme,
1362-
)
1363+
),
1364+
false,
13631365
),
13641366
common::view_chart(
13651367
&[("RPE", common::COLOR_RPE)],
@@ -1385,7 +1387,8 @@ pub fn view_charts<Ms>(
13851387
Some(5.),
13861388
Some(10.),
13871389
theme,
1388-
)
1390+
),
1391+
false,
13891392
),
13901393
]
13911394
}

frontend/src/page/training.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,8 @@ pub fn view_charts<Ms>(
537537
Some(0.),
538538
Some(10.),
539539
theme,
540-
)
540+
),
541+
false,
541542
),
542543
common::view_chart(
543544
&[("Set volume (weekly total)", common::COLOR_SET_VOLUME)],
@@ -548,7 +549,8 @@ pub fn view_charts<Ms>(
548549
Some(0.),
549550
Some(10.),
550551
theme,
551-
)
552+
),
553+
false,
552554
),
553555
common::view_chart(
554556
&[("RPE (weekly average)", common::COLOR_RPE)],
@@ -559,7 +561,8 @@ pub fn view_charts<Ms>(
559561
Some(5.),
560562
Some(10.),
561563
theme,
562-
)
564+
),
565+
false,
563566
),
564567
]
565568
}

0 commit comments

Comments
 (0)