Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve formatting of numbers on plot Y axis #5753

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/re_log_types/src/time_point/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl TimeType {
} else {
match self {
Self::Time => Time::from(time_int).format(time_zone_for_timestamps),
Self::Sequence => format!("#{}", time_int.0),
Self::Sequence => format!("#{}", re_format::format_i64(time_int.0)),
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions crates/re_space_view_time_series/src/space_view_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ It can greatly improve performance (and readability) in such situations as it pr
time_zone_for_timestamps,
)
})
.y_axis_formatter(move |mark, _, _| format_y_axis(mark))
.y_axis_width(3) // in digits
.label_formatter(move |name, value| {
let name = if name.is_empty() { "y" } else { name };
Expand All @@ -412,16 +413,14 @@ It can greatly improve performance (and readability) in such situations as it pr
time_zone_for_timestamps,
);

let is_integer = value.y.round() == value.y;
let decimals = if is_integer { 0 } else { 5 };
let y_value = re_format::format_f64(value.y);

if aggregator == TimeSeriesAggregator::Off || aggregation_factor <= 1.0 {
format!("{timeline_name}: {label}\n{name}: {:.decimals$}", value.y)
format!("{timeline_name}: {label}\n{name}: {y_value}")
} else {
format!(
"{timeline_name}: {label}\n{name}: {:.decimals$}\n\
"{timeline_name}: {label}\n{name}: {y_value}\n\
{aggregator} aggregation over approx. {aggregation_factor:.1} time points",
value.y,
)
}
});
Expand Down Expand Up @@ -791,6 +790,15 @@ fn format_time(time_type: TimeType, time_int: i64, time_zone_for_timestamps: Tim
}
}

fn format_y_axis(mark: egui_plot::GridMark) -> String {
// Example: If the step to the next tick is `0.01`, we should use 2 decimals of precision:
let num_decimals = -mark.step_size.log10().round() as usize;

re_format::FloatFormatOptions::DEFAULT_f64
.with_decimals(num_decimals)
.format(mark.value)
}

fn ns_grid_spacer(
canvas_size: egui::Vec2,
input: &egui_plot::GridInput,
Expand Down
3 changes: 2 additions & 1 deletion crates/re_time_panel/src/paint_ticks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ fn paint_time_range_ticks(
|ns| Time::from_ns_since_epoch(ns).format_time_compact(time_zone_for_timestamps),
)
}

TimeType::Sequence => {
fn next_power_of_10(i: i64) -> i64 {
i * 10
Expand All @@ -91,7 +92,7 @@ fn paint_time_range_ticks(
&ui.clip_rect(),
time_range,
next_power_of_10,
|seq| format!("#{seq}"),
|seq| format!("#{}", re_format::format_i64(seq)),
)
}
}
Expand Down
Loading