Skip to content

Commit

Permalink
Refactor chart plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
senier authored and treiher committed Nov 28, 2024
1 parent 11bd933 commit 6260db4
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 368 deletions.
437 changes: 189 additions & 248 deletions frontend/src/ui/common.rs

Large diffs are not rendered by default.

35 changes: 19 additions & 16 deletions frontend/src/ui/page/body_fat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,30 +742,33 @@ fn view_chart(model: &Model, data_model: &data::Model) -> Node<Msg> {
("Weight (kg)", common::COLOR_BODY_WEIGHT),
]
.as_slice(),
common::plot_dual_line_chart(
common::plot_chart(
&[
(
body_fat
common::PlotData {
values: body_weight
.iter()
.map(|bw| (bw.date, bw.weight))
.collect::<Vec<_>>(),
plots: common::plot_line_with_dots(common::COLOR_BODY_WEIGHT),
params: common::PlotParams::SECONDARY,
},
common::PlotData {
values: body_fat
.iter()
.filter_map(|bf| bf.jp3(sex).map(|jp3| (bf.date, jp3)))
.collect::<Vec<_>>(),
common::COLOR_BODY_FAT_JP3,
),
(
body_fat
plots: common::plot_line_with_dots(common::COLOR_BODY_FAT_JP3),
params: common::PlotParams::default(),
},
common::PlotData {
values: body_fat
.iter()
.filter_map(|bf| bf.jp7(sex).map(|jp7| (bf.date, jp7)))
.collect::<Vec<_>>(),
common::COLOR_BODY_FAT_JP7,
),
plots: common::plot_line_with_dots(common::COLOR_BODY_FAT_JP7),
params: common::PlotParams::default(),
},
],
&[(
body_weight
.iter()
.map(|bw| (bw.date, bw.weight))
.collect::<Vec<_>>(),
common::COLOR_BODY_WEIGHT,
)],
model.interval.first,
model.interval.last,
data_model.theme(),
Expand Down
22 changes: 11 additions & 11 deletions frontend/src/ui/page/body_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,35 +361,35 @@ fn view_chart(model: &Model, data_model: &data::Model) -> Node<Msg> {
("Avg. weight (kg)", common::COLOR_AVG_BODY_WEIGHT),
]
.as_slice(),
common::plot_line_chart(
common::plot_chart(
&[
(
data_model
common::PlotData {
values: data_model
.body_weight
.values()
.filter(|bw| {
bw.date >= model.interval.first && bw.date <= model.interval.last
})
.map(|bw| (bw.date, bw.weight))
.collect::<Vec<_>>(),
common::COLOR_BODY_WEIGHT,
),
(
data_model
plots: common::plot_line_with_dots(common::COLOR_BODY_WEIGHT),
params: common::PlotParams::default(),
},
common::PlotData {
values: data_model
.body_weight_stats
.values()
.filter(|bws| {
bws.date >= model.interval.first && bws.date <= model.interval.last
})
.filter_map(|bws| bws.avg_weight.map(|avg_weight| (bws.date, avg_weight)))
.collect::<Vec<_>>(),
common::COLOR_AVG_BODY_WEIGHT,
),
plots: common::plot_line_with_dots(common::COLOR_AVG_BODY_WEIGHT),
params: common::PlotParams::default(),
},
],
model.interval.first,
model.interval.last,
None,
None,
data_model.theme(),
),
true,
Expand Down
89 changes: 41 additions & 48 deletions frontend/src/ui/page/exercise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,21 +501,22 @@ pub fn view_charts<Ms>(

let mut labels = vec![("Repetitions", common::COLOR_REPS)];

let mut data = vec![(
reps_rpe
let mut data = vec![common::PlotData {
values: reps_rpe
.iter()
.map(|(date, (avg_reps, _))| {
#[allow(clippy::cast_precision_loss)]
(*date, avg_reps.iter().sum::<f32>() / avg_reps.len() as f32)
})
.collect::<Vec<_>>(),
common::COLOR_REPS,
)];
plots: common::plot_line_with_dots(common::COLOR_REPS),
params: common::PlotParams::primary_range(0., 10.),
}];

if show_rpe {
labels.push(("+ Repetitions in reserve", common::COLOR_REPS_RIR));
data.push((
reps_rpe
data.push(common::PlotData {
values: reps_rpe
.into_iter()
.filter_map(|(date, (avg_reps_values, avg_rpe_values))| {
#[allow(clippy::cast_precision_loss)]
Expand All @@ -530,105 +531,97 @@ pub fn view_charts<Ms>(
}
})
.collect::<Vec<_>>(),
common::COLOR_REPS_RIR,
));
plots: common::plot_line_with_dots(common::COLOR_REPS_RIR),
params: common::PlotParams::primary_range(0., 10.),
});
}

nodes![
common::view_chart(
&[("Set volume", common::COLOR_SET_VOLUME)],
common::plot_line_chart(
&[(
set_volume.into_iter().collect::<Vec<_>>(),
common::COLOR_SET_VOLUME,
)],
common::plot_chart(
&[common::PlotData {
values: set_volume.into_iter().collect::<Vec<_>>(),
plots: common::plot_line_with_dots(common::COLOR_SET_VOLUME),
params: common::PlotParams::primary_range(0., 10.),
}],
interval.first,
interval.last,
Some(0.),
Some(10.),
theme,
),
false,
),
common::view_chart(
&[("Volume load", common::COLOR_VOLUME_LOAD)],
common::plot_line_chart(
&[(
volume_load.into_iter().collect::<Vec<_>>(),
common::COLOR_VOLUME_LOAD,
)],
common::plot_chart(
&[common::PlotData {
values: volume_load.into_iter().collect::<Vec<_>>(),
plots: common::plot_line_with_dots(common::COLOR_VOLUME_LOAD),
params: common::PlotParams::primary_range(0., 10.),
}],
interval.first,
interval.last,
Some(0.),
Some(10.),
theme,
),
false,
),
IF![show_tut =>
common::view_chart(
&[("Time under tension (s)", common::COLOR_TUT)],
common::plot_line_chart(
&[(tut.into_iter().collect::<Vec<_>>(), common::COLOR_TUT,)],
common::plot_chart(
&[common::PlotData {
values: tut.into_iter().collect::<Vec<_>>(),
plots: common::plot_line_with_dots(common::COLOR_TUT),
params: common::PlotParams::primary_range(0., 10.),
}],
interval.first,
interval.last,
Some(0.),
Some(10.),
theme,
),
false,
)
],
common::view_chart(
&labels,
common::plot_line_chart(
&data,
interval.first,
interval.last,
Some(0.),
Some(10.),
theme,
),
common::plot_chart(&data, interval.first, interval.last, theme),
false,
),
common::view_chart(
&[("Weight (kg)", common::COLOR_WEIGHT)],
common::plot_line_chart(
&[(
weight
common::plot_chart(
&[common::PlotData {
values: weight
.into_iter()
.map(|(date, values)| {
#[allow(clippy::cast_precision_loss)]
(date, values.iter().sum::<f32>() / values.len() as f32)
})
.collect::<Vec<_>>(),
common::COLOR_WEIGHT,
)],
plots: common::plot_line_with_dots(common::COLOR_WEIGHT),
params: common::PlotParams::primary_range(0., 10.),
}],
interval.first,
interval.last,
Some(0.),
Some(10.),
theme,
),
false,
),
IF![show_tut =>
common::view_chart(
&[("Time (s)", common::COLOR_TIME)],
common::plot_line_chart(
&[(
time.into_iter()
common::plot_chart(
&[common::PlotData{
values: time.into_iter()
.map(|(date, values)| {
#[allow(clippy::cast_precision_loss)]
(date, values.iter().sum::<f32>() / values.len() as f32)
})
.collect::<Vec<_>>(),
common::COLOR_TIME,
)],
plots: common::plot_line_with_dots(common::COLOR_TIME),
params: common::PlotParams::primary_range(0., 10.)
}],
interval.first,
interval.last,
Some(0.),
Some(10.),
theme,
),
false,
Expand Down
14 changes: 6 additions & 8 deletions frontend/src/ui/page/menstrual_cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,19 +345,17 @@ fn view_chart(model: &Model, data_model: &data::Model) -> Node<Msg> {

common::view_chart(
vec![("Intensity", common::COLOR_PERIOD_INTENSITY)].as_slice(),
common::plot_bar_chart(
&[(
period
common::plot_chart(
&[common::PlotData {
values: period
.iter()
.map(|p| (p.date, f32::from(p.intensity)))
.collect::<Vec<_>>(),
common::COLOR_PERIOD_INTENSITY,
)],
&[],
plots: [common::PlotType::Histogram(common::COLOR_PERIOD_INTENSITY)].to_vec(),
params: common::PlotParams::primary_range(0., 4.),
}],
model.interval.first,
model.interval.last,
Some(0.),
Some(4.),
data_model.theme(),
),
true,
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/ui/page/muscles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ pub fn view(model: &Model, data_model: &data::Model) -> Node<Msg> {
],
common::view_chart(
&[("Set volume (weekly total)", common::COLOR_SET_VOLUME)],
common::plot_line_chart(
&[(set_volume, common::COLOR_SET_VOLUME)],
common::plot_chart(
&[common::PlotData {
values: set_volume,
plots: common::plot_line_with_dots(common::COLOR_SET_VOLUME),
params: common::PlotParams::primary_range(0., 10.),
}],
model.interval.first,
model.interval.last,
Some(0.),
Some(10.),
data_model.theme()
),
true,
Expand Down
36 changes: 18 additions & 18 deletions frontend/src/ui/page/routine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1338,27 +1338,28 @@ pub fn view_charts<Ms>(
nodes![
common::view_chart(
&[("Load", common::COLOR_LOAD)],
common::plot_line_chart(
&[(load.into_iter().collect::<Vec<_>>(), common::COLOR_LOAD)],
common::plot_chart(
&[common::PlotData {
values: load.into_iter().collect::<Vec<_>>(),
plots: common::plot_line_with_dots(common::COLOR_LOAD),
params: common::PlotParams::primary_range(0., 10.),
}],
interval.first,
interval.last,
Some(0.),
Some(10.),
theme,
),
false,
),
common::view_chart(
&[("Set volume", common::COLOR_SET_VOLUME)],
common::plot_line_chart(
&[(
set_volume.into_iter().collect::<Vec<_>>(),
common::COLOR_SET_VOLUME,
)],
common::plot_chart(
&[common::PlotData {
values: set_volume.into_iter().collect::<Vec<_>>(),
plots: common::plot_line_with_dots(common::COLOR_SET_VOLUME),
params: common::PlotParams::primary_range(0., 10.),
}],
interval.first,
interval.last,
Some(0.),
Some(10.),
theme,
),
false,
Expand All @@ -1367,9 +1368,9 @@ pub fn view_charts<Ms>(
show_rpe =>
common::view_chart(
&[("RPE", common::COLOR_RPE)],
common::plot_line_chart(
&[(
rpe.into_iter()
common::plot_chart(
&[common::PlotData{
values: rpe.into_iter()
.map(|(date, values)| {
#[allow(clippy::cast_precision_loss)]
(
Expand All @@ -1382,12 +1383,11 @@ pub fn view_charts<Ms>(
)
})
.collect::<Vec<_>>(),
common::COLOR_RPE,
)],
plots: common::plot_line_with_dots(common::COLOR_RPE),
params: common::PlotParams::primary_range(5., 10.)
}],
interval.first,
interval.last,
Some(5.),
Some(10.),
theme,
),
false,
Expand Down
Loading

0 comments on commit 6260db4

Please sign in to comment.