mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
Improve choice of number of decimals to show when hovering in plot
This commit is contained in:
@@ -185,7 +185,11 @@ impl RectElement for Bar {
|
||||
|
||||
fn default_values_format(&self, transform: &ScreenTransform) -> String {
|
||||
let scale = transform.dvalue_dpos();
|
||||
let y_decimals = ((-scale[1].abs().log10()).ceil().at_least(0.0) as usize).at_most(6);
|
||||
format!("\n{:.*}", y_decimals, self.value)
|
||||
let scale = match self.orientation {
|
||||
Orientation::Horizontal => scale[0],
|
||||
Orientation::Vertical => scale[1],
|
||||
};
|
||||
let decimals = ((-scale.abs().log10()).ceil().at_least(0.0) as usize).at_most(6);
|
||||
crate::plot::format_number(self.value, decimals)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,9 +269,15 @@ impl RectElement for BoxElem {
|
||||
|
||||
fn default_values_format(&self, transform: &ScreenTransform) -> String {
|
||||
let scale = transform.dvalue_dpos();
|
||||
let y_decimals = ((-scale[1].abs().log10()).ceil().at_least(0.0) as usize).at_most(6);
|
||||
let scale = match self.orientation {
|
||||
Orientation::Horizontal => scale[0],
|
||||
Orientation::Vertical => scale[1],
|
||||
};
|
||||
let y_decimals = ((-scale.abs().log10()).ceil().at_least(0.0) as usize)
|
||||
.at_most(6)
|
||||
.at_least(1);
|
||||
format!(
|
||||
"\nMax = {max:.decimals$}\
|
||||
"Max = {max:.decimals$}\
|
||||
\nQuartile 3 = {q3:.decimals$}\
|
||||
\nMedian = {med:.decimals$}\
|
||||
\nQuartile 1 = {q1:.decimals$}\
|
||||
|
||||
@@ -1648,6 +1648,7 @@ fn add_rulers_and_text(
|
||||
let mut text = elem.name().to_owned(); // could be empty
|
||||
|
||||
if show_values {
|
||||
text.push('\n');
|
||||
text.push_str(&elem.default_values_format(plot.transform));
|
||||
}
|
||||
|
||||
@@ -1694,8 +1695,8 @@ pub(super) fn rulers_at_value(
|
||||
|
||||
let text = {
|
||||
let scale = plot.transform.dvalue_dpos();
|
||||
let x_decimals = ((-scale[0].abs().log10()).ceil().at_least(0.0) as usize).at_most(6);
|
||||
let y_decimals = ((-scale[1].abs().log10()).ceil().at_least(0.0) as usize).at_most(6);
|
||||
let x_decimals = ((-scale[0].abs().log10()).ceil().at_least(0.0) as usize).clamp(1, 6);
|
||||
let y_decimals = ((-scale[1].abs().log10()).ceil().at_least(0.0) as usize).clamp(1, 6);
|
||||
if let Some(custom_label) = label_formatter {
|
||||
custom_label(name, &value)
|
||||
} else if plot.show_x && plot.show_y {
|
||||
|
||||
@@ -1647,3 +1647,16 @@ fn fill_marks_between(out: &mut Vec<GridMark>, step_size: f64, (min, max): (f64,
|
||||
});
|
||||
out.extend(marks_iter);
|
||||
}
|
||||
|
||||
/// Helper for formatting a number so that we always show at least a few decimals,
|
||||
/// unless it is an integer, in which case we never show any decimals.
|
||||
pub fn format_number(number: f64, num_decimals: usize) -> String {
|
||||
let is_integral = number as i64 as f64 == number;
|
||||
if is_integral {
|
||||
// perfect integer - show it as such:
|
||||
format!("{:.0}", number)
|
||||
} else {
|
||||
// make sure we tell the user it is not an integer by always showing a decimal or two:
|
||||
format!("{:.*}", num_decimals.at_least(1), number)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user