use super::*;
pub(crate) fn parse_percent(input: &str) -> Option<f64> {
let trimmed: &str = input.trim();
if let Some(percent) = trimmed.strip_suffix('%') {
return percent
.trim()
.parse::<f64>()
.ok()
.map(|value: f64| value / 100.0);
}
trimmed.parse::<f64>().ok()
}
pub(crate) fn format_number(value: f64) -> String {
if !value.is_finite() {
return String::new();
}
let rounded: f64 = (value * 1_000_000.0).round() / 1_000_000.0;
if (rounded - rounded.round()).abs() < f64::EPSILON {
format!("{}", rounded.round() as i64)
} else {
let mut text = format!("{}", rounded);
if text.contains('.') {
while text.ends_with('0') {
text.pop();
}
if text.ends_with('.') {
text.pop();
}
}
text
}
}
pub(crate) fn map_series_name(series: &SeriesOption, option: &ChartOption) -> Option<String> {
if series.kind != SeriesType::Map {
return None;
}
let map_options: MapOptions = series.map.clone().unwrap_or_default();
if let Some(name) = &map_options.map_name {
return Some(name.clone());
}
map_options
.geo_index
.and_then(|index: usize| option.geo.get(index))
.and_then(|geo: &GeoOption| geo.map_name.clone())
}
pub(crate) fn map_series_aspect(series: &SeriesOption, option: &ChartOption) -> f64 {
let map_options: MapOptions = series.map.clone().unwrap_or_default();
if let Some(aspect) = map_options.aspect_scale {
return aspect;
}
map_options
.geo_index
.and_then(|index: usize| option.geo.get(index))
.and_then(|geo: &GeoOption| geo.aspect_scale)
.unwrap_or(DEFAULT_ASPECT_SCALE)
}