egui_charts/scales/
study_format.rs1use super::price_formatter::format_decimal;
4use crate::scales::{PriceFormatter, VolumeFormatter};
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum StudyFormat {
10 Price,
12 Volume,
14 Percent,
16 Fixed(usize),
18}
19
20impl StudyFormat {
21 pub fn format(self, value: f64, precision: Option<usize>) -> String {
23 if !value.is_finite() {
24 return "-".to_owned();
25 }
26 let decimals = precision
27 .unwrap_or(match self {
28 Self::Fixed(decimals) => decimals,
29 Self::Volume if value.abs() < 1_000.0 => 0,
30 _ => 2,
31 })
32 .min(if self == Self::Price { 18 } else { 12 });
33 match self {
34 Self::Volume => VolumeFormatter {
35 precision: decimals,
36 }
37 .format(value),
38 Self::Percent => format!("{}%", format_decimal(value, decimals)),
39 Self::Price | Self::Fixed(_) => format_decimal(value, decimals),
40 }
41 }
42}