Skip to content

Commit

Permalink
Improve formatting of numbers on plot Y axis (#5753)
Browse files Browse the repository at this point in the history
### What
The formatting up numbers on the plot Y axis was really bad before,
showing very few digits of precision, and switching to scientific mode
really early.

Now it use up to 15 digits of precision and uses thousands separators
for clairy.

Sequence timelines (frames, log_tick, etc) are now also formatted with
thousands separators to make them easier to read when large.

#### Before
<img width="517" alt="Screenshot 2024-04-03 at 05 30 03"
src="https://github.com/rerun-io/rerun/assets/1148717/271a7f58-6d0a-4251-8d9b-4766b66404c9">

#### After
<img width="498" alt="Screenshot 2024-04-03 at 05 30 43"
src="https://github.com/rerun-io/rerun/assets/1148717/f82904dc-9441-4227-a194-94db67dd0cda">

<img width="71" alt="Screenshot 2024-04-03 at 05 31 28"
src="https://github.com/rerun-io/rerun/assets/1148717/4dd2b91f-4b0c-49e4-b8f6-1a7a00ed064f">

<img width="784" alt="Screenshot 2024-04-03 at 05 31 11"
src="https://github.com/rerun-io/rerun/assets/1148717/8ad3cf8f-ba3b-4948-ae28-d50f665bc459">


### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using newly built examples:
[rerun.io/viewer](https://rerun.io/viewer/pr/5753)
* Using examples from latest `main` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/5753?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/5753?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!

- [PR Build Summary](https://build.rerun.io/pr/5753)
- [Docs
preview](https://rerun.io/preview/5b0f69e4f74dfb9cc855ba62d4f1241e8a103c57/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/5b0f69e4f74dfb9cc855ba62d4f1241e8a103c57/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)
  • Loading branch information
emilk authored Apr 3, 2024
1 parent 27f3b15 commit 2434877
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
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

0 comments on commit 2434877

Please sign in to comment.